1 | ;
|
2 |
|
3 | exports.__esModule = true;
|
4 | exports["default"] = getLuminance;
|
5 | var _parseToRgb = _interopRequireDefault(require("./parseToRgb"));
|
6 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
7 | /**
|
8 | * Returns a number (float) representing the luminance of a color.
|
9 | *
|
10 | * @example
|
11 | * // Styles as object usage
|
12 | * const styles = {
|
13 | * background: getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff',
|
14 | * background: getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
|
15 | * 'rgba(58, 133, 255, 1)' :
|
16 | * 'rgba(255, 57, 149, 1)',
|
17 | * }
|
18 | *
|
19 | * // styled-components usage
|
20 | * const div = styled.div`
|
21 | * background: ${getLuminance('#CCCD64') >= getLuminance('#0000ff') ? '#CCCD64' : '#0000ff'};
|
22 | * background: ${getLuminance('rgba(58, 133, 255, 1)') >= getLuminance('rgba(255, 57, 149, 1)') ?
|
23 | * 'rgba(58, 133, 255, 1)' :
|
24 | * 'rgba(255, 57, 149, 1)'};
|
25 | *
|
26 | * // CSS in JS Output
|
27 | *
|
28 | * div {
|
29 | * background: "#CCCD64";
|
30 | * background: "rgba(58, 133, 255, 1)";
|
31 | * }
|
32 | */
|
33 | function getLuminance(color) {
|
34 | if (color === 'transparent') return 0;
|
35 | var rgbColor = (0, _parseToRgb["default"])(color);
|
36 | var _Object$keys$map = Object.keys(rgbColor).map(function (key) {
|
37 | var channel = rgbColor[key] / 255;
|
38 | return channel <= 0.03928 ? channel / 12.92 : Math.pow((channel + 0.055) / 1.055, 2.4);
|
39 | }),
|
40 | r = _Object$keys$map[0],
|
41 | g = _Object$keys$map[1],
|
42 | b = _Object$keys$map[2];
|
43 | return parseFloat((0.2126 * r + 0.7152 * g + 0.0722 * b).toFixed(3));
|
44 | }
|
45 | module.exports = exports.default; |
\ | No newline at end of file |