Color Contrast Checker: how it works and how to build one
Check text and background colors against WCAG AA and AAA. This guide explains what happens behind the button and shows the code, so you can build your own.
What it does
Enter a text color and a background color to get the contrast ratio and a pass or fail for normal text, large text and UI parts under WCAG 2.2. If it fails, it suggests the nearest color that passes.
When to use it
- Checking brand colors for accessibility
- Design reviews
- Dark mode palettes
How to use it
- Pick or type the two colors.
- Read the ratio and pass/fail results.
- Use the suggested fix if needed.
Try it free in your browser, nothing is uploaded: https://eskillsfy.com/tools/contrast-checker
How it works
WCAG contrast compares the relative luminance of two colors: (L1 + 0.05) / (L2 + 0.05), where luminance weights green most and blue least, matching how eyes see brightness. 4.5:1 passes AA for normal text, 3:1 for large text.
To suggest a fix, the tool darkens (or lightens) the text color step by step until the ratio passes.
Build it yourself
Here is the core of the tool. It has no dependencies unless noted, so you can paste it into any TypeScript or JavaScript project.
contrast.ts (ts)
const channel = (c: number) => { c /= 255; return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; };
export const luminance = ([r, g, b]: number[]) => 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
export function contrast(a: number[], b: number[]) {
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
return (hi + 0.05) / (lo + 0.05);
}
contrast([118, 118, 118], [255, 255, 255]); // 4.54 → passes AA
Make it production-ready
- Check a whole palette as a grid.
- Add APCA, the contrast method planned for WCAG 3.
Questions
What ratio do I need?
4.5:1 for normal text and 3:1 for large text and icons to pass AA. AAA needs 7:1 for normal text.
Free, runs in your browser, nothing is uploaded.