Color contrast tool
Set a text and background color in hex, RGB, or HSL — all three stay in sync — and check the WCAG luminance ratio.
This is a line of big text
This is a line of really small text. It is much harder to read if color contrast is insufficient.
—
WCAG 2.1 thresholds: 3:1 large text · 4.5:1 normal text (AA) · 7:1 enhanced (AAA). Reference
';}
function buildCol(t,title){var h='';h+='';[['r','R',255],['g','G',255],['b','B',255]].forEach(function(a){h+=channelRow(t,'rgb',a[0],a[1],a[2]);});h+='
';[['h','H',360],['s','S',100],['l','L',100]].forEach(function(a){h+=channelRow(t,'hsl',a[0],a[1],a[2]);});h+='
';$('col-'+t).innerHTML=h;}
function setVal(base,v){var s=root.querySelector('#ctd-'+base+'-s'),n=root.querySelector('#ctd-'+base+'-n');if(s)s.value=v;if(n)n.value=v;}
function syncAll(t,skipHex){var c=state[t].rgb,d=state[t].hsl;setVal(t+'-rgb-r',c.r);setVal(t+'-rgb-g',c.g);setVal(t+'-rgb-b',c.b);setVal(t+'-hsl-h',d.h);setVal(t+'-hsl-s',d.s);setVal(t+'-hsl-l',d.l);var hex=rgbToHex(c.r,c.g,c.b);if(!skipHex)$(t+'-hex').value=hex;$(t+'-sw').style.background=hex;updateOutput();}
function onChannel(t,model,k,val){val=parseInt(val,10);if(isNaN(val))val=0;var max=model==='hsl'?(k==='h'?360:100):255;val=clamp(val,0,max);state[t][model][k]=val;if(model==='rgb')state[t].hsl=rgbToHsl(state[t].rgb.r,state[t].rgb.g,state[t].rgb.b);else state[t].rgb=hslToRgb(state[t].hsl.h,state[t].hsl.s,state[t].hsl.l);syncAll(t);}
function onHex(t,val){var rgb=hexToRgb(val);if(!rgb)return;state[t].rgb=rgb;state[t].hsl=rgbToHsl(rgb.r,rgb.g,rgb.b);syncAll(t,true);}
function wire(t){$(t+'-hex').addEventListener('input',function(e){onHex(t,e.target.value);});[['rgb','r'],['rgb','g'],['rgb','b'],['hsl','h'],['hsl','s'],['hsl','l']].forEach(function(d){['s','n'].forEach(function(sfx){root.querySelector('#ctd-'+t+'-'+d[0]+'-'+d[1]+'-'+sfx).addEventListener('input',function(e){onChannel(t,d[0],d[1],e.target.value);});});});}
function updateOutput(){var tc=rgbToHex(state.text.rgb.r,state.text.rgb.g,state.text.rgb.b);var bc=rgbToHex(state.bg.rgb.r,state.bg.rgb.g,state.bg.rgb.b);$('preview').style.background=bc;$('big').style.color=tc;$('small').style.color=tc;var ratio=contrast(state.text.rgb,state.bg.rgb);$('rnum').textContent=(Math.round(ratio*100)/100).toFixed(2)+' : 1';var v=$('verdict'),lab,bg,fg;if(ratio>=7){lab='Passes AAA \u00b7 good for all text';bg='#e7f6ec';fg='#1b7f3b';}else if(ratio>=4.5){lab='Passes AA \u00b7 good for all text';bg='#e7f6ec';fg='#1b7f3b';}else if(ratio>=3){lab='Large text only';bg='#fef3cd';fg='#8a6d1b';}else{lab='Insufficient contrast';bg='#fde8e8';fg='#b42318';}v.textContent=lab;v.style.background=bg;v.style.color=fg;}
buildCol('text','Text color');buildCol('bg','Background color');
wire('text');wire('bg');
syncAll('text');syncAll('bg');
})();
Leave a Reply