UNPKG

864 BJavaScriptView Raw
1var utils = require('../utils')
2 , nodes = require('../nodes');
3
4/**
5 * Returns the relative luminance of the given `color`,
6 * see http://www.w3.org/TR/WCAG20/#relativeluminancedef
7 *
8 * Examples:
9 *
10 * luminosity(white)
11 * // => 1
12 *
13 * luminosity(#000)
14 * // => 0
15 *
16 * luminosity(red)
17 * // => 0.2126
18 *
19 * @param {RGBA|HSLA} color
20 * @return {Unit}
21 * @api public
22 */
23
24function luminosity(color){
25 utils.assertColor(color);
26 color = color.rgba;
27 function processChannel(channel) {
28 channel = channel / 255;
29 return (0.03928 > channel)
30 ? channel / 12.92
31 : Math.pow(((channel + 0.055) / 1.055), 2.4);
32 }
33 return new nodes.Unit(
34 0.2126 * processChannel(color.r)
35 + 0.7152 * processChannel(color.g)
36 + 0.0722 * processChannel(color.b)
37 );
38};
39luminosity.params = ['color'];
40module.exports = luminosity;