|
<!doctype html> |
|
<html lang="en"> |
|
<head> |
|
<meta charset="utf-8" /> |
|
<title>WCAG Contrast Ratio Example</title> |
|
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
|
<script type="module"> |
|
import { contrastRatio, wcagStatus } from "./contrast.js"; |
|
|
|
const foregroundInput = document.querySelector("#foreground"); |
|
const backgroundInput = document.querySelector("#background"); |
|
const preview = document.querySelector("#preview"); |
|
const output = document.querySelector("#output"); |
|
|
|
function update() { |
|
const foreground = foregroundInput.value; |
|
const background = backgroundInput.value; |
|
|
|
const ratio = contrastRatio(foreground, background); |
|
const status = wcagStatus(ratio); |
|
|
|
preview.style.color = foreground; |
|
preview.style.background = background; |
|
|
|
output.textContent = [ |
|
`Contrast ratio: ${ratio.toFixed(2)}:1`, |
|
`Normal text AA: ${status.normalAA ? "pass" : "fail"}`, |
|
`Normal text AAA: ${status.normalAAA ? "pass" : "fail"}`, |
|
`Large text AA: ${status.largeAA ? "pass" : "fail"}`, |
|
`Large text AAA: ${status.largeAAA ? "pass" : "fail"}`, |
|
].join("\n"); |
|
} |
|
|
|
foregroundInput.addEventListener("input", update); |
|
backgroundInput.addEventListener("input", update); |
|
|
|
update(); |
|
</script> |
|
<style> |
|
body { |
|
font-family: system-ui, sans-serif; |
|
max-width: 720px; |
|
margin: 40px auto; |
|
padding: 0 16px; |
|
line-height: 1.5; |
|
} |
|
|
|
label { |
|
display: block; |
|
margin: 12px 0; |
|
} |
|
|
|
#preview { |
|
padding: 24px; |
|
border-radius: 10px; |
|
margin: 20px 0; |
|
font-size: 20px; |
|
font-weight: 600; |
|
} |
|
|
|
pre { |
|
padding: 16px; |
|
border: 1px solid #ddd; |
|
border-radius: 8px; |
|
} |
|
</style> |
|
</head> |
|
<body> |
|
<h1>WCAG contrast ratio example</h1> |
|
|
|
<label> |
|
Foreground |
|
<input id="foreground" type="color" value="#ffffff" /> |
|
</label> |
|
|
|
<label> |
|
Background |
|
<input id="background" type="color" value="#111111" /> |
|
</label> |
|
|
|
<div id="preview"> |
|
Preview text |
|
</div> |
|
|
|
<pre id="output"></pre> |
|
|
|
<p> |
|
Full tool: |
|
<a href="https://altrepo.net/color/color-contrast-checker/" target="_blank" rel="noopener"> |
|
AltRepo Color Contrast Checker |
|
</a> |
|
</p> |
|
</body> |
|
</html> |