Skip to content

The scales packages keeps track of a set of palettes it considers 'known'. The benefit of a known palette is that it can be called by name in functions as as_continuous_pal() or as_discrete_pal().

Usage

get_palette(name, ...)

set_palette(name, palette, warn_conflict = TRUE)

palette_names()

reset_palettes()

Arguments

name

A string giving the palette name.

...

Additional arguments to pass to palette when it is a function but not a palette class function.

palette

A palette, function or character vector.

warn_conflict

A boolean which if TRUE (default), warns when replacing a known palette.

Value

The get_palette() function returns a palette. The set_palette() function is called for side effects and returns nothing.

Examples

# Get one of the known palettes
get_palette("hue")
#> function (n) 
#> {
#>     if (n == 0) {
#>         cli::cli_abort("Must request at least one colour from a hue palette.")
#>     }
#>     if ((diff(h)%%360) < 1) {
#>         h[2] <- h[2] - 360/n
#>     }
#>     hues <- seq(h[1], h[2], length.out = n)
#>     hues <- (hues + h.start)%%360
#>     hcl <- cbind(hues, c, l)
#>     pal <- farver::encode_colour(hcl, from = "hcl")
#>     if (direction == -1) {
#>         rev(pal)
#>     }
#>     else {
#>         pal
#>     }
#> }
#> <bytecode: 0x55654e659ab8>
#> <environment: 0x556555857128>
#> attr(,"class")
#> [1] "pal_discrete" "scales_pal"   "function"    
#> attr(,"type")
#> [1] "colour"
#> attr(,"nlevels")
#> [1] 255

# Set a new custom palette
cols <- c("palegreen", "deepskyblue", "magenta")
set_palette("aurora", palette = cols)

# Palette is now known
"aurora" %in% palette_names()
#> [1] TRUE
as_continuous_pal("aurora")
#> function (x) 
#> {
#>     lab_out <- cbind(l_interp(x), u_interp(x), v_interp(x))
#>     out <- farver::encode_colour(lab_out, alpha = alpha_interp(x), 
#>         from = "lab")
#>     out[is.na(out)] <- na.color
#>     out
#> }
#> <bytecode: 0x55654cb08818>
#> <environment: 0x55655696b548>
#> attr(,"class")
#> [1] "pal_continuous" "scales_pal"     "function"      
#> attr(,"type")
#> [1] "colour"
#> attr(,"na_safe")
#> [1] FALSE

# Resetting palettes
reset_palettes()