CKTIS / CSS
contrast-color() — automatic contrasting color
`contrast-color()` chooses a text color that contrasts with a supplied solid background color; it is an experimental specification surface at risk of change.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
contrast-color( <color> )contrast-color() chooses a text color that contrasts with a supplied solid background color; it is an experimental specification surface at risk of change.
The example confirms the supported contrast-color(<color>) form: contrast-color(rgb(20 20 20)) yields white, while contrast-color(rgb(240 240 240)) yields black. Extended contrast-color(<color>, white, black) is rejected; a grayscale scan finds Chromium’s current boundary between 117 and 118.
Grammar: contrast-color( <color> ).
The simple contrast-color(<color>) form is defined in CSS Color 5 and is not on that module’s formal at-risk list. It returns only white or black; the exact contrast algorithm and threshold are UA-defined, and the result should reach at least WCAG 2.1 AA Large. Separately, the current CSS Color 6 editor’s draft is marked Not Ready For Implementation and explores unfinished foreground/background roles, targets, and candidate grammar. That status is not formal at-risk, and rejection of contrast-color(<color>, white, black) shows that the extended form cannot yet be relied upon.
Examples
const supportsSimple=CSS.supports('color','contrast-color(rgb(0 0 0))'),supportsCandidates=CSS.supports('color','contrast-color(rgb(0 0 0), white, black)');const resolve=gray=>{const node=document.createElement('div');document.body.append(node);node.style.color='contrast-color(rgb('+gray+' '+gray+' '+gray+'))';return getComputedStyle(node).color};let firstBlack=-1,lastWhite=-1;for(let gray=0;gray<=255;gray++){const value=resolve(gray);if(value==='rgb(255, 255, 255)')lastWhite=gray;else if(value==='rgb(0, 0, 0)'&&firstBlack<0)firstBlack=gray;else if(value!=='rgb(0, 0, 0)')throw new Error('unexpected contrast color: '+value)}const dark=resolve(20),light=resolve(240);if(!supportsSimple||supportsCandidates||dark!=='rgb(255, 255, 255)'||light!=='rgb(0, 0, 0)'||firstBlack!==118||lastWhite!==117)throw new Error('contrast-color syntax/boundary mismatch: '+[supportsSimple,supportsCandidates,dark,light,lastWhite,firstBlack]);console.log('simple='+supportsSimple+'; candidates='+supportsCandidates+'; dark='+dark+'; light='+light+'; boundary='+lastWhite+'/'+firstBlack);Result
simple=true; candidates=false; dark=rgb(255, 255, 255); light=rgb(0, 0, 0); boundary=117/118
`contrast-color()` chooses a text color that contrasts with a supplied solid background color; it is an experimental specification surface at risk of change.