wcag-contrast
Color contrast for accessibility. Compute WCAG 2 contrast ratios from hex or RGB colors and check whether they pass AA / AAA for normal or large text. A small, focused, zero-dependency helper.
use ;
let ratio = contrast_hex.unwrap;
assert!; // ~4.48 : 1
assert_eq!; // just under 4.5 → fails AA for normal text
assert_eq!; // passes AA for large text (≥3.0)
Why wcag-contrast?
Accessibility checks (WCAG AA/AAA contrast) show up in linters, design systems, CI gates, and CSS tooling. The color math exists inside large general-purpose color crates, but there was no small, dependency-free crate that just answers "what's the contrast ratio, and does it pass?". This is that crate.
[]
= "0.1"
API
| Item | Purpose |
|---|---|
Rgb::from_hex(&str) |
Parse "#rgb" / "#rrggbb" (with/without #, case-insensitive) |
Rgb::relative_luminance() |
WCAG relative luminance (0.0–1.0) |
contrast_ratio(a, b) |
Contrast ratio (1.0–21.0), order-independent |
contrast_hex(a, b) |
Same, straight from two hex strings |
level(ratio, large_text) |
Highest WcagLevel passed (Fail / AA / AAA) |
Thresholds
| AA | AAA | |
|---|---|---|
| Normal text | 4.5 : 1 | 7 : 1 |
| Large text (≥18pt, or ≥14pt bold) | 3 : 1 | 4.5 : 1 |
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of Apache-2.0 or MIT at your option.