Created
April 20, 2014 22:58
-
-
Save apisurfer/11127400 to your computer and use it in GitHub Desktop.
This file contains 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
// As heard on "Chroma Zone" talk by Lea Verou | |
// Explanation @ http://www.w3.org/TR/2014/NOTE-WCAG20-TECHS-20140311/G18 | |
// @color - [r,g,b] | |
function luminance (color) { | |
var rgb = color.map(function(c){ | |
c /= 255; // to 0-1 range | |
return c < .03928 ? | |
c / 12.92 : | |
Math.pow((c+.055)/1.055, 2.4); | |
}); | |
return 21.26 * rgb[0] + // red | |
71.52 * rgb[1] + // green | |
7.22 * rgb[2]; // blue | |
} | |
/* | |
1. calculate luminance for text and background | |
2. Calculate the contrast ratio using the following formula. | |
(L1 + 0.05) / (L2 + 0.05), where L1 is the relative luminance of the lighter of the foreground or background colors, and L2 is the relative luminance of the darker of the foreground or background colors. | |
3. Check that the contrast ratio is equal to or greater than 4.5:1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment