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), "oklab" , "lch" (CIE Lch(ab) / polarLAB), "luv", "rgb" (sRGB), "xyz", "yxy" (CIE xyY), "hcl" (CIE Lch(uv) / polarLuv), or "oklch" (Polar form of oklab)

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] "#0A0000" "#329900" "#0AFF00" "#32FF00" "#0AFF66" "#32FFFF" "#0A66FF"
#>  [8] "#3200FF" "#0A00FF" "#320099"
set_channel(spectrum, 'l', 50, space = 'lab')
#>  [1] "#F40000" "#B86000" "#4B8600" "#009200" "#009100" "#008B8E" "#1D6BFF"
#>  [8] "#7A3FFF" "#C600F9" "#ED008A"
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] "#FF0000" "#FF9900" "#D6FF00" "#65FF00" "#0AFF66" "#32FFFF" "#0A66FF"
#>  [8] "#6500FF" "#D600FF" "#FF0099"
add_to_channel(spectrum, 'l', 50, space = 'lab')
#>  [1] "#FFB380" "#FFE663" "#DFFF2D" "#69FF41" "#55FF86" "#4BFFFF" "#D6EAFF"
#>  [8] "#EC9DFF" "#FFB7FF" "#FFB1FF"

# Multiply a channel
multiply_channel(spectrum, 'r', c(10, 50))
#>  [1] "#FF0000" "#FF9900" "#FFFF00" "#FFFF00" "#00FF66" "#00FFFF" "#0066FF"
#>  [8] "#FF00FF" "#FF00FF" "#FF0099"
multiply_channel(spectrum, 'l', 50, space = 'lab')
#>  [1] "#FFB380" "#FFE663" "#DFFF2D" "#69FF41" "#55FF86" "#4BFFFF" "#DCF0FF"
#>  [8] "#FFCCFF" "#FFB7FF" "#FFB1FF"

# set a lower bound on a channel
raise_channel(spectrum, 'r', c(10, 50))
#>  [1] "#FF0000" "#FF9900" "#CCFF00" "#33FF00" "#0AFF66" "#32FFFF" "#0A66FF"
#>  [8] "#3300FF" "#CC00FF" "#FF0099"
raise_channel(spectrum, 'l', 20, space = 'lab')
#>  [1] "#FF0000" "#FF9900" "#CCFF00" "#33FF00" "#00FF66" "#00FFFF" "#0066FF"
#>  [8] "#3300FF" "#CC00FF" "#FF0099"

# set an upper bound on a channel
cap_channel(spectrum, 'r', c(100, 50))
#>  [1] "#640000" "#329900" "#64FF00" "#32FF00" "#00FF66" "#00FFFF" "#0066FF"
#>  [8] "#3200FF" "#6400FF" "#320099"
cap_channel(spectrum, 'l', 20, space = 'lab')
#>  [1] "#920000" "#631600" "#183B00" "#004200" "#004200" "#003F44" "#0029AF"
#>  [8] "#0000D6" "#6B00A4" "#900041"