Skip to content

Instantly share code, notes, and snippets.

@attitude
Created April 1, 2022 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attitude/c444c8f2e90ff4f097d18240d47d09d2 to your computer and use it in GitHub Desktop.
Save attitude/c444c8f2e90ff4f097d18240d47d09d2 to your computer and use it in GitHub Desktop.
Reverse function of WCAG 2.0 that returns ideal Luminance of colour at current index (to be used with Array.map)
// Contrast ratio formula:
// (L1 + 0.05) / (L2 + 0.05), where
// L1 is the relative luminance of the lighter of the foreground or background colors, and
// L2 is the relative luminance of the darker of the foreground or background colors.
function idealLuminanceAtIndex(index: number, colorsCount: number) {
const exponent = (1 - (index * 1 / (colorsCount - 1)))
return (Math.pow(colorsCount, exponent) * 0.05 - 0.05) / (colorsCount - 1) * 20
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment