Perceptual color

Why perceptual color spaces suit design systems better than sRGB, and the places in cortex that depend on it.

sRGB describes colour the way a screen emits it: three channel intensities, gamma-encoded for the hardware. That is right for a display and awkward for design. Equal changes in the numbers do not look like equal changes, so a palette built by stepping sRGB or HSL values comes out uneven: some steps jump, others barely move, and the same “lightness” means different things for different hues.

A perceptual colour space reshapes the numbers so that equal distances look roughly equal to a person. CIELAB (1976) is the long-standing standard one. OKLab (2020) fixes some of its problems, notably blues that drift toward purple as they lose chroma. OKLCH, which cortex uses, is OKLab written as lightness, chroma and hue — see OKLCH.

What a design system gets from it

  • Ramps that step evenly. A fixed lightness list gives the same visual rhythm for any hue.
  • Steps that mean the same thing in every role. If step 3 is equally light for danger and info, “step 3 is an input background” holds for both.
  • Hue and chroma you can reason about. Lowering chroma makes a colour greyer without making it lighter or darker.
  • Measurable differences. The distance between two colours is a number you can test against a threshold.

Where cortex relies on it

All of these live in packages/cortex/src/theme/engine/color.ts unless noted.

One lightness ramp for every role. CHROMA_LIGHTNESS_RAMP_LIGHT and CHROMA_LIGHTNESS_RAMP_DARK (in schema/constants.ts) are shared by all seven roles. Lightness is deliberately not configurable per role: the step meanings in STEP_USAGE and the contrast pairs are worked out against these exact values. The ramp’s lightness cannot be changed per role: a lighter or darker brand seed moves only that role’s steps 9 and 10 (the seed and its hover), and seeds.surface only neutral’s dark steps 1–5.

A chroma curve, not a fixed chroma. Each step keeps a share of the seed’s chroma (CHROMA_CURVE): 0.12 at step 1, rising to 1 at step 9, falling to 0.35 at step 12. Pale backgrounds and dark text stay tinted without looking loud.

Semantic roles at fixed hues. When success, danger, warning or info is left out of seeds, the engine generates it at a fixed hue (HUE_ANCHORS: 145, 25, 85, 250) with the primary seed’s chroma, never less than 0.12, so danger still reads as red under a grey brand, and capped to what sRGB can show at that hue.

Greys that lean toward the brand. If the neutral seed has chroma below 0.05, it takes the primary seed’s hue. In the theme @flexpixels/ui ships, the neutral seed #334155 has hue 257, but the stylesheet says --fp-neutral-h: 277, the hue of the primary #4F46E5.

A hover you can see. Step 10 of a brand role is the seed moved 0.05 in lightness. hoverOf measures the OKLab distance between the two and, if the move does not read as a change (or the seed sits too close to black or white), moves the other way. A seed at the very end of the range (black, white and their neighbours) has one way to go, and one step of it cannot be seen; the hover walks further inward, 0.05 at a time, until it differs visibly (CIEDE2000 ≥ 2): a black brand’s step 10 is L + 0.2.

A separate dark ramp. Dark mode is not the light ramp reversed. Its lightness values are tuned on their own, and seeds.surface can anchor steps 1–5 of neutral to a tinted page colour in either scheme, blending back to the standard ramp by step 6 (resolveSurfaceAnchor).

Worked example: auto-generated roles

With cortex’s built-in default config (defaultThemeConfig: primary: #4f46e5, no semantic seeds), cortex generates the four semantic roles. Their hues are the anchors, and their chroma is the primary’s 0.1934, lowered where that hue cannot hold it in sRGB:

Role--fp-{role}-h--fp-{role}-cWhy that chroma
success1450.1553sRGB ceiling at hue 145
danger250.1934the primary’s chroma fits
warning850.1012sRGB ceiling at hue 85 (yellows)
info2500.1398sRGB ceiling at hue 250

All four sit at the same lightness at every step (step 9 is L 0.5 in light mode and L 0.61 in dark), so a success badge and a danger badge have the same visual weight. Yellow gets the least chroma: at mid lightness, sRGB cannot show a vivid yellow.

You can check the ceiling yourself:

ts
import { srgbChromaCeil } from "@flexpixels/cortex";

srgbChromaCeil(85); // 0.1011…  the most chroma steps 7–10 can carry at hue 85
srgbChromaCeil(277); // 0.1934…

What perception does not give you

Readable text. Perceptual lightness says how light a colour looks, not whether text on it can be read. Cortex measures that separately with WCAG and APCA — see Contrast.

Colours every screen can show. A perceptual space can describe colours outside sRGB, and browsers bring them back in differently: Chromium and Firefox clip each channel, which changes the colour’s luminance, while CSS Color 4 recommends lowering chroma. Cortex keeps the core steps inside sRGB, and wherever it promises a contrast it measures both renderings and counts the worse one (engine/gamut.ts).