Skip to content

Instantly share code, notes, and snippets.

@ncou
Forked from jfsiii/relativeLuminanceW3C.js
Created November 6, 2016 10:48
Show Gist options
  • Save ncou/f293afefe1257cd36cc0e16e3e02f5ef to your computer and use it in GitHub Desktop.
Save ncou/f293afefe1257cd36cc0e16e3e02f5ef 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;
}
@ncou
Copy link
Author

ncou commented Nov 6, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment