This set of functions allows you to modify colours as given by strings, whithout first decoding them. For large vectors of colour values this should provide a considerable speedup.

set_channel(
  colour,
  channel,
  value,
  space = "rgb",
  white = "D65",
  na_value = NA
)

add_to_channel(
  colour,
  channel,
  value,
  space = "rgb",
  white = "D65",
  na_value = NA
)

multiply_channel(
  colour,
  channel,
  value,
  space = "rgb",
  white = "D65",
  na_value = NA
)

raise_channel(
  colour,
  channel,
  value,
  space = "rgb",
  white = "D65",
  na_value = NA
)

cap_channel(
  colour,
  channel,
  value,
  space = "rgb",
  white = "D65",
  na_value = NA
)

get_channel(colour, channel, space = "rgb", white = "D65", na_value = NA)

Arguments

colour

A character string giving colours, either as hexadecimal strings or accepted colour names.

channel

The channel to modify or extract as a single letter, or 'alpha' for the alpha channel.

value

The value to modify with

space

The colour space the channel pertains to. Allowed values are: "cmy", "cmyk", "hsl", "hsb", "hsv", "lab" (CIE L*ab), "hunterlab" (Hunter Lab), "lch" (CIE Lch(ab) / polarLAB), "luv", "rgb" (sRGB), "xyz", "yxy" (CIE xyY), or "hcl" (CIE Lch(uv) / polarLuv)

white

The white reference of the channel colour space. Will only have an effect for relative colour spaces such as Lab and luv. Any value accepted by as_white_ref() allowed.

na_value

A valid colour string or NA to use when colour contains NA elements. The general approach in farver is to carry NA values over, but if you want to mimick col2rgb() you should set na_value = 'transparent', i.e. treat NA as transparent white.

Value

A character vector of the same length as colour (or a numeric vector in the case of get_channel())

See also

Other encoding and decoding functions: decode_colour(), encode_colour()

Examples

spectrum <- rainbow(10) # set a specific channel set_channel(spectrum, 'r', c(10, 50))
#> [1] "#0A0000FF" "#329900FF" "#0AFF00FF" "#32FF00FF" "#0AFF66FF" "#32FFFFFF" #> [7] "#0A66FFFF" "#3200FFFF" "#0A00FFFF" "#320099FF"
set_channel(spectrum, 'l', 50, space = 'lab')
#> [1] "#F40000FF" "#B86000FF" "#4B8600FF" "#009200FF" "#009100FF" "#008B8EFF" #> [7] "#1D6BFFFF" "#7A3FFFFF" "#C600F9FF" "#ED008AFF"
set_channel(spectrum, 'alpha', c(0.5, 1))
#> [1] "#FF000080" "#FF9900" "#CCFF0080" "#33FF00" "#00FF6680" "#00FFFF" #> [7] "#0066FF80" "#3300FF" "#CC00FF80" "#FF0099"
# Add value to channel add_to_channel(spectrum, 'r', c(10, 50))
#> [1] "#FF0000FF" "#FF9900FF" "#D6FF00FF" "#65FF00FF" "#0AFF66FF" "#32FFFFFF" #> [7] "#0A66FFFF" "#6500FFFF" "#D600FFFF" "#FF0099FF"
add_to_channel(spectrum, 'l', 50, space = 'lab')
#> [1] "#FFB380FF" "#FFE663FF" "#DFFF2DFF" "#69FF41FF" "#55FF86FF" "#4BFFFFFF" #> [7] "#D6EAFFFF" "#EC9DFFFF" "#FFB7FFFF" "#FFB1FFFF"
# Multiply a channel multiply_channel(spectrum, 'r', c(10, 50))
#> [1] "#FF0000FF" "#FF9900FF" "#FFFF00FF" "#FFFF00FF" "#00FF66FF" "#00FFFFFF" #> [7] "#0066FFFF" "#FF00FFFF" "#FF00FFFF" "#FF0099FF"
multiply_channel(spectrum, 'l', 50, space = 'lab')
#> [1] "#FFB380FF" "#FFE663FF" "#DFFF2DFF" "#69FF41FF" "#55FF86FF" "#4BFFFFFF" #> [7] "#DCF0FFFF" "#FFCCFFFF" "#FFB7FFFF" "#FFB1FFFF"
# set a lower bound on a channel raise_channel(spectrum, 'r', c(10, 50))
#> [1] "#FF0000FF" "#FF9900FF" "#CCFF00FF" "#33FF00FF" "#0AFF66FF" "#32FFFFFF" #> [7] "#0A66FFFF" "#3300FFFF" "#CC00FFFF" "#FF0099FF"
raise_channel(spectrum, 'l', 20, space = 'lab')
#> [1] "#FF0000FF" "#FF9900FF" "#CCFF00FF" "#33FF00FF" "#00FF66FF" "#00FFFFFF" #> [7] "#0066FFFF" "#3300FFFF" "#CC00FFFF" "#FF0099FF"
# set an upper bound on a channel cap_channel(spectrum, 'r', c(100, 50))
#> [1] "#640000FF" "#329900FF" "#64FF00FF" "#32FF00FF" "#00FF66FF" "#00FFFFFF" #> [7] "#0066FFFF" "#3200FFFF" "#6400FFFF" "#320099FF"
cap_channel(spectrum, 'l', 20, space = 'lab')
#> [1] "#920000FF" "#631600FF" "#183B00FF" "#004200FF" "#004200FF" "#003F44FF" #> [7] "#0029AFFF" "#0000D6FF" "#6B00A4FF" "#900041FF"