Check the contrast between two colors from the command line, using the WCAG 2.x formula. No dependencies, Node 18+.
$ npx github:wesomitsu/YOLO "#1e293b" "#ffffff"
#1e293b on #ffffff
Contrast 14.63:1
Normal text AA pass AAA pass
Large text AA pass AAA passcontrast-check <text color> <background color> [--json]Colors can be #rgb, #rrggbb (the # is optional), rgb(r, g, b) or hsl(h, s%, l%).
Quote them, since # starts a comment in most shells.
--json prints the result as JSON, handy for scripts:
$ contrast-check "#777" "#fff" --json
{
"text": "#777",
"background": "#fff",
"ratio": 4.48,
"normal": {
"AA": false,
"AAA": false
},
"large": {
"AA": true,
"AAA": false
}
}--fail-under AA (or AAA) exits with code 1 when normal text misses that level, so you can use it in CI:
contrast-check "#777" "#fff" --fail-under AA # exits 1--suggest prints the closest text color (same hue, darker or lighter) that passes AA:
$ contrast-check "#777" "#fff" --suggest
#777 on #fff
Contrast 4.48:1
Normal text AA fail AAA fail
Large text AA pass AAA fail
Try #767676 for AA (4.54:1)Results are colored green/red in a terminal. Set NO_COLOR=1 to turn that off.
- uses: wesomitsu/YOLO@main
with:
text: '#1e293b'
background: '#ffffff'
level: AA # or AAAThe step fails if the pair misses the level, and prints a suggested fix.
| Level | Normal text | Large text (24px+, or 18.66px+ bold) |
|---|---|---|
| AA | 4.5:1 | 3:1 |
| AAA | 7:1 | 4.5:1 |
npm install github:wesomitsu/YOLOimport { contrastRatio, rate } from 'contrast-check';
const ratio = contrastRatio('#1e293b', '#fff'); // 14.63...
rate(ratio).normal.AA; // truenpm testMIT