Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Last active May 13, 2024 08:23
Show Gist options
  • Save jfsiii/5641126 to your computer and use it in GitHub Desktop.
Save jfsiii/5641126 to your computer and use it in GitHub Desktop.
Calculate the relative luminance, or brightness, of a color. Accepts values 0-255 for R, G, B.
// from http://www.w3.org/TR/WCAG20/#relativeluminancedef
function relativeLuminanceW3C(R8bit, G8bit, B8bit) {
var RsRGB = R8bit/255;
var GsRGB = G8bit/255;
var BsRGB = B8bit/255;
var R = (RsRGB <= 0.03928) ? RsRGB/12.92 : Math.pow((RsRGB+0.055)/1.055, 2.4);
var G = (GsRGB <= 0.03928) ? GsRGB/12.92 : Math.pow((GsRGB+0.055)/1.055, 2.4);
var B = (BsRGB <= 0.03928) ? BsRGB/12.92 : Math.pow((BsRGB+0.055)/1.055, 2.4);
// For the sRGB colorspace, the relative luminance of a color is defined as:
var L = 0.2126 * R + 0.7152 * G + 0.0722 * B;
return L;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment