Skip to main content

Module color

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 l in [0, 1], chroma c (0 is grey; sRGB tops out near 0.37) and hue h in 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), or None for an image with no hue to speak of.
hsl_to_rgb
HSL → opaque sRGB. h in degrees (any range; wrapped), s and l in [0, 1].
hsv_to_rgb
HSV → opaque sRGB. h in degrees (any range; wrapped), s and v clamped to [0, 1].
oklch
Shorthand for Oklch::to_color.
parse_hex
Parse #rgb, #rrggbb or #rrggbbaa (leading # optional, surrounding whitespace ignored, case-insensitive). The short form doubles each digit, as CSS does. None for anything else, including a sign or a non-ASCII character that u8::from_str_radix alone 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 are 0 on the achromatic axis.
rgb_to_hsv
Inverse of hsv_to_rgb: (hue_degrees, s, v). Hue is 0 on 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.