Colors
The colors library holds the color values and the functions that convert, mix and compare them. This introduction describes what a color value is and what you can read from it.
A color value
A color has the type color. You make one with a constructor in one of five
color spaces (rgb, hsl, hsv, oklab, oklch), with rgb255 for sRGB
channels from 0 to 255, or with color from a CSS string. A color keeps the
space it was written in, and an optional alpha (opacity) from 0 to 1.
(rgb(1, 0.5, 0), rgb255(255, 128, 0), color("#ff8000"), color("hsl(120 100% 50%)"))
// ➔ (Rgb(1, 0.5, 0), Rgb(1, 128/255, 0), Rgb(1, 0.5019607843137255, 0), Hsl(120, 1, 0.5))
color of a string answers in the space of the string: an Rgb color for a
hex code, an rgb() spelling or a color name, an Hsl color for an hsl()
spelling, and an Oklch or Oklab color for an oklch() or oklab()
spelling. The channels of a hex color are whole levels: the green channel of
"#ff8000" is 128/255.
The fields of a color
Every color has the same fields, whatever space it was written in:
| Field | Value |
|---|---|
r, g, b | The sRGB channels, from 0 to 1 for a color in the sRGB gamut. A color outside the gamut has a channel under 0 or over 1. |
l, c, h | The OKLCh lightness (0 to 1), chroma (0 to about 0.4) and hue (degrees, in [0, 360)). The hue of a gray is 0. |
alpha | The opacity, from 0 to 1; 1 for a color with no alpha. |
hex | The string of colorToString: "#rrggbb", or "#rrggbbaa" with an alpha. |
let c = hsl(0, 1, 0.5)
(c.r, c.g, c.b, c.alpha, c.hex)
// ➔ (1, 0, 0, 1, "#ff0000")
A field of the space the color is written in is the channel as you wrote it, so an exact channel stays exact. A field of another space is a number.
(rgb(1/3, 0, 0).r, rgb(1, 0, 0).l)
// ➔ (1/3, 0.6279553606145515)
The channels of the other spaces (the a and b of OKLab, the saturation of
HSL and HSV) have no field, because their names are taken. Read them with
colorToColorspace(c, "hsl").
Changing a channel
c.update(…) gives a copy of a color with some channels replaced. The keys
are of one model: r, g, b give an Rgb color, and l, c, h an
Oklch color. alpha goes with either, or alone, and then the color keeps
its space.
let c = rgb(1, 0, 0)
(c.update({g -> 0.5}), c.update({alpha -> 0.5}), c.update({l -> 0.8}).hex)
// ➔ (Rgb(1, 0.5, 0), Rgb(1, 0, 0, 0.5), "#ff9989")
Keys of the two models in one call are an expected-value error, and a key
that is not a channel is an unknown-field error.
Comparing two colors
a == b is true when the two values are the same color, whatever spaces
they are written in: each sRGB channel and the alpha differ by at most
0.000001. That is much less than one step of an 8-bit channel (1/255), so two
different hex colors are never equal.
(rgb(1, 0, 0) == hsl(0, 1, 0.5), rgb(1, 0, 0) == color("#ff0000"), rgb(1, 0, 0) != rgb(0, 1, 0))
// ➔ (true, true, true)
c.matches(d, tolerance) is the same test with a tolerance that you choose.
A color with no alpha has the alpha 1.
let c = rgb(1, 0, 0)
(c.matches(rgb(0.96, 0, 0), 0.05), c.matches(rgb(0.9, 0, 0), 0.05), c.matches(rgb(1, 0, 0, 0.5), 0.05))
// ➔ (true, false, false)
matches compares numbers. For a perceptual difference, use colorDelta,
the distance in OKLab: colorDelta(a, b) <= 0.02 is true for two colors that
look almost alike.
matches and update are members of a color: you call them with a dot. They
are also the library functions colorMatches(c, d, tolerance) and
colorUpdate(c, {…}). They work on a color also when your program declares
a protocol function named matches or update for its own types: in a
function whose parameter has no type, c => c.update({alpha -> 0.5}) reaches
the member of a color for a color, and the protocol function for a value of
one of your types.
Definitions
Each definition is listed under its Epsil spelling (the MathJSON name when it has none), with its signature in the engine's type syntax. The Standard Library page is the one-page index of every category.
asHsl
(broadcastable<color | string | tuple>) -> color
- argument 1 — The color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]; a list of colors gives a list. Sample:
rgb(1, 0.5, 0).
Convert any color to HSL (hue degrees, s/l 0-1)
asHsl(rgb(1, 0.5, 0))
// ➔ Hsl(30, 1, 0.5)
MathJSON: AsHsl • LaTeX: \operatorname{asHsl}
asHsv
(broadcastable<color | string | tuple>) -> color
- argument 1 — The color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]; a list of colors gives a list. Sample:
rgb(1, 0.5, 0).
Convert any color to HSV (hue degrees, s/v 0-1)
asHsv(rgb(1, 0.5, 0))
// ➔ Hsv(30, 1, 1)
MathJSON: AsHsv • LaTeX: \operatorname{asHsv}
asOklab
(broadcastable<color | string | tuple>) -> color
- argument 1 — The color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]; a list of colors gives a list. Sample:
oklch(0.7, 0.15, 30).
Convert any color to OKLab
asOklab(oklch(0.7, 0.15, 30))
// ➔ Oklab(0.7, 0.1299038105676658, 0.07499999999999998)
MathJSON: AsOklab • LaTeX: \operatorname{asOklab}
asOklch
(broadcastable<color | string | tuple>) -> color
- argument 1 — The color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]; a list of colors gives a list. Sample:
oklab(0.7, 0.1, 0.1).
Convert any color to OKLCh
asOklch(oklab(0.7, 0.1, 0.1))
// ➔ Oklch(0.7, 0.14142135623730953, 45)
asOklch(rgb(1, 0, 0))
// ➔ Oklch(0.6279553606145515, 0.25768330773615683, 29.2338851923426)
MathJSON: AsOklch • LaTeX: \operatorname{asOklch}
asRgb
(broadcastable<color | string | tuple>) -> color
- argument 1 — The color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]; a list of colors gives a list. Sample:
hsl(120, 1, 0.5).
Convert any color to sRGB (channels 0-1)
asRgb(hsl(120, 1, 0.5))
// ➔ Rgb(0, 1, 0)
asRgb("#ff8000")
// ➔ Rgb(1, 0.5019607843137255, 0)
MathJSON: AsRgb • LaTeX: \operatorname{asRgb}
color
(string) -> color
- argument 1 — A CSS color: a hex code, an
rgb(),hsl(),oklch()oroklab()spelling, or a color name; a string that is not a color is anincompatible-typeerror. Sample:"#ff8000".
Parse a CSS-style color string to a color in the color space of the string: an Rgb color for a hex code, an rgb() spelling or a color name, an Hsl color for an hsl() spelling, and an Oklch or Oklab color for an oklch() or oklab() spelling
color("#ff8000")
// ➔ Rgb(1, 0.5019607843137255, 0)
color("hsl(120 100% 50%)")
// ➔ Hsl(120, 1, 0.5)
The result is a color in the space of the string: an Rgb color for a hex
code, an rgb() spelling, a name or a shade; an Hsl color for an hsl()
spelling; an Oklch or Oklab color for an oklch() or oklab()
spelling.
color("#f00")
// ➔ Rgb(1, 0, 0)
[color("#f00").r, color("#f00").hex]
// ➔ [1,"#ff0000"]
color("hsl(0, 100%, 50%)")
// ➔ Hsl(0, 1, 0.5)
The string is one of:
- a hex color:
#rgb,#rgba,#rrggbbor#rrggbbaa("#f00","#ff0000","#ff000080"); - an RGB color:
rgb(r, g, b)orrgba(r, g, b, a)("rgba(255, 0, 0, 0.5)"); - an HSL color:
hsl(h, s%, l%)orhsla(h, s%, l%, a)("hsl(0, 100%, 50%)"); - an OKLCh color:
oklch(l% c h)oroklch(l% c h / a)("oklch(50% 0.3 240)"); - an OKLab color:
oklab(l% a b)oroklab(l% a b / a)("oklab(50% 0.1 -0.2)"); - a color name: a name of the palette or a CSS name (see below), or
"transparent"; - a shade of a color scale, such as
"red-500"(see below).
A name or a shade can end with / and an opacity in percent: "red/50" is
red with the alpha 0.5, read as 128/255.
color("red-500/50").hex
// ➔ "#f9110080"
A hex, RGB or HSL string is read as an sRGB color with 8 bits for each
channel, so an alpha of 0.5 is read as 128/255, about 0.502:
color("rgba(255, 0, 0, 0.5)")
// ➔ Rgb(1, 0, 0, 0.5019607843137255)
An oklch(…) or oklab(…) string keeps its channels, also a chroma or a
hue that is outside the sRGB gamut. It is the same color as a call of
oklch:
[color("oklch(50% 0.3 240)"), oklch(0.5, 0.3, 240)]
// ➔ [Oklch(0.5, 0.3, 240),Oklch(0.5, 0.3, 240)]
color("oklch(50% 0.3 240 / 0.5)")
// ➔ Oklch(0.5, 0.3, 240, 0.5)
A string that is not a color is an incompatible-type error:
color("nosuchcolor")
// ➔ error: Runtime error: expected `color`, got `"nosuchcolor"`
Named colors
The names of the palette are not the colors of CSS: red is #d7170b, not
#ff0000. Case is ignored.
[color("red").hex, color("midnight").hex]
// ➔ ["#d7170b","#2c4670"]
| Name | Swatch | Name | Swatch |
|---|---|---|---|
red | orange | ||
yellow | lime | ||
green | teal | ||
cyan | blue | ||
indigo | purple | ||
magenta | brown | ||
olive | midnight | ||
sky | black | ||
white | carbon | ||
charcoal | slate | ||
dark-grey | graphite | ||
stone | grey | ||
light-grey | ash | ||
mist | snow |
CSS color names
Every CSS color name is a name. A CSS name that is also a name of the
palette (red, blue, white, …) is the color of the palette. Each other
CSS name is a palette name, a shade or a hex color, given in the table, so
its color can differ from the CSS one: navy is blue-900, #0652ab, not
#000080.
[color("navy").hex, color("aliceblue").hex, color("silver").hex]
// ➔ ["#0652ab","#e3fbff","#c0c0c0"]
| CSS name | Color | CSS name | Color | ||
|---|---|---|---|---|---|
aliceblue | blue-25 | lightgrey | light-grey | ||
antiquewhite | brown-50 | lightpink | pink-100 | ||
aqua | cyan | lightsalmon | red-200 | ||
aquamarine | teal-200 | lightseagreen | teal-400 | ||
azure | cyan-25 | lightskyblue | blue-200 | ||
beige | lime-50 | lightslategray | stone | ||
bisque | brown-100 | lightslategrey | stone | ||
blanchedalmond | orange-50 | lightsteelblue | sky | ||
blueviolet | indigo-700 | lightyellow | yellow-25 | ||
burlywood | brown-200 | limegreen | green-400 | ||
cadetblue | cyan-500 | linen | brown-25 | ||
chartreuse | lime-400 | maroon | red-900 | ||
chocolate | orange-700 | mediumaquamarine | teal-300 | ||
coral | red-300 | mediumblue | blue-800 | ||
cornflowerblue | blue-500 | mediumorchid | purple-700 | ||
cornsilk | orange-25 | mediumpurple | indigo-500 | ||
crimson | red-700 | mediumseagreen | green-500 | ||
darkblue | blue-900 | mediumslateblue | indigo-700 | ||
darkcyan | teal-700 | mediumspringgreen | green-300 | ||
darkgoldenrod | brown-400 | mediumturquoise | teal-300 | ||
darkgray | dark-grey | mediumvioletred | pink-800 | ||
darkgreen | green-900 | midnightblue | midnight | ||
darkgrey | dark-grey | mintcream | teal-25 | ||
darkkhaki | olive | mistyrose | red-50 | ||
darkmagenta | purple-900 | moccasin | orange-100 | ||
darkolivegreen | lime-900 | navajowhite | orange-100 | ||
darkorange | orange | navy | blue-900 | ||
darkorchid | purple-800 | oldlace | brown-25 | ||
darkred | red-900 | olivedrab | lime-700 | ||
darksalmon | red-300 | orangered | red-400 | ||
darkseagreen | green-300 | orchid | purple-400 | ||
darkslateblue | midnight | palegoldenrod | orange-100 | ||
darkslategray | charcoal | palegreen | green-100 | ||
darkslategrey | charcoal | paleturquoise | teal-100 | ||
darkturquoise | teal-500 | palevioletred | pink-600 | ||
darkviolet | purple | papayawhip | orange-50 | ||
deeppink | pink-500 | peachpuff | red-100 | ||
deepskyblue | blue-400 | peru | brown-400 | ||
dimgray | slate | pink | pink-200 | ||
dimgrey | slate | plum | purple-200 | ||
dodgerblue | blue-600 | powderblue | cyan-100 | ||
firebrick | red-800 | rebeccapurple | #663399 | ||
floralwhite | orange-25 | rosybrown | brown-300 | ||
forestgreen | green-700 | royalblue | blue-700 | ||
fuchsia | magenta | saddlebrown | brown-700 | ||
gainsboro | ash | salmon | red-300 | ||
ghostwhite | indigo-25 | sandybrown | orange-300 | ||
gold | yellow-700 | seagreen | green-800 | ||
goldenrod | yellow-900 | seashell | red-25 | ||
gray | grey | sienna | brown-600 | ||
greenyellow | lime-300 | silver | #c0c0c0 | ||
honeydew | green-25 | skyblue | blue-300 | ||
hotpink | pink-400 | slateblue | indigo | ||
indianred | pink-700 | slategray | graphite | ||
ivory | orange-25 | slategrey | graphite | ||
khaki | yellow-200 | springgreen | green-300 | ||
lavender | indigo-100 | steelblue | blue-800 | ||
lavenderblush | pink-25 | tan | brown-200 | ||
lawngreen | lime-400 | thistle | purple-100 | ||
lemonchiffon | yellow-50 | tomato | red-400 | ||
lightblue | cyan-200 | turquoise | teal | ||
lightcoral | pink-300 | violet | purple-300 | ||
lightcyan | cyan-25 | wheat | brown-100 | ||
lightgoldenrodyellow | yellow-50 | whitesmoke | mist | ||
lightgray | light-grey | yellowgreen | lime-300 | ||
lightgreen | green-200 |
Color scales
A shade of a scale is the name of the scale, - and a number from 0 to
1000: 0 is the lightest shade, 500 the middle one, 1000 the darkest. A
number over 1000 is read as 1000. The table shows the usual steps; any
number between them is a shade too.
[color("blue-50").hex, color("blue-500").hex, color("blue-950").hex]
// ➔ ["#dffafe","#3b9bff","#014ca5"]
A shade is not always the color of the same name: red is #d7170b, about
red-700, and red-500 is #f91100. A name with no scale has no shades:
color("grey-500")
// ➔ error: Runtime error: expected `color`, got `"grey-500"`
| Scale | 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 950 |
|---|---|---|---|---|---|---|---|---|---|---|---|
pink | |||||||||||
red | |||||||||||
brown | |||||||||||
orange | |||||||||||
yellow | |||||||||||
lime | |||||||||||
green | |||||||||||
teal | |||||||||||
cyan | |||||||||||
blue | |||||||||||
indigo | |||||||||||
purple |
MathJSON: Color
["Color", string]. The result is an Rgb, Hsl, Oklch or Oklab
expression, in the space of the string.
["Color", "'#f00'"]
// ➔ ["Rgb", 1, 0, 0]
["Color", "'rgba(255, 0, 0, 0.5)'"]
// ➔ ["Rgb", 1, 0, 0, 0.502]
["Color", "'red'"]
// ➔ ["Rgb", 0.843, 0.09, 0.043]
["Color", "'hsl(0, 100%, 50%)'"]
// ➔ ["Hsl", 0, 1, 0.5]
colorContrast
MathJSON ColorContrast · (background: color | string | tuple, foreground: color | string | tuple) -> number
background— The background color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]; APCA is not symmetric, so dark text on a light background gives a positive contrast and light text on a dark one a negative contrast. Sample:"#ffffff".foreground— The text color, in the same spellings. Sample:"#000000".
APCA contrast ratio between two colors
colorContrast("#ffffff", "#000000")
// ➔ 1.0604067321268862
colorContrast("#000000", "#ffffff")
// ➔ -1.0788473318309848
colorDelta
(color | string | tuple, color | string | tuple) -> number
- argument 1 — The first color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]. Sample:
rgb(1, 0, 0). - argument 2 — The second color; the result is 0 for the same color and about 1 between white and black. Sample:
rgb(0.9, 0, 0).
Perceptual color difference (ΔE_OK) between two colors
colorDelta(rgb(1, 0, 0), rgb(0.9, 0, 0))
// ➔ 0.05197810440998347
colorDelta("#ffffff", "#000000")
// ➔ 0.9999999934735466
MathJSON: ColorDelta • LaTeX: \operatorname{colorDelta}
colorFromColorspace
(channels: color | tuple, space: string) -> color
channels— Three channel values in the order of the space, and an optional alpha in [0, 1]; the channels of a color head are read as they are, ignoring its own space. Sample:(120, 1, 0.5).space— The color space of the channels: "rgb", "hsl", "hsv", "oklab" (or "lab") or "oklch", in any case; another name is anexpected-valueerror. Sample:"hsl".
Build a color from channel values in a named color space. The result is a color on every route — the color head of the named space when evaluated, the target's color value when compiled. To read the channels back out, call ColorToColorspace(color, space)
colorFromColorspace((120, 1, 0.5), "hsl")
// ➔ Hsl(120, 1, 0.5)
colorFromColorspace((0.7, 0.15, 30, 0.5), "oklch")
// ➔ Oklch(0.7, 0.15, 30, 0.5)
The result is the color of the named space: rgb, hsl, hsv, oklch or
oklab, with an alpha when the tuple has a fourth component. The color
space names are those of colorToColorspace.
colorFromColorspace((0, 1, 0.5), "hsl")
// ➔ Hsl(0, 1, 0.5)
[asRgb(colorFromColorspace((0, 1, 0.5), "hsl")), colorFromColorspace((1, 0, 0, 0.5), "rgb")]
// ➔ [Rgb(1, 0, 0),Rgb(1, 0, 0, 0.5)]
MathJSON: ColorFromColorspace
["ColorFromColorspace", components, space]. The result is an Rgb,
Hsl, Hsv, Oklch or Oklab expression.
["ColorFromColorspace", ["Tuple", 0, 1, 0.5], "'hsl'"]
// ➔ ["Hsl", 0, 1, 0.5]
["AsRgb", ["ColorFromColorspace", ["Tuple", 0, 1, 0.5], "'hsl'"]]
// ➔ ["Rgb", 1, 0, 0]
colorMatches
MathJSON ColorMatches · (color: color, other: color, tolerance: number) -> boolean
color— The first color. Sample:rgb(1, 0, 0).other— The second color. Sample:hsl(0, 1, 0.5).tolerance— The largest difference of an sRGB channel (on the scale of 0 to 1) or of the alpha between the two colors; one step of an 8-bit channel is 1/255. Sample:0.01.
Whether two colors are the same within a tolerance: True when each sRGB channel and the alpha of the two colors differ by at most the tolerance, whatever color space each color is written in. A color with no alpha has the alpha 1. A color with a NaN channel matches no color. a == b is this test with a tolerance of 1e-6. For a perceptual distance, use colorDelta
colorMatches(rgb(1, 0, 0), hsl(0, 1, 0.5), 0.01)
// ➔ true
colorMatches(rgb(1, 0, 0), rgb(0.9, 0, 0), 0.05)
// ➔ false
colorMix
MathJSON ColorMix · (a: color | string | tuple, b: color | string | tuple, ratio: number?) -> color
a— The first color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]. Sample:"#ff0000".b— The second color, in the same spellings. Sample:"#0000ff".ratio— The weight of the second color, from 0 (the first color) to 1 (the second color), clamped into that range; 0.5 when omitted. Sample:0.25.
Mix two colors in OKLCh space
colorMix("#ff0000", "#0000ff")
// ➔ Oklch(0.5399845394999472, 0.28544883970037904, 326.6429529151988)
colorMix("#ff0000", "#0000ff", 0.25)
// ➔ Oklch(0.5839699500572493, 0.2715660737182679, 357.9384190537707)
colorToColorspace
(color: color | string | tuple, space: string) -> tuple
color— The color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]. Sample:rgb(1, 0.5, 0).space— The target color space: "rgb", "hsl", "hsv", "oklab" (or "lab") or "oklch", in any case; another name is anexpected-valueerror. Sample:"hsl".
Convert a color to components in a target color space
colorToColorspace(rgb(1, 0.5, 0), "hsl")
// ➔ (30, 1, 0.5)
colorToColorspace(oklab(0.7, 0.1, 0.1), "oklch")
// ➔ (0.7, 0.14142135623730953, 45)
The color space is "rgb", "hsl", "hsv", "oklch" or "oklab"
("lab" is the same as "oklab"); case is ignored. The color is a color
value, a color string or a tuple of sRGB channels. The result is a tuple,
with a fourth component for an alpha other than 1.
[colorToColorspace("#f00", "hsl"), colorToColorspace("#f00", "hsv")]
// ➔ [(0, 1, 0.5),(0, 1, 1)]
colorToColorspace("#f00", "oklab")
// ➔ (0.6279553606145515, 0.22486306106597417, 0.12584629853073503)
An sRGB channel outside [0, 1] is extended sRGB: it describes a color
outside the sRGB gamut, which a Display-P3 display can show. The
conversions among rgb, oklab and oklch keep it:
let c = colorToColorspace(rgb(1.2, 0, 0), "oklch")
asRgb(colorFromColorspace(c, "oklch"))
// ➔ Rgb(1.2000000000000004, 0, 0)
HSV and HSL describe only the sRGB gamut. Their saturation, value and
lightness are clamped to [0, 1], and an infinite one reads as its bound. An
extended sRGB color is first mapped into the sRGB gamut, as
gamutMap maps it: the OKLCh chroma is reduced at constant
lightness and hue. The channels are not clipped one by one, so the HSL hue
of rgb(1.2, 0, 0) is not 0:
colorToColorspace(rgb(1.2, 0, 0), "hsl")
// ➔ (6.455435134724467, 1, 0.6695605539773027)
colorToColorspace(hsl(0, oo, 0.5), "rgb")
// ➔ (1, 0, 0)
A NaN channel, an infinite hue, and an infinite sRGB, OKLab or OKLCh
channel are an incompatible-type error. An unknown color space is an
expected-value error.
MathJSON: ColorToColorspace
["ColorToColorspace", color, space]. The result is a Tuple.
["ColorToColorspace", "'#f00'", "'hsl'"]
// ➔ ["Tuple", 0, 1, 0.5]
colorToString
MathJSON ColorToString · (color: color | string | tuple, format: string?) -> string
color— The color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]. Sample:rgb(1, 0.5, 0).format— The output format, "hex" when omitted: "hex", "rgb", "hsl", "oklch", "srgb" or "display-p3", in any case; another name is anexpected-valueerror. Sample:"hsl".
Convert a color to a string in the specified format: "hex" (the default), "rgb", "hsl", "oklch", "srgb" (the same as "hex") or "display-p3" (the CSS spelling color(display-p3 r g b)). The hex, rgb, hsl and srgb formats map the color into the sRGB gamut, and display-p3 maps it into the Display-P3 gamut, with the CSS Color 4 gamut mapping: the OKLCh chroma is reduced at constant lightness and hue. The channels are not clipped one by one. The oklch format has no gamut and is not mapped
colorToString(rgb(1, 0.5, 0))
// ➔ "#ff8000"
colorToString(oklch(0.7, 0.15, 30), "oklch")
// ➔ "oklch(0.7 0.15 30)"
colorUpdate
MathJSON ColorUpdate · (color: color, channels: dictionary) -> color
color— The color to copy. Sample:rgb(1, 0, 0).channels— The channels to replace and their values:{l -> 0.8},{r -> 0, b -> 1},{alpha -> 0.5}. Sample:{l -> 0.8}.
A copy of a color with some channels replaced. The keys of the dictionary are of one model: r, g, b (sRGB, the result is an Rgb color) or l, c, h (OKLCh, the result is an Oklch color); alpha goes with either, or alone, and then the result is in the color space of the color. Keys of the two models together are an expected-value error, a key that is not a channel is an unknown-field error, and a value that is not a number is an incompatible-type error
colorUpdate(rgb(1, 0, 0), {l -> 0.8})
// ➔ Oklch(0.8, 0.25768330773615683, 29.2338851923426)
colorUpdate(rgb(1, 0, 0), {alpha -> 0.5})
// ➔ Rgb(1, 0, 0, 0.5)
colormap
(string, number?) -> color | list<color>
- argument 1 — The name of the palette, such as "viridis", "tableau10" or "tycho11"; an unknown name is an
expected-valueerror. Sample:"viridis". - argument 2 — An integer n from 2 to 10000 gives a list of n colors (a larger n stays unevaluated); any other number is a position in [0, 1], clamped, and gives one color (1 is the last color, not a count). Sample:
0.5.
Sample colors from a named palette. With no second argument, the colors of the palette. With an integer n >= 2, a list of n colors. With a number t in [0, 1] (clamped), the color at position t. A sequential or diverging palette is a gradient: n gives n evenly spaced samples, and t an interpolated color. A categorical palette (such as "tycho11" or "tableau10") is a list of distinct colors and is never interpolated: n gives its first n colors, repeated from the start past the length of the palette, and t the nearest color, the color at index round(t × (length − 1))
colormap("viridis", 0.5)
// ➔ Oklch(0.5953489984141304, 0.09460305103214128, 191.23555350205825)
colormap("tableau10", 3)
// ➔ [Oklch(0.5492406836799802, 0.12380974317992, 244.60325596315428),Oklch(0.7306522245131786, 0.18510803457288688, 51.88404768505323),Oklch(0.6192883551551218, 0.18397372537450857, 142.94995785173705)]
- With only a name, the result is the list of the colors of the palette.
- With an integer
nof 2 or more, it is a list ofncolors. For a sequential or a diverging palette, they are at evenly spaced positions, from the first color of the palette to the last. For a categorical palette, they are its firstncolors; whennis larger than the palette, its colors repeat from the start. - With a number
tbetween 0 and 1, it is the color at positiont. A position below 0 is read as 0, and one above 1 as 1.
In a sequential or a diverging palette, a position between two colors of
the palette gives a color interpolated in OKLCh. A categorical palette is
not interpolated: a position gives the nearest color of the palette, the
one at index round(t × (n − 1)) (counting from 0) in a palette of n
colors.
map(colorToString, colormap("viridis", 3))
// ➔ ["#440154","#21918d","#fde725"]
colorToString(colormap("viridis", 0.5))
// ➔ "#21918d"
map(colorToString, colormap("tycho11", 3))
// ➔ ["#4e79a7","#f28e2b","#59a14f"]
[colorToString(colormap("tycho11", 0.05)), colorToString(colormap("tycho11", 0.5))]
// ➔ ["#f28e2b","#9c755f"]
count(colormap("tycho11"))
// ➔ 11
colorToString(colormap("tycho11", 12)[12])
// ➔ "#4e79a7"
An unknown palette name is an expected-value error. A request for more
than 10,000 colors stays unevaluated.
Available palettes
Sequential
These palettes are perceptually uniform, for ordered data. Each one also has
a -reversed variant (viridis-reversed).
| Name | |
|---|---|
viridis | |
inferno | |
magma | |
plasma | |
turbo | |
cividis | |
rocket | |
mako | |
grey | |
cmocean-phase |
Diverging
These palettes are symmetric around a neutral midpoint. Each one also has a
-reversed variant (roma-reversed).
| Name | |
|---|---|
roma | |
rdbu | |
coolwarm | |
vik | |
broc |
Categorical
These palettes are for discrete categories with no order. They have no
-reversed variant, and colormap does not interpolate them.
| 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, for filled areas and dashboards. Less suitable for thin lines. |
tycho-soft-dark11 | Soft-contrast palette for charcoal backgrounds, for both lines and filled surfaces. |
tycho-bold11 | High-contrast palette with strong separation, for thin lines and dense line charts. |
tycho-bold-dark11 | High-contrast palette for dark backgrounds, for thin strokes. |
tableau10 | The Tableau 10 palette, also the default of Matplotlib. |
mathematica10 | Palette inspired by the default colors of Wolfram Mathematica. |
cupertino10 | Apple system palette for light appearance (11 colors). |
cupertino-dark10 | Apple system palette for dark appearance (11 colors). |
kelly22 | 22 colors of maximum contrast, for data with many categories. |
spectrum12 | 12 colors across the visible spectrum. |
MathJSON: Colormap
["Colormap", name], ["Colormap", name, n], ["Colormap", name, t].
The name is a string.
["Colormap", "'viridis'", 5]
["Colormap", "'viridis'", 0.5]
contrastingColor
MathJSON ContrastingColor · (background: color | string | tuple, a: (color | string | tuple)?, b: (color | string | tuple)?) -> color
background— The background color: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]. Sample:"#000080".a— The first candidate foreground color; it is used only withb, and without both candidates the result is white or black. Sample:"#ffd700".b— The second candidate foreground color; on equal contrast the first candidate wins. Sample:"#800080".
Choose the foreground color with better APCA contrast against a background, answered as given: the interpreter keeps the color head the candidate was written with, and a compiled target answers the same color in its canonical form
contrastingColor("#000080")
// ➔ Rgb(1, 1, 1)
contrastingColor("#000080", "#ffd700", "#800080")
// ➔ Rgb(1, 0.8431372549019608, 0)
gamutMap
MathJSON GamutMap · (color: color | string | tuple, gamut: string?) -> color
color— The color to map: a color head, a CSS color string or a tuple of sRGB channels in [0, 1]. Sample:oklch(0.7, 0.4, 30).gamut— The target gamut, "srgb" when omitted, or "display-p3", in any case; another name is anexpected-valueerror. Sample:"display-p3".
Map a color into a target gamut, "srgb" (the default) or "display-p3", with the CSS Color 4 gamut-mapping algorithm: the OKLCh chroma is reduced, at constant lightness and hue, until the color is inside the gamut or until clipping each channel changes the color by less than a just noticeable difference (ΔE_OK 0.02). A lightness of 1 or more gives white, and 0 or less gives black. A color already inside the gamut is returned unchanged. The result is an Rgb color, in sRGB coordinates also for "display-p3": its channels are in [0, 1] for "srgb", and for "display-p3" they can be outside [0, 1] (extended sRGB) for a color that is inside the Display-P3 gamut but outside the sRGB gamut. Color values themselves have no gamut: only this operator and the string output map a color
gamutMap(oklch(0.7, 0.4, 30))
// ➔ Rgb(1, 0.3451350864103807, 0.26457522622474827)
gamutMap(oklch(0.7, 0.4, 30), "display-p3")
// ➔ Rgb(1.0872362010596204, 0.17872507792896167, 0.09415278671783497)
hsl
(h: broadcastable<number>, s: broadcastable<number>, l: broadcastable<number>, alpha: broadcastable<number>?) -> color
h— The hue, in degrees, read modulo 360. Sample:120.s— The saturation, from 0 to 1; a value outside is clamped. Sample:1.l— The lightness, from 0 (black) to 1 (white); a value outside is clamped. Sample:0.5.alpha— The opacity, from 0 (transparent) to 1 (opaque, the default). Sample:0.5.
HSL color (hue degrees, saturation/lightness 0-1, optional alpha)
asRgb(hsl(120, 1, 0.5))
// ➔ Rgb(0, 1, 0)
MathJSON: Hsl • LaTeX: \operatorname{hsl}
hsv
(h: broadcastable<number>, s: broadcastable<number>, v: broadcastable<number>, alpha: broadcastable<number>?) -> color
h— The hue, in degrees, read modulo 360. Sample:30.s— The saturation, from 0 to 1; a value outside is clamped. Sample:1.v— The value (brightness), from 0 to 1; a value outside is clamped. Sample:1.alpha— The opacity, from 0 (transparent) to 1 (opaque, the default). Sample:0.5.
HSV color (hue degrees, saturation/value 0-1, optional alpha)
asRgb(hsv(30, 1, 1))
// ➔ Rgb(1, 0.5, 0)
MathJSON: Hsv • LaTeX: \operatorname{hsv}
oklab
(l: broadcastable<number>, a: broadcastable<number>, b: broadcastable<number>, alpha: broadcastable<number>?) -> color
l— The perceptual lightness, from 0 (black) to 1 (white). Sample:0.7.a— The green-red axis, about -0.4 (green) to 0.4 (red). Sample:0.1.b— The blue-yellow axis, about -0.4 (blue) to 0.4 (yellow). Sample:0.1.alpha— The opacity, from 0 (transparent) to 1 (opaque, the default). Sample:0.5.
OKLab color (L 0-1, a/b ~ -0.4..0.4, optional alpha)
asOklch(oklab(0.7, 0.1, 0.1))
// ➔ Oklch(0.7, 0.14142135623730953, 45)
MathJSON: Oklab • LaTeX: \operatorname{oklab}
oklch
(l: broadcastable<number>, c: broadcastable<number>, h: broadcastable<number>, alpha: broadcastable<number>?) -> color
l— The perceptual lightness, from 0 (black) to 1 (white). Sample:0.7.c— The chroma, from 0 (gray) to about 0.4. Sample:0.15.h— The hue angle, in degrees. Sample:30.alpha— The opacity, from 0 (transparent) to 1 (opaque, the default). Sample:0.5.
OKLCh color (L 0-1, C 0-~0.4, hue degrees, optional alpha)
colorToString(oklch(0.7, 0.15, 30))
// ➔ "#ed7665"
MathJSON: Oklch • LaTeX: \operatorname{oklch}
rgb
(r: broadcastable<number>, g: broadcastable<number>, b: broadcastable<number>, alpha: broadcastable<number>?) -> color
r— The red channel, from 0 to 1; a value outside [0, 1] is extended sRGB and is kept. Sample:1.g— The green channel, from 0 to 1. Sample:0.5.b— The blue channel, from 0 to 1. Sample:0.alpha— The opacity, from 0 (transparent) to 1 (opaque, the default). Sample:0.5.
sRGB color (channels 0-1, optional alpha 0-1)
rgb(1, 0.5, 0)
// ➔ Rgb(1, 0.5, 0)
colorToString(rgb(1, 0.5, 0, 0.5))
// ➔ "#ff800080"
MathJSON: Rgb • LaTeX: \operatorname{rgb}
rgb255
MathJSON Rgb255 · (r: broadcastable<number>, g: broadcastable<number>, b: broadcastable<number>, alpha: broadcastable<number>?) -> color
r— The red channel, from 0 to 255; a value outside [0, 255] is extended sRGB and is kept. Sample:255.g— The green channel, from 0 to 255. Sample:128.b— The blue channel, from 0 to 255. Sample:0.alpha— The opacity, from 0 (transparent) to 1 (opaque, the default): it is not on the scale of 0 to 255. Sample:0.5.
sRGB color with channels on the scale of 0 to 255 (optional alpha 0-1): rgb255(r, g, b, alpha) is rgb(r/255, g/255, b/255, alpha)
rgb255(255, 128, 0)
// ➔ Rgb(1, 128/255, 0)
colorToString(rgb255(255, 128, 0, 0.5))
// ➔ "#ff800080"