Skip to main content

Crate prime_color

Crate prime_color 

Source
Expand description

prime-color — Color space math: sRGB, Oklab, HSL, and perceptual mixing.

All functions are pure (LOAD + COMPUTE only). No mutation, no side effects, no hidden state. Same inputs always produce the same output.

Colors are represented as bare (f32, f32, f32) tuples for simplicity and FFI/WASM interop. No structs.

§Color spaces

  • sRGB — standard display color space, gamma-encoded, components in [0, 1]
  • Linear RGB — gamma-decoded sRGB; linear light values used for math
  • Oklab — perceptually uniform opponent color space by Björn Ottosson
    • L[0, 1] (lightness), a[-0.5, 0.5] (green↔red), b[-0.5, 0.5] (blue↔yellow)
  • HSL — hue/saturation/lightness cylinder on sRGB
    • h[0, 360), s[0, 1], l[0, 1]

Functions§

contrast_ratio
WCAG contrast ratio between two sRGB colors. Returns value >= 1.
hsl_to_srgb
Converts an HSL color to sRGB.
hsv_to_srgb
Convert HSV to sRGB. h in [0, 360), s and v in [0, 1].
linear_to_srgb
Converts a linear RGB color to sRGB (gamma-encoded).
luminance
Relative luminance (ITU-R BT.709). Input is linear RGB.
oklab_mix
Mixes two sRGB colors in Oklab space for perceptually uniform blending.
oklab_to_srgb
Converts an Oklab color back to sRGB.
palette_analogous
Analogous palette — two colors at +30 and -30 degrees in HSL.
palette_complementary
Complementary color — rotate hue by 180 degrees in HSL.
palette_triadic
Triadic palette — two colors at +120 and +240 degrees in HSL.
srgb_to_hsl
Converts an sRGB color to HSL.
srgb_to_hsv
Convert sRGB to HSV. Returns (h, s, v) where h in [0, 360), s and v in [0, 1].
srgb_to_linear
Converts an sRGB color (gamma-encoded) to linear RGB.
srgb_to_oklab
Converts an sRGB color to Oklab.