Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                .display
  #hex.display__cell.display__cell--background #EB0000
  #white.display__cell.display__cell--foreground.display__cell--white 4.5
  #black.display__cell.display__cell--foreground.display__cell--black 4.5
form.form
  label.form__field(for="hue")
    .form__label Hue
    input.form__slider(type="range", name="hue", min="0", max="360", step="15", value="0")
    input.form__number(type="number", name="hue", min="0", max="360", step="15", value="0")
  label.form__field(for="saturation")
    .form__label Saturation
    input.form__slider(type="range", name="saturation", min="0", max="100", step="5", value="100")
    input.form__number(type="number", name="saturation", min="0", max="100", step="5", value="100")
  label.form__field(for="ratio")
    .form__label Ratio
    input.form__slider(type="range", name="ratio", min="-21", max="21", step="0.5", value="0")
    input.form__number(type="number", name="ratio", min="-21", max="21", step="0.5", value="0")
article.content
  h1.content__title HSR Color Space
  p.content_body This is an experimental color space. The hue and saturation are the same as HSL.
  p.content_body The ratio is defined as the contrast ratio against black minus the contrast ratio against white.
  p.content_body -21 is black, 0 is roughly equal contrast, and 21 is white.
  p.content_body 0 is AA for both black and white. -4 is AAA for white. 4 is AAA for black.
  
              
            
!

CSS

              
                *,
*::before,
*::after {
  box-sizing: inherit;
}

html {
  box-sizing: border-box;
}

body {
  background-color: white;
  color: black;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  line-height: 1.5rem;
  margin: 0;
  text-align: left;
  width: 100%;
}

.display {
  --hex: #000;
  display: flex;
  width: 100%;
}

.display__cell {
  align-items: center;
  flex: 1;
  display: flex;
  justify-content: center;
  height: 4rem;
}

.display__cell--background {
  background-color: var(--hex);
}

.display__cell--foreground {
  color: var(--hex);
}

.display__cell--white {
  background-color: white;
}

.display__cell--black {
  background-color: black;
}

.form__field {
  height: 3rem;
  display: flex;
}

.form__label {
  font-weight: bold;
  padding: 0.75rem 0.25rem;
  width: 6rem;
}

.form__slider {
  flex: 1;
  margin: 0.25rem;
}

.form__number {
  border: 0.0625rem solid rgba(0, 0, 0, 0.16);
  border-radius: 0.125rem;
  font-size: inherit;
  text-align: left;
  margin: 0.25rem;
  padding: 0.5rem;
  width: 4rem;
}

.form__number:invalid {
  border-color: red;
}

.content {
  padding: 0rem 0.25rem;
}

.content_title {
  font-size: 1.25rem;
  line-height: 1.5rem;
  margin: 0;
}

.content_body {
  margin-bottom: 1.5rem;
}
              
            
!

JS

              
                interface RGB {
  readonly r: number;
  readonly g: number;
  readonly b: number;
}

interface HSL {
  readonly h: number;
  readonly s: number;
  readonly l: number;
}

const fromHue = (p: number, q: number, t: number): number => {
  if (t < 0) t += 1;
  if (t > 1) t -= 1;
  if (t < 1 / 6) return p + (q - p) * 6 * t;
  if (t < 1 / 2) return q;
  if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
  return p;
};

const toRGB = (hsl: HSL): RGB => {
  const h = hsl.h / 360;
  const s = hsl.s / 100;
  const l = hsl.l / 100;
  let r, g, b;

  if (s == 0) {
    r = g = b = l;
  } else {
    const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
    const p = 2 * l - q;
    r = fromHue(p, q, h + 1 / 3);
    g = fromHue(p, q, h);
    b = fromHue(p, q, h - 1 / 3);
  }

  r = Math.round(r * 255);
  g = Math.round(g * 255);
  b = Math.round(b * 255);

  return { r, g, b };
};

const encode = (value: number): string => {
  let code = value.toString(16);
  return code.length < 2 ? `0${code}` : code;
};

const toHex = ({ r, g, b }: RGB): string =>
  `#${encode(r)}${encode(g)}${encode(b)}`;

const contrastRatio = (a: RGB, b: RGB): number => {
  const arl = relativeLuminance(a) + 0.05;
  const brl = relativeLuminance(b) + 0.05;
  return arl > brl ? arl / brl : brl / arl;
};

const relativeLuminance = (color: RGB): number =>
  0.2126 * lRGB(color.r) + 0.7152 * lRGB(color.g) + 0.0722 * lRGB(color.b);

const lRGB = (value: number): number =>
  value > 0.03928
    ? ((sRGB(value) + 0.055) / 1.055) ** 2.4
    : sRGB(value) / 12.92;

const sRGB = (value: number): number => value / 255;

const BLACK: RGB = { r: 0, g: 0, b: 0 };
const WHITE: RGB = { r: 255, g: 255, b: 255 };

const STATE = {
  hue: 0,
  saturation: 100,
  ratio: 0
};

const search = (min: number = 0, max: number = 100, prev: string = ""): RGB => {
  const lightness = (min + max) / 2;
  const hsl: HSL = { h: STATE.hue, s: STATE.saturation, l: lightness };
  const rgb = toRGB(hsl);
  const hex = toHex(rgb);
  if (hex === prev) {
    return rgb;
  }
  const black = contrastRatio(BLACK, rgb);
  const white = contrastRatio(WHITE, rgb);
  const ratio = black - white;
  return ratio > STATE.ratio
    ? search(min, lightness, hex)
    : search(lightness, max, hex);
};

const rerender = () => {
  const rgb = search();
  const hex = toHex(rgb);
  document.getElementById("hex").textContent = toHex(rgb);
  document.getElementById("white").textContent = contrastRatio(WHITE, rgb);
  document.getElementById("black").textContent = contrastRatio(BLACK, rgb);
  document.querySelector(".display").style.setProperty("--hex", hex);
};

document.querySelector(".form").addEventListener("change", evt => {
  const target = evt.target as HTMLInputElement;
  const name = target.getAttribute("name") as "hue" | "saturation" | "ratio";
  STATE[name] = parseFloat(target.value);
  document
    .querySelectorAll(`input[name=${name}]`)
    .forEach((el: HTMLInputElement) => {
      el.value = target.value;
    });
  rerender();
});

rerender();
              
            
!
999px

Console