Malachi. UI. TokenGen. Color
(malachi v0.22.7)
View Source
An authored OKLCH color and the conversions the token generator needs from it.
Tokens are authored in OKLCH, and the web stylesheet and Malachi.UI.Tokens carry them as written,
since both are CSS a browser draws. The terminal palette, the contrast and color vision checks and
the snapshot's srgb8 receive sRGB instead. This module is that conversion,
OKLCH to OKLab to linear sRGB, a clip into the unit cube in linear light, and the sRGB transfer
function to bytes, with Björn Ottosson's published matrices.
The clip is deliberately the simple one. A color sRGB cannot show is moved channel by channel to
the nearest face of the cube, and clip_delta/1 reports how far in OKLab that moved it, so the
generator can refuse an authored color that sRGB cannot approximate ($gamut in the token file)
instead of hiding the error inside a gamut mapping search.
Summary
Types
Linear light sRGB, nominally 0 to 1 per channel and outside it when out of gamut.
A point in OKLab: lightness, a, b.
An sRGB color as the bytes a screen shows.
An authored OKLCH color: lightness 0 to 1, chroma 0 to 0.4, hue in degrees, alpha 0 to 1.
Functions
Clamps every channel of a linear sRGB color into 0..1.
How far, in OKLab, the clip into sRGB moves this color. Zero for a color inside the gamut.
The inverse sRGB transfer function, encoded value to linear light.
The Euclidean distance between two OKLab points.
The sRGB transfer function, linear light to the encoded value.
Linear sRGB to OKLab.
OKLab to linear sRGB, unclipped.
An OKLab point as sRGB bytes, after the clip.
Parses an authored oklch(L C H) or oklch(L C H / A) string.
sRGB bytes in linear light.
sRGB bytes in OKLab.
The color in OKLab.
The color as the sRGB bytes a screen shows, after the clip.
Types
Linear light sRGB, nominally 0 to 1 per channel and outside it when out of gamut.
A point in OKLab: lightness, a, b.
@type srgb8() :: {0..255, 0..255, 0..255}
An sRGB color as the bytes a screen shows.
An authored OKLCH color: lightness 0 to 1, chroma 0 to 0.4, hue in degrees, alpha 0 to 1.
Functions
Clamps every channel of a linear sRGB color into 0..1.
How far, in OKLab, the clip into sRGB moves this color. Zero for a color inside the gamut.
The inverse sRGB transfer function, encoded value to linear light.
The Euclidean distance between two OKLab points.
The sRGB transfer function, linear light to the encoded value.
Linear sRGB to OKLab.
OKLab to linear sRGB, unclipped.
An OKLab point as sRGB bytes, after the clip.
Parses an authored oklch(L C H) or oklch(L C H / A) string.
Lightness must lie in 0..1, chroma in 0..0.4, hue in 0 up to but excluding 360, and alpha in 0..1. Percentages, commas, missing components and any other color syntax are refused.
sRGB bytes in linear light.
sRGB bytes in OKLab.
The color in OKLab.
The color as the sRGB bytes a screen shows, after the clip.