Module color
Expand description
Colour conversions shared by the toolkit and apps: #hex parsing and
formatting, HSV and HSL, and WCAG relative luminance and contrast.
Everything takes and returns Color with 8-bit sRGB channels, the
precision every picker, palette and persisted colour in the platform uses.
Hues are in degrees, [0, 360); saturation, value and lightness in
[0, 1].
Structs§
- Oklch
- A colour in OKLCH: perceptual lightness
lin[0, 1], chromac(0 is grey; sRGB tops out near 0.37) and huehin degrees.
Functions§
- contrast_
ratio - WCAG 2.1 contrast ratio between two colours, in
[1.0, 21.0](1.0 = same luminance, 21.0 = black on white). WCAG AA body text wants ≥ 4.5:1; AAA wants ≥ 7:1. - dominant_
hue - The dominant OKLCH hue of an RGBA8 image (
pixels.len()a multiple of four), orNonefor an image with no hue to speak of. - hsl_
to_ rgb - HSL → opaque sRGB.
hin degrees (any range; wrapped),sandlin[0, 1]. - hsv_
to_ rgb - HSV → opaque sRGB.
hin degrees (any range; wrapped),sandvclamped to[0, 1]. - oklch
- Shorthand for
Oklch::to_color. - parse_
hex - Parse
#rgb,#rrggbbor#rrggbbaa(leading#optional, surrounding whitespace ignored, case-insensitive). The short form doubles each digit, as CSS does.Nonefor anything else, including a sign or a non-ASCII character thatu8::from_str_radixalone would let through or trip over. - relative_
luminance - WCAG 2.1 relative luminance of an sRGB colour, in
[0.0, 1.0]. Alpha is ignored (colours are assumed opaque for contrast purposes). - rgb_
to_ hsl - sRGB → HSL:
(hue_degrees, s, l). Straight sRGB rather than a perceptual space; hue and saturation are0on the achromatic axis. - rgb_
to_ hsv - Inverse of
hsv_to_rgb:(hue_degrees, s, v). Hue is0on the achromatic axis — a caller editing a grey should keep its prior hue rather than rely on this fallback. - to_hex
#RRGGBB(upper-case, alpha dropped) for a colour.- to_rgb8
- The 8-bit
[r, g, b]channels of a colour, alpha dropped — the triple apps persist colours as.