Contrast helpers
The APCA and WCAG functions cortex uses to pick foreground colours, exported for build scripts, editor plugins and runtime checks.
Cortex’s contrast layer has two levels: functions that take OKLCH numbers (what the engine works in) and convenience wrappers that take any CSS colour string culori can parse (hex, named colours, rgb(…), oklch(…)). Everything on this page is exported from @flexpixels/cortex and from @flexpixels/cortex/browser; it uses culori and apca-w3 and no Node APIs.
For what the two contrast models are and how the engine uses them, see Contrast.
calcLc(fgL, fgC, fgH, bgL, bgC, bgH)
APCA lightness contrast between a foreground and a background given as OKLCH triples. Returns an LcResult:
import { calcLc, type LcResult } from "@flexpixels/cortex";
const result: LcResult = calcLc(0.55, 0.18, 252, 0.99, 0, 0);
// { lc: 71.38…, raw: 71.38…, polarity: "dark-on-light" }lc— the magnitude, always 0 or more. This is the number to compare with a target.raw— the signed value fromapca-w3: positive for dark text on a light background, negative for light on dark.polarity—"dark-on-light"or"light-on-dark".
Colours outside sRGB are brought in by lowering chroma at the same lightness, then rounded to 8-bit channels, before APCA runs. Targets cortex uses: 90 body text, 60 large or secondary text, 45 icons and other non-text.
calcLcHex(fg, bg)
The same, for CSS colour strings:
import { calcLcHex } from "@flexpixels/cortex";
calcLcHex("#3b82f6", "#ffffff");
// { lc: 63.89…, raw: 63.89…, polarity: "dark-on-light" }If either colour cannot be parsed it returns { lc: 0, raw: 0, polarity: "dark-on-light" } instead of throwing.
wcagRatio(fgL, fgC, fgH, bgL, bgC, bgH)
The WCAG 2 contrast ratio between two OKLCH colours, as a browser would paint them. A colour outside sRGB can be rendered two ways (each channel clipped, or chroma lowered), and they differ in luminance; wcagRatio measures both and returns the lower ratio.
import { wcagRatio } from "@flexpixels/cortex";
wcagRatio(0.55, 0.18, 252, 0.99, 0, 0);
// 4.70…WCAG 2 thresholds: 4.5:1 for normal text (1.4.3), 3:1 for large text and non-text UI (1.4.11), 7:1 for AAA text (1.4.6).
checkContrast(fg, bg, level?)
“Is this pair readable?” in one call, for CSS colour strings. level is "AA" (default) or "AAA".
import { checkContrast } from "@flexpixels/cortex";
checkContrast("#3b82f6", "#ffffff", "AA");
// {
// passes: false, // ratio >= required
// ratio: 3.68, // WCAG ratio, rounded to 2 decimals
// required: 4.5, // 4.5 for AA, 7 for AAA
// lc: 63.89, // APCA |Lc|, rounded to 2 decimals
// polarity: "dark-on-light",
// }
checkContrast("#ffffff", "#4f46e5").ratio; // 6.29The ratio is of what a screen shows. A translucent foreground is composited over the background (50% black on white is 3.98:1, not 21:1). A translucent background has no backdrop to be measured on, so three are tried: white, black, and the backdrop that brings the background closest to the text (on a nearly clear background, a page the colour of the text hides it). The worst of the three is taken. A colour outside sRGB is measured both clipped and chroma-reduced, and the worse of the two stands.
passes looks at the WCAG ratio only. If a colour cannot be parsed, the result has passes: false, ratio: 0 and lc: 0. ThemeEngine#getContrast(fg, bg) gives the same ratio at the engine’s settings.accessibility level, and throws on an unparseable colour instead.
LC_TARGETS, WCAG_FLOORS and WCAG_FLOORS_AAA
The per-tier targets the engine applies when it builds the contrast matrix. WCAG_FLOORS holds the AA floors; with settings.accessibility: "AAA" the engine uses WCAG_FLOORS_AAA.
import { LC_TARGETS, WCAG_FLOORS, WCAG_FLOORS_AAA } from "@flexpixels/cortex";
LC_TARGETS; // { text: 90, textSoft: 60, icon: 45 }
WCAG_FLOORS; // { text: 4.5, textSoft: 4.5, icon: 3 }
WCAG_FLOORS_AAA; // { text: 7, textSoft: 7, icon: 4.5 }A foreground the matrix picks must reach both the tier’s APCA target and its WCAG floor. When the engine checks a floor it adds a margin of 0.05 (so 4.55:1), because the painted colour is rounded to 8 bits per channel.
buildContrastMatrix(lightRamp, darkRamp, chromaCurve, seedC, seedH, darkStepOverrides?, lightStepOverrides?, floors?)
The solver behind --fp-{role}-{n}-on-text, -on-text-soft and -on-icon. For every background step (12) and every tier (text, textSoft, icon), it looks for a step of the same ramp that meets the tier’s APCA target and WCAG floor, in light and dark mode.
import {
buildContrastMatrix,
CHROMA_LIGHTNESS_RAMP_LIGHT,
CHROMA_LIGHTNESS_RAMP_DARK,
CHROMA_CURVE,
type ContrastPair,
type ContrastTier,
} from "@flexpixels/cortex";
const matrix: Record<ContrastTier, ContrastPair[]> = buildContrastMatrix(
CHROMA_LIGHTNESS_RAMP_LIGHT,
CHROMA_LIGHTNESS_RAMP_DARK,
CHROMA_CURVE,
/* seedC */ 0.18,
/* seedH */ 252,
);
matrix.text[8]; // background step 9
// { lightFgStep: -2, darkFgStep: -1, lightLc: 84.57…, darkLc: 39.10… }- Step indexes are 0-based (
8is step 9).-1means pure black,-2pure white. texttakes the step with the highest Lc;textSoftandicontake the lowest Lc that still meets the target (the softest readable colour).- It first looks for one step that works in both modes; if there is none, it solves each mode on its own.
- If no step meets both gates in a mode, it falls back to black or white, whichever passes the WCAG floor. That can leave Lc below the target, as in the dark-mode result above (39 against 90).
- The optional override arrays replace individual steps’ colours (12 entries,
nullfor no change). The engine uses them for theseeds.surfaceanchor and for brand seeds, which make steps 9 and 10 exact colours rather than ramp values. floors— the WCAG floor per tier (defaultWCAG_FLOORS; passWCAG_FLOORS_AAAfor AAA).
srgbChromaCeil(h)
The highest seed chroma at hue h that keeps the core steps (those with a chroma multiplier of at least 0.7, steps 7–10) inside sRGB in both the light and dark ramps. The engine caps each role’s ramp chroma (--fp-{role}-c) at this value; you can use it to check input before it reaches the engine.
import { srgbChromaCeil } from "@flexpixels/cortex";
srgbChromaCeil(252); // 0.1477…Steps 1–6 and 11–12 may still fall slightly outside sRGB. Browsers bring them back in differently (Chromium and Firefox clip channels), which is why every contrast the engine promises is measured on both renderings. maxSrgbChroma(h, l) gives the ceiling at a single lightness.
Related
- Contrast — which tokens carry which guarantee.
- ThemeEngine —
getContrastat the theme’s level. - Helpers & constants — the ramps and curve the solver reads.