GrepBoard/Manuals
605 manuals ·

Check an AI-Picked Palette for WCAG Contrast Ratio

·Tested on Python 3.12 · WCAG 2.2 · Claude Sonnet (claude -p) 2026-10·Test scope ↓·7 steps

Port the WCAG relative-luminance formula to a short Python script, test an AI-generated palette against 4.5:1 and 3:1, and re-prompt only the pairs that fail.

An AI hands you a palette as hex codes and never tells you whether the text is readable. Port the WCAG 2.2 contrast formula to a script, check the color pairs, and send only the failures back.

[1] Know the thresholds. WCAG 2.2 SC 1.4.3 (AA) requires a contrast ratio of at least 4.5:1 between text and background; large text needs 3:1. Large means 18 point or larger, or 14 point bold. SC 1.4.11 asks for 3:1 against adjacent colors for UI components such as button borders and icons.

[2] Port the formula. It follows the spec definition: divide each 8-bit channel by 255, linearize, take relative luminance as 0.2126 R + 0.7152 G + 0.0722 B, then compute (L1 + 0.05) / (L2 + 0.05) with the lighter color as L1.

contrast.py
import json, sys def lin(c8):    s = c8 / 255    return s / 12.92 if s <= 0.04045 else ((s + 0.055) / 1.055) ** 2.4 def lum(color):    h = color.lstrip("#")    r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))    return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b) def ratio(a, b):    hi, lo = sorted((lum(a), lum(b)), reverse=True)    return (hi + 0.05) / (lo + 0.05) failed = 0for p in json.load(open(sys.argv[1])):    need = p.get("min", 4.5)    r = ratio(p["fg"], p["bg"])    ok = r >= need          # compare without rounding    failed += not ok    print(f'{p["name"]:22} {p["fg"]} on {p["bg"]}  {r:5.2f}:1  need {need}  {"pass" if ok else "FAIL"}')sys.exit(1 if failed else 0)

[3] Sanity-check with black and white. The spec says ratios range from 1:1 to 21:1.

console
$ python3 -c "exec(open('contrast.py').read().rsplit('failed = 0',1)[0]); print(ratio('#000000','#ffffff'), ratio('#ffffff','#ffffff'))"21.0 1.0

[4] List the pairs as JSON and run the script. Without min the threshold is 4.5; add "min": 3 for large text or UI elements. The exit code is 0 or 1, so it works in CI.

pairs.json
[  {"name": "gray hint", "fg": "#999999", "bg": "#ffffff"},  {"name": "white on blue", "fg": "#ffffff", "bg": "#4a90e2"},  {"name": "white on orange", "fg": "#ffffff", "bg": "#ff9800"},  {"name": "link", "fg": "#0b5fff", "bg": "#f4f5f7"}]
console
$ python3 contrast.py pairs.jsongray hint              #999999 on #ffffff   2.85:1  need 4.5  FAILwhite on blue          #ffffff on #4a90e2   3.29:1  need 4.5  FAILwhite on orange        #ffffff on #ff9800   2.16:1  need 4.5  FAILlink                   #0b5fff on #f4f5f7   4.70:1  need 4.5  pass$ echo $?1

The common light gray #999999 and white text on a sky-blue button both fail. white on blue at 3.29 passes only for large text.

[5] Check a palette an AI actually produced. Ask Claude Sonnet (claude -p) for eight light-theme pairs as JSON only.

prompt1.txt
Give a light-theme UI palette for a note-taking app as JSON only (no code fence, no prose): an array of 8 objects {"name","fg","bg"} for text/background pairs actually used in the UI (body text, muted caption, link, primary button label, warning badge, success badge, placeholder, disabled label). Use 6-digit hex. Make it look soft and modern.
console
$ claude -p --model sonnet < prompt1.txt > ai1.json

Add "min": 3 to the disabled-label entry by hand. SC 1.4.3 does not require it, but 3:1 is a reasonable floor.

console
$ python3 contrast.py ai1.jsonBody text              #2B2A33 on #FAF9F7  13.47:1  need 4.5  passMuted caption          #5F5D6B on #FAF9F7   6.11:1  need 4.5  passLink                   #4A44C6 on #FAF9F7   6.80:1  need 4.5  passPrimary button label   #FFFFFF on #5B52D9   5.75:1  need 4.5  passWarning badge          #7A4A00 on #FCEBC8   6.36:1  need 4.5  passSuccess badge          #1E6B45 on #D9F2E4   5.48:1  need 4.5  passPlaceholder            #6E6C78 on #F1EFEC   4.49:1  need 4.5  FAILDisabled label         #8A8893 on #ECEAE6   2.90:1  need 3  FAIL

Six of eight pass. The placeholder prints as 4.49 but is really 4.4856. The Understanding document says computed values must not be rounded, and gives 4.499:1 as an example that does not meet 4.5:1.

[6] Send back only the failures. Leave the passing pairs out, pin the backgrounds, and change the foreground only. Give the numbers and the threshold in the prompt.

prompt2.txt
These text/background pairs failed a WCAG 2.2 contrast check (ratio computed from relative luminance, no rounding):- Placeholder: fg #6E6C78 on bg #F1EFEC = 4.49:1, needs at least 4.5:1- Disabled label: fg #8A8893 on bg #ECEAE6 = 2.90:1, needs at least 3:1 Keep each background as it is. Change only the foreground, as little as possible so the look stays soft. Reply with JSON only (no code fence, no prose): an array of {"name","fg","bg"}.
console
$ claude -p --model sonnet < prompt2.txt > ai2.json$ cat ai2.json[{"name":"Placeholder","fg":"#6D6B77","bg":"#F1EFEC"},{"name":"Disabled label","fg":"#868490","bg":"#ECEAE6"}]

[7] Run the same script on the reply. Do not take the model's word that it now passes. Add "min": 3 to the disabled label again.

console
$ python3 contrast.py ai2.jsonPlaceholder            #6D6B77 on #F1EFEC   4.55:1  need 4.5  passDisabled label         #868490 on #ECEAE6   3.06:1  need 3  pass$ echo $?0

Each color moved one digit darker. If a pair still fails, repeat the check and re-prompt; this one passed the first time.

If your colors live in a JSON token file, run this check after the build. To build tokens into CSS variables, see Build design tokens JSON into CSS variables.

#wcag#contrast#color#claude