Skip to content
animorio
IT
Back to the library

Contrast checker

HoverGSAP, CSS onlyMediumCompressed weight: Demo 6.8 KBGSAP 28 KBFreeDownload the zip

The contrast checker of a design tool: drag the lightness of the text colour and the ratio counts live, the three WCAG marks flip one at a time and the thresholds are drawn on the bar itself. One key swaps text and background, turning the preview over.

The code

A folder that runs on its own: index.html, style.css, script.js and the libraries from a CDN. The React and Vue tabs hold the same demo as a ready component, which switches its animations off when it leaves the page.

<!doctype html>
<!--
  "Contrast checker" from Animorio · https://animorio.com
  License: free on your own sites and your clients’ sites, paid work included.
  Not for resale, redistribution, templates or code collections · https://animorio.com/en/license/
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Contrast checker</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/mona-sans@5.3.0/wdth.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <main class="stage">
    <!-- a contrast checker for a design tool: drag the lightness of the text colour and the ratio counts,
         the three WCAG marks flip as their thresholds pass, and the thresholds are drawn on the slider itself -->
    <section class="cc" data-cc aria-label="Contrast checker">
      <div class="cc__flip">
        <div class="cc__preview" data-preview>
          <span class="cc__aa" aria-hidden="true">Aa</span>
          <span class="cc__lines"><b>Readable at a glance</b><span>Small print needs more contrast than a headline.</span></span>
        </div>
      </div>

      <div class="cc__read">
        <span class="cc__ratio"><b data-ratio>1.00</b><i>:1</i></span>
        <span class="cc__roll cc__verdict" aria-live="polite"><span data-verdict></span></span>
      </div>

      <ul class="cc__marks" data-marks>
        <li data-mark="3"><svg viewBox="0 0 16 16" aria-hidden="true"><path class="ok" d="M3.500 8.300 6.700 11.300 12.700 4.800"/><path class="no" d="M4.500 4.500l7 7M11.500 4.500l-7 7"/></svg><b>AA large</b><span>3:1</span></li>
        <li data-mark="4.5"><svg viewBox="0 0 16 16" aria-hidden="true"><path class="ok" d="M3.500 8.300 6.700 11.300 12.700 4.800"/><path class="no" d="M4.500 4.500l7 7M11.500 4.500l-7 7"/></svg><b>AA</b><span>4.5:1</span></li>
        <li data-mark="7"><svg viewBox="0 0 16 16" aria-hidden="true"><path class="ok" d="M3.500 8.300 6.700 11.300 12.700 4.800"/><path class="no" d="M4.500 4.500l7 7M11.500 4.500l-7 7"/></svg><b>AAA</b><span>7:1</span></li>
      </ul>

      <div class="cc__slider" data-slider role="slider" tabindex="0" aria-label="Lightness of the text colour" aria-valuemin="0" aria-valuemax="100">
        <div class="cc__track" data-track><div class="cc__ticks" data-ticks></div></div>
        <i class="cc__thumb" data-thumb></i>
      </div>

      <footer class="cc__foot">
        <span class="cc__well"><i data-well-a></i><span>Text<b data-hex-a></b></span></span>
        <button class="cc__swap" type="button" data-swap aria-label="Swap text and background">
          <svg viewBox="0 0 18 18" aria-hidden="true"><path d="M4 6.500h10l-2.800-2.800M14 11.500H4l2.800 2.800"/></svg>
        </button>
        <span class="cc__well"><i data-well-b></i><span>Background<b data-hex-b></b></span></span>
      </footer>
    </section>

    <svg class="ghost" data-ghost viewBox="0 0 24 24" aria-hidden="true"><path d="M5 3.200 19.200 11l-6.300 1.600-3.300 5.900Z"></path></svg>
  </main>

  <script src="https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/gsap.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

Playground

Change values, colours and text with the controls and watch the animation respond live. Or write in the code yourself. Then download the folder with your changes.

25 values to change

Works with Claude, Claude Code, ChatGPT, Cursor, VS Code, Windsurf, Codex, Gemini CLI

Or ask your AI assistant for it

With the Animorio connector, Claude Code, ChatGPT, Cursor or VS Code take it from here, guide included, and fit it into your project. Paste this request:

Take “Contrast checker” from Animorio and fit it into my site
Platform licenseFree on your sites and your clients’ sites. Want it inside a website builder, a template platform or a client dashboard? There’s a license for agencies and platforms.

What it does

On a warm grey ground, at the top the preview: a panel in the background colour with a large “Aa”, a headline and a line of small text, in the text colour. Below, the ratio in large type (3.62:1, say), digits growing heavier as contrast climbs, and on the right the verdict rolling: “Fails”, “Large text only”, “Passes AA”, “Passes AAA”. Then three marks, “AA large 3:1”, “AA 4.5:1” and “AAA 7:1”: once its threshold is passed a mark turns dark with a check and gives a small spring, when lost it goes back to an outline with a cross. The bar shows every lightness of that hue, darkest to lightest, and carries three thin lines with a label: the exact points where each threshold is crossed, found by the script. Drag the knob (or use the arrows) and everything follows: the knob chases the finger on a spring and shows the current colour inside, the two hex codes change below. The key with the two arrows swaps text and background: the preview turns over and comes back inverted, while the ratio stays the same.

How it works

The ratio, as WCAG wants it

From HSL to sRGB, then relative luminance (each channel linearised and weighted 0.2126, 0.7152, 0.0722) and the ratio of the lighter to the darker, each plus 0.05. Ten lines, no library.

The thresholds on the bar

At start the script tries lightness from 0 to 100 in quarter steps and marks where the ratio crosses 3, 4.5 and 7. Whoever drags already knows where to go, without trial and error.

A spring between finger and colour

The wanted value changes at once, the shown one chases it on a near-critical spring on GSAP’s ticker: knob, colours, ratio and marks all read the shown one, so even a far click on the bar arrives gliding.

A real slider

role=slider, arrows, Shift for steps of ten, Home and End, and in aria-valuetext the lightness, the ratio and the verdict in words. The pointer is captured, so you can keep dragging outside the bar.

Values to change

NameFileDefaultWhat it changes
HUEscript.js232The hue of the text colour, in degrees (232 = blue).
SATscript.js78How saturated the text colour is, in percent.
STARTscript.js62The lightness the text colour starts from (0 to 100).
PAPERscript.js#ffffffThe other colour: the background, until they are swapped.
FOLLOWscript.js1How readily the knob follows the value: 0.4 = lazy, 2 = immediate.
LOOPscript.jstrueLeft alone, the demo’s pointer darkens the text, lightens it and swaps the two colours.

Try them in the Playground

When to use it

In a design tool, in the colour panel of a site builder, on a design system page where brand colours are chosen; or in an article explaining accessibility, to let people try it instead of reading about it.

On phones

The same tool by finger: the bar does not scroll the page while you drag, the knob is 36 pixels wide and the marks sit on one row. Sizes also look at the window’s height, so preview, numbers and bar fit together.

With reduced motion

Knob and colours go straight to the chosen value, marks change without a spring, the verdict without rolling and the swap inverts the colours without turning the preview over. The demo does not move by itself.

Pitfalls

  • The ratio is computed on the colour rounded to integers 0 to 255, the same one read in the hex code: computed on decimals, right at a threshold it would say one thing and the copied colour another.
  • Swapping text and background leaves the ratio unchanged (the formula is symmetric), but not the legibility: light on dark looks thinner. The tool gives the threshold, the eye is still needed.
  • The threshold lines use mix-blend-mode: difference to show on both the dark and the light part of the bar.
  • touch-action: none on the bar, or on a phone the finger’s first move scrolls the page and the drag is cut off.

Libraries

GSAP 3.15.0, Mona Sans (font variabile) 5.3.0.

Compressed with gzip, the three demo files weigh 6.8 KB; the libraries: GSAP 28 KB.

Testing

Tested on 20 November 2026

  • Desktop: Done
  • Phone: Done
  • Reduced motion: Done
  • Safari on iPhone: To do

License

Use it in your own projects and in your clients’ projects. Do not resell it or redistribute it as is. GSAP stays under its own Standard license. Full license.

All animations