| Title: | Convert from One Colour Space to Another, Print a Ready-to-Paste Modern 'CSS' Syntax |
|---|---|
| Description: | Provides a comprehensive 'API' for colour conversion between popular colour spaces ('RGB', 'HSL', 'OKLab', 'OKLch', 'hex', and named colours) along with clean, modern 'CSS' Color Level 4 syntax output. Integrates seamlessly into 'Shiny' and 'Quarto' workflows. Includes nearest colour name lookup powered by a curated database of over 30,000 colour names. 'OKLab'/'OKLCh' colour spaces are described in Ottosson (2020) <https://bottosson.github.io/posts/oklab/>. 'CSS' Color Level 4 syntax follows the W3C specification <https://www.w3.org/TR/css-color-4/>. |
| Authors: | Yann Cohen [aut, cre] (ORCID: <https://orcid.org/0009-0009-0509-3609>) |
| Maintainer: | Yann Cohen <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.1 |
| Built: | 2026-05-26 10:12:39 UTC |
| Source: | https://github.com/iamyannc/colourspace |
A data frame containing each colour name, hex code, and coordinates in multiple colour spaces for fast nearest-neighbour search.
color_mapcolor_map
A data frame with the following columns:
Lowercase hex code starting with '#'.
Colour name (character).
Origin of the colour name: "r" or "extended"
(see color_names).
CIELAB components.
OKLCH components.
sRGB components (0-255).
HSL components.
Data curated by David Aerne (https://github.com/meodai).
Derived from color_names using farver decoders.
A dataset containing 31k+ color names compiled by David Aerne's meodai/color-names project, merged with R's built-in colour names.
color_namescolor_names
A data frame with three columns:
Lowercase hex triplet starting with '#'.
Color name as provided by the source (character).
Origin of the colour name: "r" for R's built-in
colours (grDevices::colors()) or "extended" for the
meodai/color-names community database.
Data curated by David Aerne (https://github.com/meodai).
https://github.com/meodai/color-names
Convert between colour spaces
convert_colourspace(value, from, to, fallback = c("all", "r", "none"))convert_colourspace(value, from, to, fallback = c("all", "r", "none"))
value |
Colour input. For |
from |
Source colour space. One of |
to |
Target colour space. One of |
fallback |
Behaviour when mapping
|
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).
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".
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")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")
Parse CSS color function strings (e.g., oklch(...), rgb(...), hsl(...))
or hex colors and convert them to a target color space. Automatically detects
the input format from the CSS syntax.
from_css(css, to = "hex", fallback = c("all", "r", "none"))from_css(css, to = "hex", fallback = c("all", "r", "none"))
css |
Character vector of CSS color strings. Supported formats:
|
to |
Target colour space. One of |
fallback |
Passed to |
Both modern (space-separated) and legacy (comma-separated) CSS notations are supported:
Modern: rgb(255 0 0), rgb(255 0 0 / 0.5)
Legacy: rgb(255, 0, 0), rgb(255, 0, 0, 0.5)
Legacy with explicit alpha: rgba(255, 0, 0, 0.5)
Alpha channels are currently parsed but ignored during conversion.
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" or to = "hex".
# Parse OKLCH CSS string to hex from_css("oklch(62.792% 0.258 29.221 / 1)") # Parse RGB CSS string (modern & legacy) from_css("rgb(255 0 0 / 1)", to = "oklch") from_css("rgb(255, 0, 0)", to = "hex") # Parse HSL CSS string from_css("hsl(210 50% 40% / 1)", to = "rgb") # Also works with hex colors from_css("#ff0000", to = "oklch") # Vectorized from_css(c("oklch(62.792% 0.258 29.221 / 1)", "rgb(0 255 0 / 1)"))# Parse OKLCH CSS string to hex from_css("oklch(62.792% 0.258 29.221 / 1)") # Parse RGB CSS string (modern & legacy) from_css("rgb(255 0 0 / 1)", to = "oklch") from_css("rgb(255, 0, 0)", to = "hex") # Parse HSL CSS string from_css("hsl(210 50% 40% / 1)", to = "rgb") # Also works with hex colors from_css("#ff0000", to = "oklch") # Vectorized from_css(c("oklch(62.792% 0.258 29.221 / 1)", "rgb(0 255 0 / 1)"))
Convert HEX to HSL
hex_to_hsl(hex)hex_to_hsl(hex)
hex |
Character vector of hex colour strings. |
Numeric vector (length 3) or matrix with columns h, s, l.
hex_to_hsl("#336699")hex_to_hsl("#336699")
Reverse lookup using the bundled name database. When an exact match is not
found, behaviour depends on fallback:
hex_to_name(hex, fallback = c("all", "r", "none"))hex_to_name(hex, fallback = c("all", "r", "none"))
hex |
Character vector of hex colour strings. |
fallback |
One of |
"all"(default) Return the closest named colour from the full 31 000+ colour database.
"r"Always return the nearest R built-in colour
(from grDevices::colors()). Useful when the result will be used in
base-R or ggplot2 plotting functions.
"none"Return NA for colours without an exact name match.
Character vector of colour names (or NA).
hex_to_name("#c93f38") hex_to_name("#111114", fallback = "all") hex_to_name("#ff5733", fallback = "r")hex_to_name("#c93f38") hex_to_name("#111114", fallback = "all") hex_to_name("#ff5733", fallback = "r")
Convert HEX to OKLAB
hex_to_oklab(hex)hex_to_oklab(hex)
hex |
Character vector of hex colour strings. |
Numeric vector (length 3) or matrix with columns l, a, b.
hex_to_oklab("#ff0000")hex_to_oklab("#ff0000")
Convert HEX to OKLCH
hex_to_oklch(hex)hex_to_oklch(hex)
hex |
Character vector of hex colour strings. |
Numeric vector (length 3) or matrix with columns l, c, h.
hex_to_oklch("#ff0000")hex_to_oklch("#ff0000")
Convert HEX to RGB
hex_to_rgb(hex)hex_to_rgb(hex)
hex |
Character vector of hex colour strings. |
Numeric vector (length 3) or matrix with columns r, g, b.
hex_to_rgb("#336699")hex_to_rgb("#336699")
Convert HSL to HEX
hsl_to_hex(hsl)hsl_to_hex(hsl)
hsl |
Numeric vector/matrix of HSL values (h: 0-360, s/l: 0-100). |
Character vector of hex colours.
hsl_to_hex(c(210, 50, 40))hsl_to_hex(c(210, 50, 40))
Looks up CSS-style colour names from the bundled meodai list and returns hex values.
name_to_hex(name)name_to_hex(name)
name |
Character vector of colour names (case-insensitive). |
Character vector of hex colours.
name_to_hex("100 Mph")name_to_hex("100 Mph")
Convert OKLAB to HEX
oklab_to_hex(oklab)oklab_to_hex(oklab)
oklab |
Numeric vector/matrix of OKLAB values ( |
Character vector of hex colours.
oklab_to_hex(c(0.628, 0.225, 0.126))oklab_to_hex(c(0.628, 0.225, 0.126))
Convert OKLCH to HEX
oklch_to_hex(oklch)oklch_to_hex(oklch)
oklch |
Numeric vector/matrix of OKLCH values ( |
Character vector of hex colours.
oklch_to_hex(c(0.628, 0.258, 29.221))oklch_to_hex(c(0.628, 0.258, 29.221))
Convert RGB to HEX
rgb_to_hex(rgb)rgb_to_hex(rgb)
rgb |
Numeric vector/matrix of RGB values (0-255). |
Character vector of hex colours.
rgb_to_hex(c(51, 102, 153))rgb_to_hex(c(51, 102, 153))
Convert colours between supported spaces and return a character vector in
modern CSS Color 4 functional notation (space-separated components with an
optional alpha channel introduced by /).
to_css( value, from = NULL, to = c("oklch", "oklab", "rgb", "hsl", "hex"), alpha = 1, fallback = c("all", "r", "none") )to_css( value, from = NULL, to = c("oklch", "oklab", "rgb", "hsl", "hex"), alpha = 1, fallback = c("all", "r", "none") )
value |
Colour input. For |
from |
Source colour space. One of |
to |
Target CSS function. One of |
alpha |
Alpha channel as numbers in |
fallback |
Passed to |
A character vector of CSS colors.
OKLCH in CSS: why we moved from RGB and HSL for a detailed explanation of why OKLCH is the recommended colour space for modern CSS.
to_css("red") to_css("#ff5a3c", from = "hex", to = "oklch", alpha = 0.8) to_css(c("#ff0000", "#00ff00"), to = "rgb", alpha = c(1, 0.5))to_css("red") to_css("#ff5a3c", from = "hex", to = "oklch", alpha = 0.8) to_css(c("#ff0000", "#00ff00"), to = "rgb", alpha = c(1, 0.5))