How to Build an Interactive WCAG 2.2 Color Contrast & OKLCH Palette Inspector
WCAG 2.2 relative luminance is calculated by converting sRGB channels to linear light (`channel / 255 <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4`) and computing `L = 0.2126*R + 0.7152*G + 0.0722*B`. The contrast ratio is `(L1 + 0.05) / (L2 + 0.05)`, requiring 4.5:1 for AA normal text and 7.0:1 for AAA.
Step 1: Design the Palette Swatch & Ratio Card
Build the interactive swatch picker and live contrast compliance status badge.
<div class="contrast-swatch-widget" id="contrastWidget">
<div class="swatch-preview" id="swatchPreview" style="background: #6366f1; color: #ffffff;">
<span class="preview-text">Sample Typography</span>
<span class="ratio-badge" id="ratioBadge">4.8:1 (AA Pass)</span>
</div>
<div class="swatch-controls">
<div class="color-picker-group">
<label>Background:</label>
<input type="color" id="bgColor" value="#6366f1">
</div>
<div class="color-picker-group">
<label>Foreground:</label>
<input type="color" id="fgColor" value="#ffffff">
</div>
</div>
</div>
Step 2: Compute Relative Luminance & Contrast Ratios
Implement the W3C luminance formula to calculate exact contrast ratios.
function hexToRgb(hex) {
const num = parseInt(hex.replace('#', ''), 16);
return [ (num >> 16) & 255, (num >> 8) & 255, num & 255 ];
}
function luminance([r, g, b]) {
const a = [r, g, b].map(v => {
v /= 255;
return v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
});
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
}
function getContrast(hex1, hex2) {
const lum1 = luminance(hexToRgb(hex1));
const lum2 = luminance(hexToRgb(hex2));
const brightest = Math.max(lum1, lum2);
const darkest = Math.min(lum1, lum2);
return (brightest + 0.05) / (darkest + 0.05);
}
Step 3: Update Badge Compliance and Copy Feedback
Evaluate AA and AAA compliance and update the preview box dynamically.
const bgInput = document.getElementById('bgColor');
const fgInput = document.getElementById('fgColor');
const preview = document.getElementById('swatchPreview');
const badge = document.getElementById('ratioBadge');
function updateCompliance() {
const bg = bgInput.value;
const fg = fgInput.value;
preview.style.backgroundColor = bg;
preview.style.color = fg;
const ratio = getContrast(bg, fg);
const formatted = ratio.toFixed(1) + ':1';
let status = 'Fail';
if (ratio >= 7.0) status = 'AAA Pass';
else if (ratio >= 4.5) status = 'AA Pass';
else if (ratio >= 3.0) status = 'AA Large Pass';
badge.textContent = `${formatted} (${status})`;
badge.className = 'ratio-badge ' + (ratio >= 4.5 ? 'pass' : 'fail');
}
bgInput.addEventListener('input', updateCompliance);
fgInput.addEventListener('input', updateCompliance);
updateCompliance();
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
A trick I use in almost every project: don't fight specificity battles with !important. Leverage modern CSS cascade layers (@layer) and CSS variables as design tokens—it makes dark/light theme switching and responsive tuning effortless.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What is the difference between WCAG 2.1 and WCAG 2.2 contrast rules?
WCAG 2.2 preserves the existing 4.5:1 AA contrast ratio while introducing Success Criterion 2.4.11 (Focus Appearance), mandating at least 3:1 contrast for interactive keyboard focus rings.
Why is OKLCH color space superior for design system palette generation?
OKLCH is perceptually uniform: colors with the same lightness value have identical perceived brightness across all hues, eliminating muddy yellows and blinding blues.
What is considered "Large Text" under WCAG standards?
Large text is defined as at least 18pt (24px) normal weight, or at least 14pt (18.66px) bold weight, requiring a minimum ratio of 3.0:1.
Does dark mode require different contrast considerations than light mode?
Yes, pure white text (#fff) on pure black (#000) causes 21:1 extreme contrast, which can trigger visual halation and eye fatigue for astigmatic users. Prefer off-white (#f1f5f9) on charcoal (#0f172a).
How does APCA (Advanced Perceptual Contrast Algorithm) differ from WCAG 2 contrast?
APCA (planned for WCAG 3) models human spatial vision, text size, and font weight dynamically, avoiding anomalies where dark colors pass standard ratios but remain hard to read.