Dev Helpers
← Back

Ink contrast ratio

Import and render <InkContrastRatio /> in a React project.

Open this example in the interactive library

Live preview

Source code

// components/InkContrastRatio.tsx

export default function InkContrastRatio({ hex = "#bc805a" }: { hex?: string } = {}) {
  const c = [1, 3, 5]
    .map((i) => parseInt(hex.slice(i, i + 2), 16) / 255)
    .map((x) => (x <= 0.04045 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4));
  const lum = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];
  return (
    <output>
      White on {hex}: {(1.05 / (lum + 0.05)).toFixed(2)} to 1
    </output>
  );
}