Skip to contents

Make a specially-formatted color palette based on color codes.

Usage

util_make_palette(colors, continuous = length(colors) < 5,
  divergent = length(colors) > 2, polynomial = FALSE, degrees = 6,
  pad = 10, name = "custom", preview = TRUE, print = TRUE)

Arguments

colors

A vector of color names or HEX codes, or a matrix-like object with colors in columns, and their RGB values in separate rows.

continuous

Logical; if TRUE, colors are treated as points in a linear gradient. One provided color will be from white to that color. Two provided colors will be between those colors. Three or four provided colors will be between the first and last color, with the central color (or average of the central colors) as the midpoint.

divergent

Logical; if TRUE, marks continuous scales as divergent, which will reverse the lower half of the scale.

polynomial

Logical; if TRUE, will fit a polynomial regression model to each color channel in the specified colors sequence. Used to either compress a long sequence (e.g., model a fully manually specified scale), or interpolate a scale between anchors.

degrees

Number of polynomial degrees, if polynomial is TRUE.

pad

If polynomial is TRUE, number of repeated observations of the initial and final colors in the sequence to add in order to reduce warping at the edges.

name

Name of the palette.

preview

Logical; if TRUE, makes a plot showing the palette colors / scale.

print

Logical; if FALSE, will not print a version of the palette.

Value

An invisible list of the created palette.

Examples

# a discrete palette
util_make_palette(c("red", "green", "blue"), FALSE)
#> {
#>   "name": "custom",
#>   "type": "discrete",
#>   "colors": ["#FF0000", "#00FF00", "#0000FF"]
#> }

# a continuous palette
util_make_palette("red")
#> {
#>   "name": "custom",
#>   "type": "continuous",
#>   "colors": [
#>     [
#>       [255, 0, 0],
#>       [-127.5, 0, 0]
#>     ],
#>     [127.5, 0, 0],
#>     [
#>       [0, 0, 0],
#>       [127.5, 0, 0]
#>     ]
#>   ]
#> }

# a divergent continuous palette
util_make_palette(c("red", "green"), divergent = TRUE)
#> {
#>   "name": "custom",
#>   "type": "continuous-divergent",
#>   "colors": [
#>     [
#>       [0, 255, 0],
#>       [127.5, -127.5, 0]
#>     ],
#>     [127.5, 127.5, 0],
#>     [
#>       [255, 0, 0],
#>       [-127.5, 127.5, 0]
#>     ]
#>   ]
#> }