Skip to content

Instantly share code, notes, and snippets.

@danhollick
Last active February 3, 2020 16:39
Show Gist options
  • Save danhollick/614833cf0916b6b4c5f57cea2cb5aafe to your computer and use it in GitHub Desktop.
Save danhollick/614833cf0916b6b4c5f57cea2cb5aafe to your computer and use it in GitHub Desktop.
Fix contrast calculation
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