wcag-contrast 0.1.1

Compute WCAG 2 color contrast ratios and AA/AAA pass levels from hex or RGB colors. Focused, zero-dependency accessibility helper.
Documentation
  • Coverage
  • 100%
    18 out of 18 items documented1 out of 10 items with examples
  • Size
  • Source code size: 28.23 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 476.2 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • trananhtung/wcag-contrast
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • trananhtung

wcag-contrast

All Contributors

Crates.io Documentation CI License

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 wcag_contrast::{contrast_hex, level, WcagLevel};

let ratio = contrast_hex("#777777", "#ffffff").unwrap();
assert!((ratio - 4.48).abs() < 0.01);          // ~4.48 : 1
assert_eq!(level(ratio, false), WcagLevel::Fail); // just under 4.5 → fails AA for normal text
assert_eq!(level(ratio, true), WcagLevel::AA);    // 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.

[dependencies]
wcag-contrast = "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.