Colors
The colors library provides functions for color manipulation, color space conversion, and color palettes.
Functions
Color(input: string)
Convert a color string or named color into a color value.
The output is an Oklch expression with 3 or 4 operands: the lightness L (0
to 1), the chroma C, the hue H in degrees, and an optional alpha (0 to 1). Use
AsRgb to get its sRGB components.
The string is read as 8 bits for each channel, so an alpha of 0.5 is read as
128/255, about 0.502.
Supports various formats:
- Hex:
#rgb,#rrggbb,#rrggbbaa(e.g.'#f00','#ff0000','#ff000080') - RGB/RGBA:
rgb(r, g, b),rgba(r, g, b, a)(e.g.'rgb(255, 0, 0)','rgba(255, 0, 0, 0.5)') - HSL:
hsl(h, s%, l%),hsla(h, s%, l%, a)(e.g.'hsl(0, 100%, 50%)') - OKLCH:
oklch(l%, c, h),oklch(l%, c, h / a)(e.g.'oklch(50% 0.3 240)') - Named colors: From the
NAMED_COLORSpalette (see below)
["Color", "'#f00'"]
// ➔ ["Oklch", 0.628, 0.258, 29.234]
["Color", "'rgba(255, 0, 0, 0.5)'"]
// ➔ ["Oklch", 0.628, 0.258, 29.234, 0.502]
["Color", "'red'"]
// ➔ ["Oklch", 0.559, 0.22, 29.619]
["AsRgb", ["Color", "'red'"]]
// ➔ ["Rgb", 0.843, 0.09, 0.043]
["Color", "'blue'"]
// ➔ ["Oklch", 0.607, 0.194, 254.822]
Named Colors
The Color function supports a comprehensive palette of named colors from the NAMED_COLORS object:
| Color Name | Swatch | Color Name | Swatch |
|---|---|---|---|
red | orange | ||
yellow | lime | ||
green | teal | ||
cyan | blue | ||
indigo | purple | ||
magenta | black | ||
white | dark-grey | ||
grey | light-grey | ||
brown | olive | ||
midnight | sky | ||
carbon | charcoal | ||
slate | graphite | ||
stone | ash | ||
mist | snow |
Colormap(name: string)
Colormap(name: string, n: number)
Colormap(name: string, t: number)
Sample colors from a named palette.
- If only a name is provided, returns the full list of colors in the palette.
- If an integer
n≥ 2 is provided, returnsnevenly-spaced colors. - If a real number
tbetween 0 and 1 is provided, returns the color at positiont.
["Colormap", "'viridis'", 5]
Available Palettes
Sequential
These palettes are perceptually uniform and suitable for ordered data.
Each sequential palette also has a -reversed variant (e.g., viridis-reversed).
| Name | |
|---|---|
viridis | |
inferno | |
magma | |
plasma | |
turbo | |
cividis | |
rocket | |
mako | |
grey |
Diverging
These palettes are symmetric around a neutral midpoint.
Each diverging palette also has a -reversed variant (e.g., roma-reversed).
| Name | |
|---|---|
roma | |
rdbu | |
coolwarm | |
vik | |
broc | |
ocean-balance |
Categorical
These palettes are designed for discrete, non-ordered categories.
| Name | |
|---|---|
tycho11 | Default categorical palette. Provides very good perceptual separation for up to 8 colors. |
tycho-dark11 | A variant of Tycho 11 optimized for dark backgrounds. |
tycho-robust11 | Reinforced variant that improves separability under color vision deficiency and low-contrast environments. |
tycho-soft11 | Low-contrast palette with soft tones, ideal for filled areas and dashboards. Less suitable for thin lines. |
tycho-soft-dark11 | Soft-contrast palette optimized for charcoal backgrounds, suitable for both lines and filled surfaces. |
tycho-bold11 | High-contrast palette with strong separation, ideal for thin lines and dense line charts. |
tycho-bold-dark11 | Energetic, high-contrast palette for dark UI, providing strong structural consistency for thin strokes. |
tableau10 | The classic Tableau 10 palette, also used as the default in Matplotlib. |
mathematica10 | Palette inspired by the default colors used in Wolfram Mathematica. |
cupertino10 | Apple system palette optimized for light appearance. |
cupertino-dark10 | Apple system palette optimized for dark appearance. |
kelly22 | Set of 22 colors maximized for contrast, suitable for datasets with a large number of categories. |
spectrum12 | Balanced palette of 12 colors covering the full visible spectrum. |
ColorToColorspace(color, colorspace: string)
Convert a color to its components in a specified color space.
Supported color spaces: "rgb", "hsl", "oklch", "oklab".
An sRGB channel outside [0, 1] is extended sRGB: it describes a color outside
the sRGB gamut (a Display-P3 display can show it), and it is kept by every
conversion among rgb, oklab and oklch. HSV and HSL describe only the sRGB
gamut, so their saturation, value and lightness are clamped to [0, 1] (an
infinite value reads as its bound), and converting an extended sRGB color to
HSV or HSL clips each channel. A NaN channel, an infinite hue, and an infinite
sRGB, OKLab or OKLCh channel are an error.
["ColorToColorspace", "'#f00'", "'hsl'"]
// ➔ ["Tuple", 0, 1, 0.5]
ColorFromColorspace(components: tuple, colorspace: string)
Convert components in a specified color space to a color value. The result
is the color expression of that space: Rgb, Hsl, Oklch or Oklab.
["ColorFromColorspace", ["Tuple", 0, 1, 0.5], "'hsl'"]
// ➔ ["Hsl", 0, 1, 0.5]
["AsRgb", ["ColorFromColorspace", ["Tuple", 0, 1, 0.5], "'hsl'"]]
// ➔ ["Rgb", 1, 0, 0]