Skip to contents

[Maturing]

interpolate_colors() interpolate colors for sequential, diverging, and qualitative color scales.

Usage

interpolate_colors(
  n,
  colors = getOption("BRANDR_COLOR_SEQUENTIAL"),
  type = "seq",
  alpha = NULL,
  direction = 1,
  ...
)

Arguments

n

This parameter accepts two types of inputs:

  • If the value is an integer number and type is "seq" or "div", the function will return a discrete color spectrum with n colors

  • If the value is an integer number and type is "qual", the function will return n colors from the colors parameter, repeating them if necessary

  • If the value is a numeric vector between 0 and 1, the function will return the color positions at n considering a continuous color spectrum ranging from 0 to 1

colors

(Optional) A character vector of colors to use in the scale. If NULL, brandr will choose the colors based on the type argument.

type

(Optional) A character string indicating the type of color scale: "seq"/"sequential", "div"/"diverging", or "qual"/"qualitative" (Default: seq).

alpha

(Optional) A number between 0 and 1, indicating the transparency of the colors (Default: NULL).

direction

(Optional) A number (1 or -1) indicating the direction of the colors. If 1, the order remains the same. If -1, the order is reversed. (Default: 1).

...

Additional arguments passed to colorRampPalette() when creating the color ramp. Only valid when type is "seq" or "div".

Value

A character vector with hexadecimal color codes.

See also

Other color functions: color_brand_sequential()

Examples

interpolate_colors(3, colors = c("red", "blue"), type = "seq")
#> [1] "#FF0000" "#7F007F" "#0000FF"
#> [1] "#FF0000" "#7F007F" "#0000FF" # Expected

interpolate_colors(3, colors = c("red", "blue"), direction = -1)
#> [1] "#0000FF" "#7F007F" "#FF0000"
#> [1] "#0000FF" "#7F007F" "#FF0000" # Expected

interpolate_colors(3, colors = c("red", "blue"), alpha = 0.5)
#> [1] "#FF000080" "#7F007F80" "#0000FF80"
#> [1] "#FF000080" "#7F007F80" "#0000FF80" # Expected

# `type = "seq"` and `type = "div"` produce the same result
interpolate_colors(3, colors = c("red", "white", "blue"), type = "div")
#> [1] "#FF0000" "#FFFFFF" "#0000FF"
#> [1] "#FF0000" "#FFFFFF" "#0000FF" # Expected

interpolate_colors(3, colors = c("red", "blue"), type = "qual")
#> [1] "#FF0000" "#0000FF" "#FF0000"
#> [1] "#FF0000" "#0000FF" "#FF0000" # Expected