OKLCH
Why cortex generates its palette in OKLCH instead of HSL or RGB, and how a hex seed becomes a 12-step ramp.
OKLCH writes a colour as three numbers (lightness, chroma, hue) designed to track how people perceive colour:
| Channel | Range in practice | What it means |
|---|---|---|
L | 0 to 1 | Perceived lightness. 0 is black, 1 is white. |
C | 0 to about 0.37 | Chroma: how colourful it is. 0 is grey. |
H | 0 to 360 | Hue angle in degrees (around 25 is red, 145 green, 250 blue). |
It is the polar form of OKLab, a colour space designed so that equal numeric steps look like roughly equal visual steps. Browsers support it natively: oklch(0.5 0.19 277) is CSS Color 4, and relative colour syntax from CSS Color 5 (oklch(from var(--x) calc(l - 0.05) c h)) derives one colour from another.
Why not HSL
HSL is a reshaped version of sRGB, so its “lightness” is not the lightness you see. Two colours with the same HSL lightness can look very different:
| Colour | HSL | OKLCH L |
|---|---|---|
| Yellow | hsl(60 100% 50%) | 0.968 |
| Blue | hsl(240 100% 50%) | 0.452 |
Both are “50% lightness” in HSL; in OKLCH the yellow’s L is more than twice the blue’s. A ramp built in HSL would put step 3 of a yellow role and step 3 of a blue role at visibly different lightness. In OKLCH, one lightness value looks roughly equally light for every hue, so cortex can use one lightness ramp for every colour role.
How cortex uses it
The colour engine is packages/cortex/src/theme/engine/color.ts; the constants it reads are in packages/cortex/src/theme/schema/constants.ts.
- Parse the seed. Each hex in
seedsis converted to OKLCH withculori(parseSeed). The hue and chroma are kept; chroma is capped at0.37, and a seed below0.005chroma is treated as pure grey. - Clamp to the screen.
srgbChromaCeil(h)finds the highest chroma at that hue that keeps the core steps (the ones whose chroma multiplier is at least0.7: steps 7–10) inside sRGB in both the light and dark ramps. The seed’s chroma is lowered to that ceiling if needed. - Build the ramp. Step
nisoklch(L[n] C × CHROMA_CURVE[n] H), whereL[n]comes fromCHROMA_LIGHTNESS_RAMP_LIGHTorCHROMA_LIGHTNESS_RAMP_DARK. The seed’s own lightness is not used for the ramp: the ramp’s lightness is shared by every role, which is what keeps step meanings (1 = page background, 12 = high-contrast text) the same across roles. - Keep the brand colour. For a seed the config gives (
primary,secondary, and any semantic seed set explicitly), step 9 is the seed itself, unchanged, in both schemes, and step 10 is its hover:0.05darker in light mode and lighter in dark mode, or the other way when the seed sits too near that end or the move would not read as a change (hoverOf); for a black or white seed it walks further inward until the step differs visibly.neutraland auto-generated roles stay on the ramp. - Emit live CSS. The stylesheet carries the hue, the chroma and the curve as variables, and every step except a brand role’s step 9 (the seed itself) is a
light-dark()pair computed in CSS.
The two lightness ramps:
| Step | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Light | 0.985 | 0.96 | 0.93 | 0.895 | 0.855 | 0.79 | 0.7 | 0.58 | 0.5 | 0.42 | 0.34 | 0.22 |
| Dark | 0.15 | 0.19 | 0.23 | 0.26 | 0.3 | 0.35 | 0.42 | 0.53 | 0.61 | 0.69 | 0.78 | 0.92 |
| Curve | 0.12 | 0.2 | 0.3 | 0.28 | 0.42 | 0.55 | 0.72 | 0.92 | 1 | 0.85 | 0.55 | 0.35 |
The dark ramp is tuned separately, not mirrored from the light one. The curve row is CHROMA_CURVE: the share of the seed’s chroma each step keeps, highest at step 9.
Worked example
The theme @flexpixels/ui ships (the theme-config.json at the root of the FlexPixels repository) uses "primary": "#4F46E5". In OKLCH that seed is L 0.5106, C 0.2301, H 276.97. The chroma is above the sRGB ceiling for hue 277 (0.1934), so the ramp uses 0.1934. The generated stylesheet contains:
--fp-primary-h: 277;
--fp-primary-c: 0.1934;
--fp-C-1: 0.12;
/* … --fp-C-2 to --fp-C-12 … */
--fp-primary-1: light-dark(
oklch(0.985 calc(var(--fp-primary-c) * var(--fp-C-1)) var(--fp-primary-h)),
oklch(0.15 calc(var(--fp-primary-c) * var(--fp-C-1)) var(--fp-primary-h))
);
--fp-primary-9: var(--fp-primary-seed);
--fp-primary-10: light-dark(
oklch(from var(--fp-primary-seed) calc(l - 0.05) min(c, 0.285) h),
oklch(from var(--fp-primary-seed) calc(l + 0.05) min(c, 0.2451) h)
);
--fp-primary-seed: #4f46e5;Step 9 is exactly #4f46e5. The other steps are computed. In light mode, steps 1, 8 and 12 come out as #f6f9ff, #636ae1 and #141639.
What happens on wide-gamut screens
OKLCH can describe colours an sRGB screen cannot show. Cortex keeps the core steps inside sRGB by default, and adds a block for P3 displays that raises the chroma curve at steps 7, 8 and 10 (step 9 is already 1):
@media (color-gamut: p3) {
:root {
--fp-C-7: 0.8;
--fp-C-8: 1;
--fp-C-9: 1;
--fp-C-10: 1;
}
}With output.legacyFallback: true, browsers without light-dark() or relative colour syntax get static rgb() fallbacks for the colour tokens instead, from an @supports not (…) block at the end of the stylesheet. It is off by default: the stylesheet is modern-only.
Changing colours at runtime
Because the ramps are CSS math, you can re-tint a role without regenerating:
const root = document.documentElement.style;
root.setProperty("--fp-primary-h", "200");
root.setProperty("--fp-primary-c", "0.15");
root.setProperty("--fp-primary-seed", "#0e7490");Steps 1–8 and 11–12 follow -h and -c; steps 9 and 10 follow -seed. Anything cortex measured at build time is not measured again: the black or white -contrast foregrounds, --fp-primary-text and --fp-primary-border (where one of them is the seed itself it follows the new seed unchecked; elsewhere it keeps the old colour), the hover’s chroma cap and direction, and the choice of steps in the contrast matrix. For a new brand colour, regenerate the theme so those are measured again.
Related
- Perceptual color — why perceptual spaces suit design systems, and where cortex relies on it.
- Contrast — how cortex measures text and icon colours against these ramps.
- Color System — the roles and steps in use.
seedsin the config reference — what you can set.- Schema constants —
CHROMA_LIGHTNESS_RAMP_LIGHT,CHROMA_CURVEand the rest.