color-contrast-a11y
v1.0.0
Published
Zero-dependency pure mathematical WCAG 2.2 contrast ratio calculator and intelligent auto-snap color fixer for AI agents, design systems, and frontend developers.
Downloads
386
Maintainers
Readme
🎨 color-contrast-a11y
Zero-dependency Pure Mathematical WCAG 2.2 Contrast Ratio Calculator & Intelligent Auto-Snap Color Fixer.
Built for AI coding agents, design system engineers, and frontend accessibility auditing.
💥 The Problem with Colors in AI UI Generation
When AI coding assistants (Cursor, Claude, v0, Windsurf) build dark mode UIs:
- Low-contrast text (e.g. muted slate
#64748bon dark navy#0f172a) fails accessibility audits (3.75:1< required4.5:1). - Developers struggle to find the exact hex color that preserves the color tone while satisfying WCAG AA.
color-contrast-a11y calculates sRGB Relative Luminance in 0.05ms and intelligently steps the color until it passes WCAG AA (4.5:1) or AAA (7.0:1).
⚡ Quick Start (CLI)
1. Check Contrast Ratio
npx contrast-a11y check "#64748b" "#0f172a"
# Output:
# 📊 WCAG 2.2 Contrast Ratio: 3.75:1
# - AA Normal Text (>= 4.5:1): ❌ FAIL2. Auto-Fix Text Color for Compliance
npx contrast-a11y fix "#64748b" "#0f172a"
# Output:
# 🔧 WCAG Auto-Fix Result (Target: 4.5:1 AA)
# - Background: #0f172a
# - Original Text: #64748b (Ratio: 3.75:1)
# - Adjusted Text: #748297 (Ratio: 4.56:1) ✨ Auto-adjusted to pass WCAG AA📦 TypeScript / JavaScript SDK
import { getContrastRatio, autoFixTextColor } from 'color-contrast-a11y';
// 1. Calculate Ratio
const evaluation = getContrastRatio('#38bdf8', '#0f172a');
console.log(evaluation.score); // e.g. "8.92:1"
console.log(evaluation.aaNormal); // true
// 2. Auto-Fix Failing Colors
const fixed = autoFixTextColor('#64748b', '#0f172a', 4.5);
console.log(fixed.fixedColor); // "#748297" (passes 4.5:1 AA)📜 License
MIT License © OIYO Lab
