Contrast

How cortex measures contrast with WCAG 2 and APCA, which tokens carry a contrast guarantee, and what the two contrast settings change.

Contrast is how much a foreground stands out from its background. Cortex measures it two ways: the WCAG 2 ratio, which it guarantees, and APCA Lc, which it uses to choose tinted text colours.

WCAG 2 contrast ratio. The formula in the Web Content Accessibility Guidelines: (L1 + 0.05) / (L2 + 0.05), where L1 and L2 are the relative luminances of the lighter and darker colour. It runs from 1:1 to 21:1 and ignores which colour is the text. The thresholds most products are held to are 4.5:1 for normal text (1.4.3), 3:1 for large text (1.4.3) and for icons, borders and other non-text UI (1.4.11), and 7:1 for AAA text (1.4.6).

APCA lightness contrast (Lc). The Accessible Perceptual Contrast Algorithm, from the apca-w3 package. It models how readable text is: it knows which colour is the text, so dark-on-light and light-on-dark give different values (positive and negative Lc). Cortex uses the magnitude, |Lc|, with three targets (LC_TARGETS): 90 for body text, 60 for large or secondary text, 45 for icons and other non-text.

WCAG is the floor cortex guarantees. For the tinted -on-* tokens, APCA picks among the steps that pass that floor; the black-or-white -contrast tokens and the page tokens (-text, -border) use WCAG alone.

Which tokens carry a guarantee

Every colour role (primary, secondary, neutral, success, danger, warning, info) gets these, all measured when the theme is generated.

TokenWhat it isGuarantee
--fp-{role}-{n}-contrastPure black or white for text on step n.The better of the two; white is kept whenever it reaches 4.5:1.
--fp-{role}-textThe role’s colour for text and icons on the page (neutral steps 1–3). Step 9 itself where it reaches 4.5:1 against all three, else the nearest lightness that does, same hue.4.5:1 against the page.
--fp-{role}-borderThe role’s colour for a control’s outline. Step 8 where it reaches 3:1, else the nearest lightness that does.3:1 against the page.
--fp-{role}-{n}-on-text, -on-text-soft, -on-iconA same-hue foreground for step n: another step of the same ramp, or black or white if none fits.The tier’s WCAG floor (WCAG_FLOORS), and its APCA target wherever a step reaches it.

These are the AA floors. With settings.accessibility: "AAA" the floors are 7:1 for the role -contrast tokens, -on-text, -on-text-soft and -text (for -contrast, wherever black or white can reach it; the chart palette’s -contrast stays at 4.5:1), and 4.5:1 for -on-icon and -border (WCAG_FLOORS_AAA).

The three matrix tiers pick differently: on-text takes the most readable step that meets Lc 90, on-text-soft and on-icon take the softest step that still meets Lc 60 or 45. The WCAG floor is 4.5:1 for both text tiers and 3:1 for icons (7:1 and 4.5:1 with accessibility: "AAA"). When no step of the ramp meets both the target and the floor, the token falls back to black or white, whichever passes the floor; that fallback can sit below the APCA target (a mid-lightness step in dark mode is the usual case).

The default (lean) stylesheet carries -contrast on the fill steps 9–11 and on any other step a FlexPixels component reads. The matrix tokens and the rest of the -contrast tokens are in the full stylesheet, or in the lean one with "output": { "contrastMatrix": true } — see Build & Import.

How the engine measures

  • On the colour the browser paints. A ramp step can sit outside sRGB, and browsers bring it in differently: Chromium and Firefox clip each channel, CSS Color 4 recommends lowering chroma. These give different luminances. Every WCAG check is run on both renderings and the worse one counts (packages/cortex/src/theme/engine/gamut.ts).
  • With head-room. A picked colour must clear its floor by 0.05 (4.55:1 rather than 4.5:1), because the painted colour is rounded to 8 bits per channel.
  • Against the real step 9 and 10. A brand seed is step 9 exactly, so the foregrounds for steps 9 and 10 are measured on the seed and its hover, not on the ramp.
  • In both schemes. Each token is a light-dark() pair, and each half is measured in its own scheme.

The solvers are in packages/cortex/src/theme/engine/apca.ts (buildContrastMatrix) and engine/color.ts (pickBinaryFg, textColor, borderColor).

Worked example

The theme @flexpixels/ui ships (the theme-config.json at the root of the FlexPixels repository) sets "primary": "#4F46E5" and "warning": "#F59E0B". Its generated stylesheet has:

css
--fp-primary-9: var(--fp-primary-seed);
--fp-primary-9-contrast: light-dark(oklch(1 0 0), oklch(1 0 0));
--fp-primary-text: light-dark(var(--fp-primary-seed), oklch(0.6606 0.1821 276.9656));

--fp-warning-9: var(--fp-warning-seed);
--fp-warning-9-contrast: light-dark(oklch(0 0 0), oklch(0 0 0));
--fp-warning-text: light-dark(oklch(0.5186 0.1122 70.0804), var(--fp-warning-seed));
  • White on #4F46E5 is 6.29:1, so a primary button gets white text in both schemes.
  • White on the amber #F59E0B is only 2.15:1; black is 9.78:1, so a warning button gets black text.
  • In light mode the amber seed reaches only about 2:1 against the page, so --fp-warning-text moves to a darker amber (#915b00) that clears 4.5:1. In dark mode the seed itself is readable and is used as is. The indigo seed is the reverse: readable on the light page, too dark on the dark one.

You can check any pair with the same functions the engine uses:

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

checkContrast("#ffffff", "#4f46e5");
// { passes: true, ratio: 6.29, required: 4.5, lc: 85.67, polarity: "light-on-dark" }

checkContrast("#ffffff", "#f59e0b");
// { passes: false, ratio: 2.15, required: 4.5, lc: 46.44, polarity: "light-on-dark" }

What the two settings change

Both live in settings in theme-config.json.

accessibility: "AA" (default) or "AAA".

  • Sets the ratio ThemeEngine#getContrast requires when it reports passes: 4.5 for AA, 7 for AAA. (The standalone checkContrast takes the level as its third argument, "AA" by default.)
  • Sets the --fp-contrast-threshold token (0.6 for AA, 0.45 for AAA). It is in the full stylesheet only, and no FlexPixels component reads it.
  • With elevation.style: "neumorphic", AAA raises the ELEVATION_NEUMORPHIC_AAA warning (see Warnings).
  • It raises every floor the colour engine solves to: 7:1 for the role -contrast tokens, -on-text, -on-text-soft and -text (for -contrast, wherever black or white can reach it; the chart palette’s -contrast stays at 4.5:1), 4.5:1 for -on-icon and -border. It also moves --fp-text-subtle from neutral step 10 to 11.

contrastAlgorithm: "apca" (default) or "wcag2".

It only changes how the black-or-white -contrast tokens (and the chart palette’s -contrast tokens) are chosen:

  • "apca": white whenever white reaches the text floor plus the head-room above (4.55:1, or 7.05:1 at AAA) on both renderings, otherwise whichever of black and white has the higher ratio. This keeps white text on mid-tone brand colours that pass.
  • "wcag2": whichever of black and white has the higher WCAG ratio, as measured on the engine’s colour before it is rounded to 8 bits per channel. Where the two ratios are within 0.01 of each other, the colour a browser paints can favour the other one by less than 0.01.

For many seeds the two settings give the same result; the theme @flexpixels/ui ships is identical under both. The matrix tokens are always solved with APCA plus the WCAG floors, whatever this is set to.