-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
67 lines (57 loc) · 1.47 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
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Module dependencies
*/
var names = require('./names');
/**
* Lighten the given color
* @param {String} color hex value
* @param {Number} value
* @return {String}
*/
exports.lighten = function(color, v) {
v = (v <= 1) ? v*100 : v;
return tint(color, v);
};
/**
* Darken the given color
* @param {String} color hex value
* @param {Number} value
* @return {String}
*/
exports.darken = function(color, v) {
v = (v <= 1) ? v*100 : v;
return tint(color, -v);
};
/**
* Tint the color by the given value
*
* Credits: richard maloney 2006
*
* @param {String} color
* @param {Number} v
* @return {String}
*/
function tint(color, v) {
color = names[color] ? names[color] : color;
color = color.replace(/^#/, '');
color = (color.length == 3) ? hex3tohex6(color) : color;
var rgb = parseInt(color, 16);
var r = Math.abs(((rgb >> 16) & 0xFF)+v); if (r>255) r=r-(r-255);
var g = Math.abs(((rgb >> 8) & 0xFF)+v); if (g>255) g=g-(g-255);
var b = Math.abs((rgb & 0xFF)+v); if (b>255) b=b-(b-255);
r = Number(r < 0 || isNaN(r)) ? 0 : ((r > 255) ? 255 : r).toString(16);
if (r.length == 1) r = '0' + r;
g = Number(g < 0 || isNaN(g)) ? 0 : ((g > 255) ? 255 : g).toString(16);
if (g.length == 1) g = '0' + g;
b = Number(b < 0 || isNaN(b)) ? 0 : ((b > 255) ? 255 : b).toString(16);
if (b.length == 1) b = '0' + b;
return "#" + r + g + b;
}
/**
* Hex3 to Hex6
*/
function hex3tohex6(h) {
return h[0] + h[0]
+ h[1] + h[1]
+ h[2] + h[2];
}