-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (50 loc) · 1.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
/**
* Convert hex string to rgb array
* @param {String} hex
* @return {Array}
* @api public
*/
function hexToRGB(hex) {
hex = hex.replace(/[^0-9A-F]/gi, '');
var bigint = parseInt(hex, 16);
var r = (bigint >> 16) & 255;
var g = (bigint >> 8) & 255;
var b = bigint & 255;
return [r, g, b];
};
/**
* Taken from http://24ways.org/2010/calculating-color-contrast/
*
* @param {String|Array} color
* @return {String}
* @api private
*/
function getContrastYIQ(color){
if (typeof color === 'string' || color instanceof String) color = hexToRGB(color);
var yiq = ((color[0]*299)+(color[1]*587)+(color[2]*114)) / 1000;
return (yiq >= 128) ? 'light' : 'dark';
}
/**
* Return `true` if `color` is dark
* @param {String|Array} color
* @return {Boolean}
* @api public
*/
function dark(color) {
return (getContrastYIQ(color) === 'dark') ? true : false;
};
/**
* Return `true if `color` is light
* @param {String|Array} color
* @return {Boolean}
* @api public
*/
function light(color) {
return !this.dark(color);
};
module.exports = {
dark: dark,
light: light,
hexToRGB: hexToRGB
};