Learn R Programming

colourspace (version 0.0.1)

convert_colourspace: Convert between colour spaces

Description

Convert between colour spaces

Usage

convert_colourspace(
  value,
  from,
  to,
  fallback = TRUE,
  distance = c("lab", "oklch", "rgb", "hsl")
)

Value

For scalar inputs, a named numeric vector (or hex string or colour name). For vectorised inputs, a matrix with one row per input colour or a character vector for to = "name".

Arguments

value

Colour input. For from = "hex" or from = "name", a character vector. For numeric spaces (rgb, hsl, oklch), a numeric vector of length 3, matrix/data frame with three columns, or a list of such vectors.

from

Source colour space. One of "hex", "rgb", "hsl", "oklab", "oklch", or "name".

to

Target colour space. One of "hex", "rgb", "hsl", "oklab", "oklch", or "name" (reverse lookup).

fallback

Behaviour when mapping to = "name" and no exact hex/name match is found. TRUE (default) returns the closest named colour using distance (a warning is issued). FALSE returns NA for unknown colours.

distance

Distance metric for nearest-colour fallback: one of "lab" (default), "oklch", "rgb", or "hsl".

Details

All conversions and nearest-colour calculations are powered by the farver package. Hex inputs may include an alpha channel (#rgba/#rrggbbaa), but alpha is currently ignored (stripped before decoding).

Examples

Run this code
convert_colourspace("#ff0000", from = "hex", to = "rgb")
convert_colourspace(c(255, 255, 0), from = "rgb", to = "hex")
convert_colourspace(c("#ff0000", "#00ff00"), from = "hex", to = "oklch")

Run the code above in your browser using DataLab