<div class="element">
  <h2>Enforcing High Contrast Colors</h2>
</div>

<div class="form">
  <div class="control">
    <label for="r">R:</label>
    <input type="range" min="0" max="255" name="color" step="1" id="r" value="255" />
  </div>
  <div class="control">
    <label for="g">G:</label>
    <input type="range" min="0" max="255" name="color" step="1" id="g" value="0" />
  </div>
  <div class="control">
    <label for="b">B:</label>
    <input type="range" min="0" max="255" name="color" step="1" id="b" value="0" />
  </div>
</div>
@import url("https://fonts.googleapis.com/css?family=Gochi+Hand");

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}


body {
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  background-color: #291642;
  font-family: "Gochi Hand", sans-serif;
  font-size: 130%;
  letter-spacing: 0.1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.element {
  padding: 10vmin 20vmin;
  display: flex;
  justify-content: center;
  background-image: radial-gradient(circle at center, hsl(var(--h) calc(var(--s) * 1%) calc(var(--l) * 1%)), #291642),
    linear-gradient(to bottom, #291642, #291642);
  background-repeat: no-repeat;
  background-position: center;
  background-clip: padding-box;
  border-radius: 8px;
  background-blend-mode: multiply, screen;
  margin-bottom: 10px;
}

h2 {
  border-radius: 2vmin;
  padding: 5vmin;
  white-space: nowrap;
}

.form {
  display: flex;
  align-items: center;
  color: #fff;
}

.control {
  margin: 4vmin;
  display: flex;
  align-items: center;
  font-size: 3rem;
}

:root {
    --r: 255;
    --g: 0;
    --b: 0;
    --a11yColor: calc((((var(--r) * 299 + var(--g) * 587 + var(--b) * 114) / 1000) - 128) * -1000);
}


h2 {
    color: rgb(var(--a11yColor) var(--a11yColor) var(--a11yColor));
    background-color: rgb(var(--r) var(--g) var(--b));
}
View Compiled
const inputs = document.querySelectorAll("input");

const color = [...inputs].map((input, index) => {
  input.addEventListener("change", function (e) {
    handleUpdate(e, index);
  });
  return input.value;
});

function handleUpdate(e, index) {
  color[index] = e.target.value;

  document.documentElement.style.setProperty(
    `--${e.target.id}`,
    `${e.target.value}`
  );
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.