<h1>CSS level 4 color contrast adjuster just with custom properties and calc()</h1>

  <label><strong>Background lightness</strong></label><input type="range" name="--bg-l" value="35"/><code></code>
  <label>Background hue</label><input type="range" name="--bg-h" value="180" min="0" max="360"/><code></code>
  <label>Background saturation</label><input type="range" name="--bg-s" value="100"/><code></code>
  <label><strong>Contrast ratio</strong></label><input type="range" name="--contrast-ratio" value="30"/><code></code>
  The CSS level 4 contrast adjuster mimicked with just custom properties and calc().
  While it's pretty straightforward to mimick the hue, saturation and
  lightness adjusters, color contrast requires some "serious" math.
  Results are not perfect, but very much usable IMHO.
  Syntax of course is even more bloated than for the simple adjusters mentioned above.
body {
  /* Actual values set from JS */
  --bg-h: 0; --bg-s: 0; --bg-l: 0; /* Background hue, saturation, lightness. */
  /* The ratio for the contrast() adjuster,
     see https://www.w3.org/TR/css-color-4/#modifying-colors */
  --contrast-ratio: 0%;
h1 {
  /* If you see a red foreground,
     you might have a syntax error somwehere below this. */
  color: hsl(0, 100%, 50%);

  /* This is how colors need to be specified.
     Conversion of saturation and lightness to percentage done here
     to enable prior calculations. */
  background-color: hsl(
    calc(var(--bg-s) * 1%),
    calc(var(--bg-l) * 1%)

  /* The foreground color with contrast adjuster. */
  color: hsl(
    calc(var(--bg-s) * 1%),
    /* Calculate the lightness:
       1. Take the 1/x function
       2. Turn it upside down: -1/x
       2. Adjust it to the x and y axis, but avoid -Infinity being hit,
          since it tends to act funny when used in further calculations
          (make sure the input lightness value is rounded to at most 2 decimal points):
       3. Make it less curved, so the y-axis makes sense between 0 and 100:
       4. Adjust the curve with the contrast ratio,
          but make sure we're never multiplying by 0: (-2500*(c/100+1))/(x-49.999)+x
    calc((-2500 * (var(--contrast-ratio) * 1% / 100 + 1%)) / (var(--bg-l) - 49.999) + var(--bg-l) * 1%)

/* A few demo styles. */
body {
  font-family: system, -apple-system, ".SFNSText-Regular", "San Francisco", "Roboto", "Segoe UI", "Helvetica Neue", "Lucida Grande", sans-serif;
form {
  display: grid;
  grid-template-columns: minmax(min-content, max-content) max-content max-content;
  grid-gap: .5em;
  align-items: center;
code { text-align: right }
h1 {
  font-weight: bold;
  font-size: 26px;
  padding: 1em;
const updateStyle = element => document.body.style.setProperty(element.name, element.value) || (element.nextElementSibling.innerText = element.value)
Array.from(document.querySelectorAll('input')).forEach(input => updateStyle(input))
document.addEventListener('input', ({ target }) => requestAnimationFrame(() => updateStyle(target)))

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.