Learn R Programming

colourspace (version 0.0.1)

to_css: Format colours as modern CSS color functions

Description

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 /).

Usage

to_css(
  value,
  from = NULL,
  to = c("oklch", "oklab", "rgb", "hsl", "hex"),
  alpha = 1,
  fallback = TRUE,
  distance = c("lab", "oklch", "rgb", "hsl")
)

Value

A character vector of CSS colors.

Arguments

value

Colour input. For from = "hex" or from = "name", a character vector. For numeric spaces (rgb, hsl, oklch, oklab), 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". If NULL (default), to_css() will infer "hex" vs "name" for character inputs.

to

Target CSS function. One of "oklch" (default), "oklab", "rgb", "hsl", or "hex".

alpha

Alpha channel as numbers in [0, 1]. Recycled to match the number of colours.

fallback

Behaviour when from = "name" (via convert_colourspace()/name_to_hex()) or when generating names elsewhere. TRUE (default) returns the closest named colour; FALSE returns NA. Included for API consistency with convert_colourspace().

distance

Distance metric used for nearest-name fallback when applicable. Included for API consistency with convert_colourspace().

See Also

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.

Examples

Run this code
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))

Run the code above in your browser using DataLab