Checking colour contrast by API
WebAIM's contrast checker has a free API: add &api to its URL with fcolor and bcolor and it returns the contrast ratio and AA and AAA pass or fail as JSON. The WCAG 2.x formula is also short enough to run in your own code. Our Color Converter and Contrast Checker API returns the ratio and every AA and AAA result, says whether black or white text reads better, and converts the colour to hex, RGB, HSL, HWB and CMYK, for $0.002 per call.
Try it free Color Converter & Contrast API — hex, RGB, HSL, WCAG, $0.002 · $0.002 / call
The thresholds
Example: #1e90ff (dodger blue) on white is about 3.23:1. It passes for large text and UI parts and fails for body text.
| Check | Minimum ratio |
|---|---|
| AA, normal text | 4.5:1 |
| AA, large text (about 24 px, or 18.7 px bold) | 3:1 |
| AA, UI parts and graphics | 3:1 |
| AAA, normal text | 7:1 |
| AAA, large text | 4.5:1 |
Free: WebAIM, or the formula
# WebAIM, hex without #
curl "https://webaim.org/resources/contrastchecker/?fcolor=1E90FF&bcolor=FFFFFF&api"
# The WCAG 2.x formula in Python
def lum(hexc):
def ch(v):
v /= 255
return v / 12.92 if v <= 0.04045 else ((v + 0.055) / 1.055) ** 2.4
r, g, b = (int(hexc[i:i + 2], 16) for i in (0, 2, 4))
return 0.2126 * ch(r) + 0.7152 * ch(g) + 0.0722 * ch(b)
a, b = sorted([lum("1e90ff"), lum("ffffff")], reverse=True)
print(round((a + 0.05) / (b + 0.05), 2)) # 3.24 (rounded; WCAG tools often truncate)
What the API adds
- Input in any CSS form: hex, rgb(), hsl(), hwb(), cmyk() or one of the 148 CSS colour names.
- The ratio cut down, not rounded up, to two decimals, so a colour at 4.49 never shows as 4.5.
- All five results above plus better_text_color: black or white, whichever has more contrast on your background.
- The colour in hex, RGB, HSL, HWB and CMYK, its nearest CSS name, and a 10-step light-to-dark scale.
curl -i -X POST https://iamai.markets/api/x402/services/h2LjIAqyb-72 \
-H "Content-Type: application/json" \
-d '{"color": "#1e90ff", "background": "white"}'
# contrast: {"ratio": 3.23, "aa_normal_text": false, "aa_large_text": true, ...}
Limits
It uses the WCAG 2.x formula, which is what audits and most laws refer to today; it doesn't compute APCA, the method drafted for WCAG 3. Transparency is ignored for the contrast check, so blend semi-transparent colours onto their background first. CMYK is a plain formula without a print profile. A contrast pass is one part of accessibility, not a full audit.
The product
Questions
Is the WebAIM API free to use?
It is a public URL on WebAIM's free checker. For heavy automated use, run the formula yourself.
Does contrast depend on font size?
Yes. Large text needs 3:1 for AA instead of 4.5:1, because bigger letters are easier to read.
Can I check a whole website?
Not with this API; it checks one colour pair per call. Page-level audits need a browser-based accessibility tool.
Related
Updated 2026-12-02
