Check color contrast in JavaScript
Your muted label looks fine on white, until you run a contrast check. At 4.48:1, #777777 falls short of the 4.5:1 WCAG AA minimum for normal text. Colore can measure the pair and find a nearby gray that passes.
npm install colore-js
yarn add colore-js
Check the color against its actual background
Start with contrastRatio to see how close you are to the limit. Use isReadable for the pass or fail decision. Both take the text color and the background behind it.
import { contrastRatio, isReadable } from 'colore-js/fn';
const text = '#777777';
const background = '#ffffff';
console.log(contrastRatio(text, background).toFixed(2)); // '4.48'
console.log(isReadable(text, background)); // false
console.log(isReadable(text, background, { intent: 'largeText' })); // true
Large text has a 3:1 AA minimum. Use that intent only when the text meets the WCAG definition of large text. For small labels and body text, keep the default text intent. Round the ratio for display after you make the decision.
WCAG contrast thresholds
For AAA large text, pass { intent: 'largeText', level: 'aaa' }. Sources: WCAG 2.2 contrast minimum, enhanced contrast and non-text contrast.
How the ratio is calculated
WCAG 2 compares the relative luminance of the two colors. If your colors are always six-digit hex, you can write it yourself in a few lines:
function channel(value) {
const c = value / 255;
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
}
function luminance(hex) {
const [r, g, b] = [1, 3, 5].map((i) => channel(parseInt(hex.slice(i, i + 2), 16)));
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function contrast(a, b) {
const [light, dark] = [luminance(a), luminance(b)].sort((x, y) => y - x);
return (light + 0.05) / (dark + 0.05);
}
contrast('#777777', '#ffffff'); // 4.478089453577214
It returns NaN as soon as a color arrives in another form: '#fff', 'oklch(0.6 0.2 260)' from a Tailwind CSS v4 theme, or a translucent rgb(0 0 0 / 50%). contrastRatio reads any CSS color and composites translucent ones over the background first:
import { contrastRatio } from 'colore-js/fn';
contrastRatio('#777777', '#fff'); // 4.478089453577214
contrastRatio('oklch(0.6 0.2 260)', '#ffffff'); // 4.060703177675777
contrastRatio('rgb(0 0 0 / 50%)', '#ffffff'); // 3.97665302491244
Fix a failing foreground
Pass the same pair to ensureContrast. It returns the color and a satisfied flag. Check the flag before writing the replacement, particularly if you restrict which direction the color can move.
import { ensureContrast } from 'colore-js';
import { isReadable } from 'colore-js/fn';
const result = ensureContrast('#777777', '#ffffff');
console.log(result.satisfied); // true
console.log(result.color.hex()); // '#767676'
console.log(isReadable(result.color.hex(), '#ffffff')); // true
The difference is one step per RGB channel. At an exact cutoff, check the value you export. A color you measured before rounding can land on the other side of the threshold once you write it as hex. For a wide-gamut color, decide whether you intend to ship a wide CSS value or an sRGB fallback and check the fallback separately. If the same label appears on more than one surface, repeat the check for each background.
Check and repair a stylesheet
Use auditCssVariables when the colors already live in a stylesheet. It checks named pairs in each scope. fixCssVariables returns the updated CSS and the issues it could not repair. Review the changes before writing them back to a file.
Here is the full workflow on a stylesheet with one text pair.
import { auditCssVariables, fixCssVariables } from 'colore-js';
const css = ':root { --background: #fff; --foreground: #999; }';
const before = auditCssVariables(css);
const after = fixCssVariables(css);
console.log(before.issues[0]?.message);
// '--foreground on --background in :root: 2.85:1, needs 4.5:1'
console.log(after.css); // ':root { --background: #fff; --foreground: #767676; }'
console.log(after.checked, after.issues.length); // 1 0
Use the pair count alongside the issue count. A stylesheet with zero recognized pairs gives you no evidence about its text.
What Colore checks for you
Colore measures the foreground and background colors you provide. It composites translucent colors for the check and assumes white beneath a translucent background. If your component sits over a different page color, use the resolved background for that component. You can also read contrastAPCA when your project calls for APCA. WCAG AA and AAA use the WCAG ratio.
Related
- Dark mode colors for the same check in two modes
- Black or white text to pick a label color for any background
- Audit a palette or stylesheet for explicit pair configuration
- Precision and equality for how hex export rounds channels