Skip to content

Instantly share code, notes, and snippets.

@danhollick
Last active February 3, 2020 16:32
Show Gist options
  • Save danhollick/6950b2c942f53b9398c8cd509286cd5c to your computer and use it in GitHub Desktop.
Save danhollick/6950b2c942f53b9398c8cd509286cd5c to your computer and use it in GitHub Desktop.
Calculating Luminance
function calculateLuminance(color) {
const normalizedColor = color.map(channel => channel / 255)
const gammaCorrectedRGB = normalizedColor.map(channel =>
channel <= 0.03928
? channel / 12.92
: Math.pow((channel + 0.055) / 1.055, 2.4)
)
const luminance =
gammaCorrectedRGB[0] * 0.2126 +
gammaCorrectedRGB[1] * 0.7152 +
gammaCorrectedRGB[2] * 0.0722
return luminance
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment