color-contrast-wcag
v1.0.1
Published
Zero-dependency, precision color contrast ratio and WCAG 2.1/2.2 accessibility checker with AA/AAA compliance reporting and auto-adjustment suggestions.
Maintainers
Readme
🎨 color-contrast-wcag
Zero-dependency, precision color contrast ratio and WCAG 2.1/2.2 accessibility checker with AA/AAA compliance reporting and auto-adjustment suggestions.
✨ Features
- ⚡ Zero Dependencies — Pure mathematical implementation of W3C WCAG 2.1/2.2 specs (~2 KB).
- 🌈 Multiple Color Formats — Supports
#HEX(3, 4, 6, 8-digit),rgb(),rgba(), and standard named colors. - 🎯 Full WCAG Evaluation — Reports compliance for Normal Text (AA >= 4.5, AAA >= 7.0), Large Text (AA >= 3.0, AAA >= 4.5), and UI Components (AA >= 3.0).
- 💡 Auto-Fix Suggester —
suggestAccessibleColorautomatically adjusts an inaccessible color until it reaches passing contrast! - 🌓 Smart Text Color Picker —
getBestTextColorpicks either#000000or#ffffffbased on optimal background contrast. - 📦 Dual ESM & CommonJS — Works in Node.js, React, Next.js, Vite, and browsers.
📥 Installation
npm install color-contrast-wcagor with Yarn / pnpm:
pnpm add color-contrast-wcag
# or
yarn add color-contrast-wcag🚀 Quick Start
1. Contrast Ratio & WCAG Evaluation
import { checkWcag, getContrastRatio } from 'color-contrast-wcag';
// 1. Quick ratio calculation
const ratio = getContrastRatio('#3b82f6', '#ffffff');
console.log(ratio); // 3.68
// 2. Comprehensive WCAG compliance report
const evaluation = checkWcag('#3b82f6', '#ffffff');
console.log(evaluation);
/*
{
ratio: 3.68,
formattedRatio: "3.68:1",
normalText: { aa: false, aaa: false },
largeText: { aa: true, aaa: false },
uiComponent: { aa: true },
score: "Fail"
}
*/2. Auto-Fix an Inaccessible Color (suggestAccessibleColor)
If a designer or user provides a brand color that fails accessibility audits, automatically darken or lighten it to reach compliance:
import { suggestAccessibleColor, getContrastRatio } from 'color-contrast-wcag';
// Original text color #9ca3af on white background has low contrast (~2.0:1)
const accessibleColor = suggestAccessibleColor('#9ca3af', '#ffffff', 4.5);
console.log(accessibleColor); // "#717882" (adjusted to meet AA threshold)
console.log(getContrastRatio(accessibleColor, '#ffffff')); // >= 4.53. Dynamic Text Color for Badges & Buttons (getBestTextColor)
Automatically pick high-contrast black or white text for dynamic background colors:
import { getBestTextColor } from 'color-contrast-wcag';
console.log(getBestTextColor('#111827')); // "#ffffff" (white text for dark background)
console.log(getBestTextColor('#fef08a')); // "#000000" (black text for light yellow background)📊 WCAG 2.1 / 2.2 Standards Reference
| Category | Level AA Threshold | Level AAA Threshold |
|---|---|---|
| Normal Text (< 18pt or < 14pt bold) | 4.5 : 1 | 7.0 : 1 |
| Large Text (>= 18pt or >= 14pt bold) | 3.0 : 1 | 4.5 : 1 |
| UI Components & Graphics | 3.0 : 1 | N/A |
🛠 Development & Testing
# Install dependencies
npm install
# Run unit tests
npm test
# Build dual ESM/CJS bundles + .d.ts types
npm run build💖 Support the Project
If you find this package useful, consider sponsoring its ongoing development and maintenance: 👉 Sponsor @Piratha97 on GitHub Sponsors
📄 License
MIT © 2026 Pirapaakaran Pirathapan
