Skip to contents

Interpolate the set of colours in palettes_palette or palettes_colour objects to create new colour palettes.

Usage

pal_ramp(
  palette,
  n = NULL,
  direction = 1,
  space = "lab",
  interpolate = c("linear", "spline")
)

# S3 method for class 'palettes_colour'
pal_ramp(
  palette,
  n = NULL,
  direction = 1,
  space = "lab",
  interpolate = c("linear", "spline")
)

# S3 method for class 'palettes_palette'
pal_ramp(
  palette,
  n = NULL,
  direction = 1,
  space = "lab",
  interpolate = c("linear", "spline")
)

Arguments

palette

An object of class palettes_palette or palettes_colour.

n

An integer specifying the number of colours to return.

direction

Sets the order of colours in the scale. If 1, the default, colours are ordered from first to last. If -1, the order of colours is reversed.

space

The colour space to interpolate in. One of: "cmy", "hsl", "hsb", "hsv", "lab" (CIE L*ab), "hunterlab" (Hunter Lab), "oklab", "lch" (CIE Lch(ab) / polarLAB), "luv", "rgb" (sRGB), "xyz", "yxy" (CIE xyY), "hcl" (CIE Lch(uv) / polarLuv), or "oklch" (Polar form of oklab).

interpolate

The interpolation method. Either "linear" (default) or "spline".

Value

An object of the same type as palette. The output has the following properties:

  • For objects of class palettes_colour: A colour vector with n colours.

  • For objects of class palettes_palette: Colour palettes with n colours in each palette.

Examples

# The class returned after interpolation matches the input class.
x <- pal_colour(c("darkslateblue", "cornflowerblue", "slategray1"))
y <- pal_palette(blues = x)
class(pal_ramp(x))
#> [1] "palettes_colour" "vctrs_vctr"     
class(pal_ramp(y))
#> [1] "palettes_palette" "vctrs_list_of"    "vctrs_vctr"       "list"            

# Choose between linear and spline interpolation.
pal_ramp(x, n = 7, interpolate = "linear")
#> <palettes_colour[7]>
#>  #483D8B
#>  #535AAB
#>  #5C77CB
#>  #6495ED
#>  #89AEF3
#>  #A9C8F9
#>  #C6E2FF
pal_ramp(x, n = 7, interpolate = "spline")
#> <palettes_colour[7]>
#>  #483D8B
#>  #495CB3
#>  #5179D4
#>  #6495ED
#>  #80B0FD
#>  #A2CAFF
#>  #C6E2FF

# Palettes will have the same length after interpolation, regardless of the
# number of colours in the original palette.
z <- pal_palette(
  Egypt = c("#DD5129", "#0F7BA2", "#43B284", "#FAB255"),
  Java  = c("#663171", "#CF3A36", "#EA7428", "#E2998A", "#0C7156")
)
pal_ramp(z, n = 5)
#> <palettes_palette[2]>
#> $Egypt
#> <palettes_colour[5]>
#>  #DD5129
#>  #737484
#>  #369694
#>  #83B479
#>  #FAB255
#> 
#> $Java
#> <palettes_colour[5]>
#>  #663171
#>  #CF3A36
#>  #EA7428
#>  #E2998A
#>  #0C7156
#>