Created
April 1, 2022 08:37
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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