Auto Contrast

A readable foreground derived in CSS for a colour nobody knew at build time — measured, with the accessibility floor proved over every sRGB colour.

Cortex solves contrast at build time. Every colour step gets a measured foreground: --fp-{role}-{n}-contrast (steps 8–11 in the default stylesheet, every step in the full one), plus the -on-text / -on-text-soft / -on-icon matrix when it is enabled. They are solved against APCA targets with WCAG floors and pinned by a test that walks every pair in both modes.

Those are the answer, and this page does not replace them.

Auto contrast is for the one case they cannot cover: a colour that does not exist until runtime.

  • a brand colour the user picks in a settings panel
  • a tag or category colour coming out of a CMS
  • a chart series coloured from data

There is no build step to solve those in, so the decision has to happen in the stylesheet.

Usage

The cortex theme output does not declare --fp-auto-*. Put what autoContrastCss() returns inside a * {} rule. An element that sets no --fp-auto-surface, and has no ancestor that does, is solved against --fp-surface-app where it is, so a theme applied to part of the page counts as the page there; do not give --fp-auto-surface a value on :root, which would carry the root’s page into every such part. (@flexpixels/ui, which is not published yet, ships the same rules in its theme.css.)

Name the surface once. The foreground follows.

css
.brand-chip {
  --fp-auto-surface: var(--user-brand-color);

  background: var(--fp-auto-surface);
  color: var(--fp-auto-text);
}

Because the surface is the only input, moving it moves both halves — no second declaration, no per-state foreground:

css
.brand-chip:hover {
  --fp-auto-surface: var(--user-brand-hover);
}
.brand-chip:active {
  --fp-auto-surface: var(--user-brand-active);
}

No FlexPixels component reads --fp-auto-*. To use it on one, point the component’s colour hooks at it: for fp-button, --fp-button-background and --fp-button-color and their -hover / -active forms.

The two tiers

TokenWhat it isGuaranteedUse for
--fp-auto-textPure white or black, by measured luminance4.5:1Text of any size
--fp-auto-iconThat pick, drifted 25% back toward the surface3:1Icons, borders, other non-text only

--fp-auto-text clears WCAG AA on all 16,777,216 sRGB colours — proved exhaustively, not sampled.

--fp-auto-icon reads as tinted rather than stark, which is usually what a design wants for a glyph sitting on a brand colour. It clears WCAG 1.4.11’s 3:1 non-text floor and nothing more is claimed, because nothing more is true.

There is no tinted text tier

Picking white or black by measured luminance can do no better than √21 ≈ 4.583:1 in the worst case, the crossover where white and black are equally bad. The shipped rule bottoms out at 4.524:1 (on #e5271f). AA needs 4.5:1, so there is only about 0.024 to spend, and a 5% tint already overspends it. Worst ratios over every sRGB colour, measured on the painted (8-bit) colours:

Tint toward the surfaceWorst ratio
0% (the binary pick)4.524:1
5%4.235:1
15%3.714:1
25% (--fp-auto-icon)3.242:1
30%3.029:1

A tinted, on-brand text colour comes from the build-time matrix, which measures each pair instead of deriving it and therefore has no such ceiling.

Why it thresholds luminance, not OKLCH lightness

The obvious version of this rule reads beautifully:

css
/* Do NOT do this. */
color: oklch(from var(--surface) clamp(0, calc((0.6 - l) * 1000), 1) 0 0);

Over all 16,777,216 sRGB colours, l < 0.6 leaves 4.2% below AA — worst case 3.66:1 on #199b09 — and no threshold fixes it: the best value (about 0.58) still fails 2.0%. Failures occur at every hue, most at 130–150° (greens), and even greys fail, from #777777 (4.48:1) to #808080 (3.95:1).

OKLCH l is perceptual lightness. WCAG contrast is computed from relative luminance, which weights green at 0.7152. They are different functions of the same colour, and no constant reconciles them.

So the rule thresholds the luminance itself, which CSS can now express — relative colour syntax hands the stylesheet the surface’s own channels, and pow() linearises them:

css
--fp-auto-text: rgb(from var(--fp-auto-surface) CH CH CH / 1);
/* CH = clamp(0, calc((0.18 - LUMINANCE) * infinity), 255) */

The switch point, 0.18, is measured rather than chosen: 0.179–0.181 is the entire window that clears AA everywhere, and 0.180 is the only value that also clears 4.52.

Limits

  • A surface outside sRGB is read as the sRGB colour it clips to. A P3 colour, or a step a hair past the gamut edge, has a negative sRGB channel; each channel is clamped to zero before it is linearised, so the choice is made on what an sRGB screen paints. (Before the clamp such a surface got black text whatever its lightness.)
  • The surface must be a single colour. A gradient or an image in --fp-auto-surface is not a colour, so rgb(from …) is invalid, --fp-auto-text with it, and the text inherits its colour from its parent, whatever the surface. Set --fp-auto-surface to a colour, the one behind the text, and paint the gradient or image with background-image on its own.
  • The surface must be opaque. Relative colour syntax reads the surface’s own channels, so a translucent surface is judged on its own colour rather than on whatever shows through it. The foreground is forced opaque so at least the text is not translucent too.
  • Set it in the light DOM, or on a component’s :host. The universal rule that re-derives the pair cannot reach inside a shadow root; a shadow part gets its host’s foreground unless the component declares a second pair itself.
  • Do not register it. @property syntax: "<color>" on the input resolves light-dark() once, at the declaring element, and the value stops flipping with color-scheme.
  • Forced-colors mode replaces color outright, as it should. This mechanism neither helps nor fights it.

Contracts

FileWhat it proves
packages/cortex/src/theme/engine/auto-contrast.tsThe measured constants, and why each one is what it is
packages/cortex/src/theme/test/auto-contrast.test.tsAA on every one of the 16,777,216 sRGB colours; the threshold fails if nudged either way
packages/ui/src/theme/auto-contrast.test.tsThe shipped stylesheet has not drifted from those constants
packages/ui/e2e/auto-contrast.e2e.tsA real engine paints it, graded from rasterised pixels