Palette Contrast Matrix
Test every colour in a five-colour palette against every other at once and see which pairs are safe for text.
AI prompt
Design documentation sheet showing a five-colour palette as a five by five contrast matrix: colour names along the top and left, each cell filled with the background colour and showing a bold Aa in the text colour with the ratio such as 7.3 beneath, small green ticks for pairs above 4.5 and grey dashes for failing pairs, clean grid with thin gutters, flat vector style, plenty of white space. Negative: gradients, shadows, text errors, decorative frames. Reduced motion: static sheet.About this pattern
Definition
A palette is only useful if its colours can sit on each other. A contrast matrix puts every colour as text against every colour as background, with the ratio and a pass mark in each cell, so you can see at a glance which pairs are safe for body text, large text or neither.
When to use it
- Run the matrix before committing to a palette, not after the design is built.
- Mark pairs by what they can do: 4.5 or more for body text, 3 or more for large text and icons, below 3 for decoration only.
- Fix a failing pair by moving one colour lighter or darker, not by adding an outline.
What the controls do
- Three palettes; each cell shows the ratio and a pass, large-only or fail mark.
Motion & timing
- Updates are instant; no animation.
- Reduced motion: nothing to change.
Tips & variations
- Include the real background and surface colours the interface will use.
- Check dark mode as a second palette.
Code of this live preview
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Palette"><button data-p="0" aria-pressed="true">Ink & Sand</button><button data-p="1" aria-pressed="false">Ocean</button><button data-p="2" aria-pressed="false">Forest</button></div><div class="mx" id="mx" role="table" aria-label="Contrast matrix"></div><p class="out">Rows are text colours, columns are backgrounds.</p></div>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.mx{display:grid;grid-template-columns:34px repeat(5,1fr);gap:3px;font-size:10px}.mx .h{display:grid;place-items:center;color:var(--muted);height:34px}.mx .h i{width:16px;height:16px;border-radius:50%;border:1px solid rgba(0,0,0,.15)}
.mx .c{height:40px;border-radius:6px;display:grid;place-items:center;font-weight:700;font-size:14px;position:relative;border:1px solid rgba(0,0,0,.08)}.mx .c small{font-size:9px;font-weight:600;position:absolute;right:3px;bottom:1px}.mx .c.f{opacity:.55}.mx .c.f::after{content:"";position:absolute;inset:0;background:repeating-linear-gradient(135deg,transparent 0 5px,rgba(128,128,128,.25) 5px 6px)}
.out{font-size:11.5px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const P=[['#1d1b19','#4a443d','#a8825a','#e6d8c3','#faf6ee'],['#08203a','#1d5c8a','#2fa0c9','#a9dcee','#f2fafd'],['#10281c','#2d6a45','#7fb069','#cfe5b9','#f6faee']],mx=document.getElementById('mx'),bs=[...document.querySelectorAll('#pre button')];let p=0;
const lum=h=>{const [r,g,b]=[1,3,5].map(i=>parseInt(h.slice(i,i+2),16)/255).map(v=>v<=.03928?v/12.92:Math.pow((v+.055)/1.055,2.4));return .2126*r+.7152*g+.0722*b},cr=(a,b)=>{const x=lum(a),y=lum(b);return (Math.max(x,y)+.05)/(Math.min(x,y)+.05)};
function up(){const c=P[p];let h='<span></span>'+c.map(x=>'<span class="h"><i style="background:'+x+'"></i></span>').join('');c.forEach(fg=>{h+='<span class="h"><i style="background:'+fg+'"></i></span>';c.forEach(bg=>{const r=cr(fg,bg),f=r<3,t=r>=4.5?'text':r>=3?'large':'fail';h+='<span class="c'+(f?' f':'')+'" role="cell" style="background:'+bg+';color:'+fg+'" aria-label="Text '+fg+' on '+bg+': ratio '+r.toFixed(1)+', '+t+'">Aa<small>'+(f?'\u2013':r.toFixed(1))+'</small></span>';});});mx.innerHTML=h;bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));up();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Palette Contrast Matrix — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.mx{display:grid;grid-template-columns:34px repeat(5,1fr);gap:3px;font-size:10px}.mx .h{display:grid;place-items:center;color:var(--muted);height:34px}.mx .h i{width:16px;height:16px;border-radius:50%;border:1px solid rgba(0,0,0,.15)}
.mx .c{height:40px;border-radius:6px;display:grid;place-items:center;font-weight:700;font-size:14px;position:relative;border:1px solid rgba(0,0,0,.08)}.mx .c small{font-size:9px;font-weight:600;position:absolute;right:3px;bottom:1px}.mx .c.f{opacity:.55}.mx .c.f::after{content:"";position:absolute;inset:0;background:repeating-linear-gradient(135deg,transparent 0 5px,rgba(128,128,128,.25) 5px 6px)}
.out{font-size:11.5px;color:var(--muted)}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<div class="bx"><div class="pre" id="pre" role="group" aria-label="Palette"><button data-p="0" aria-pressed="true">Ink & Sand</button><button data-p="1" aria-pressed="false">Ocean</button><button data-p="2" aria-pressed="false">Forest</button></div><div class="mx" id="mx" role="table" aria-label="Contrast matrix"></div><p class="out">Rows are text colours, columns are backgrounds.</p></div>
</div>
<script>
const P=[['#1d1b19','#4a443d','#a8825a','#e6d8c3','#faf6ee'],['#08203a','#1d5c8a','#2fa0c9','#a9dcee','#f2fafd'],['#10281c','#2d6a45','#7fb069','#cfe5b9','#f6faee']],mx=document.getElementById('mx'),bs=[...document.querySelectorAll('#pre button')];let p=0;
const lum=h=>{const [r,g,b]=[1,3,5].map(i=>parseInt(h.slice(i,i+2),16)/255).map(v=>v<=.03928?v/12.92:Math.pow((v+.055)/1.055,2.4));return .2126*r+.7152*g+.0722*b},cr=(a,b)=>{const x=lum(a),y=lum(b);return (Math.max(x,y)+.05)/(Math.min(x,y)+.05)};
function up(){const c=P[p];let h='<span></span>'+c.map(x=>'<span class="h"><i style="background:'+x+'"></i></span>').join('');c.forEach(fg=>{h+='<span class="h"><i style="background:'+fg+'"></i></span>';c.forEach(bg=>{const r=cr(fg,bg),f=r<3,t=r>=4.5?'text':r>=3?'large':'fail';h+='<span class="c'+(f?' f':'')+'" role="cell" style="background:'+bg+';color:'+fg+'" aria-label="Text '+fg+' on '+bg+': ratio '+r.toFixed(1)+', '+t+'">Aa<small>'+(f?'\u2013':r.toFixed(1))+'</small></span>';});});mx.innerHTML=h;bs.forEach(b=>b.setAttribute('aria-pressed',String(+b.dataset.p===p)));}
bs.forEach(b=>b.addEventListener('click',()=>{p=+b.dataset.p;up();}));up();
</script>
</body></html>