View source: R/convert_color.R
convert_color | R Documentation |
Convert a vector of colors to a given color model.
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)
x |
vector of colors specified as hex strings or named R colors. |
model |
string defining the color model; valid models are |
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.
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.
parse_color
for a general function to parse colors in various specifications.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.