Last active
February 3, 2020 16:39
-
-
Save danhollick/614833cf0916b6b4c5f57cea2cb5aafe to your computer and use it in GitHub Desktop.
Fix contrast calculation
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
function calculateContrast(foreground, alpha, backgound) { | |
const foregroundLuminance = calculateLuminance(foreground) + 0.05 | |
const backgroundLuminance = calculateLuminance(backgound) + 0.05 | |
let contrast = foregroundLuminance / backgroundLuminance | |
if (backgroundLuminance > foregroundLuminance) { | |
contrast = 1 / contrast | |
} | |
// round to two decimal places | |
contrast = Math.floor(contrast * 100) / 100 | |
return contrast | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment