Skip to content
wesomitsuPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

18 Commits

Folders and files

Repository files navigation

contrast-check

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 pass

Usage

contrast-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.

GitHub Action

- uses: wesomitsu/YOLO@main
  with:
    text: '#1e293b'
    background: '#ffffff'
    level: AA # or AAA

The step fails if the pair misses the level, and prints a suggested fix.

What the levels mean

Level Normal text Large text (24px+, or 18.66px+ bold)
AA 4.5:1 3:1
AAA 7:1 4.5:1

Use it in code

npm install github:wesomitsu/YOLO
import { contrastRatio, rate } from 'contrast-check';

const ratio = contrastRatio('#1e293b', '#fff'); // 14.63...
rate(ratio).normal.AA; // true

Development

npm test

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages