convert_color: Convert colors to a given color model

View source: R/convert_color.R

convert_colorR Documentation

Convert colors to a given color model

Description

Convert a vector of colors to a given color model.

Usage

convert_color(x, model)

as.rgb(x)

as.rgba(x)

as.hsv(x)

as.hsl(x)

as.hsi(x)

as.hcl(x)

as.lch(x)

as.lab(x)

as.cmyk(x)

as.css(x)

as.hex(x)

as.temperature(x)

as.wavelength(x)

as.ryb(x)

Arguments

x

vector of colors specified as hex strings or named R colors.

model

string defining the color model; valid models are rgb, rgba, ryb, hsv, hsl, hsi, hcl, lch, lab, cmyk, css, hex, temperature, wavelength.

Details

All colors in chroma are represented internally as hex codes in sRGB space. So if a color exists in that space, converting it to most other models will be lossless and reversible. For example, converting from rgb to lab back to rgb should give the same R, G, and B values. If the starting color is not representable in sRGB, it will be converted to the closest sRGB color and the reversibility will be lost. In addition, the temperature and wavelength color "models" are very different because they represent only one aspect of the color (its temperature or its corresponding wavelength) and conversion to those is almost never lossless or reversible.

Value

A matrix containing the color components in most cases, except for the models:

css

a vector of css color definition strings,

hex

a vector of hexadecimal strings defining colors,

temperature

a vector of numbers corresponding to the temperature of the colors in Kelvin,

wavelength

a vector of numbers corresponding to the wavelength of monochromatic light closest to the input colors.

See Also

parse_color for a general function to parse colors in various specifications.

Examples

convert_color("red", model="rgb")
convert_color("red", model="hsl")
convert_color("red", model="hcl")
convert_color("red", model="cmyk")

as.hsv("red")
as.lab("red")
as.lch("red")

as.css("red")
as.hex("red")
as.temperature("red")
as.wavelength("red")

# Can be vectorised
as.rgb(colors()[1:5])
as.rgb(c("#B55FFC", "blue", "purple", "#6A9F16"))

# Starting from sRGB leads to reversibility
col <- rgb(150, 100, 200, maxColorValue=255)
as.cmyk(col)
as.rgb(cmyk(as.cmyk(col)))
# or near-reversability
col <- lab(0.5, 0.5, 0)
col
as.lab(col)

# But starting outside of sRGB looses reversibility
col <- lab(0.5, -0.5, -1)
# this L*a*b* color does not exist in sRGB => it is converted to the
# closest sRGB equivalent
col
# and is different from the original L*a*b* specification
as.lab(col)

jiho/chroma documentation built on Nov. 26, 2022, 2:39 a.m.