-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.js
More file actions
49 lines (46 loc) · 1.64 KB
/
color.js
File metadata and controls
49 lines (46 loc) · 1.64 KB
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
const color = {
HexToRgb: function(hex) {
hex = hex.replace(/^#/, '');
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
const bigint = parseInt(hex, 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;
return [r, g, b];
},
RgbToHex: function(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
},
getDarkColor: function(hex, percent) {
const [r, g, b] = this.HexToRgb(hex);
const newR = Math.round(r * (1 - percent));
const newG = Math.round(g * (1 - percent));
const newB = Math.round(b * (1 - percent));
return this.RgbToHex(newR, newG, newB);
},
getLightColor: function(hex, percent) {
const [r, g, b] = this.HexToRgb(hex);
const newR = Math.min(255, Math.round(r * (1 + percent)));
const newG = Math.min(255, Math.round(g * (1 + percent)));
const newB = Math.min(255, Math.round(b * (1 + percent)));
return this.RgbToHex(newR, newG, newB);
},
colorNameToHex: function(color) {
const colors = {
"red": "#FF0000",
"blue": "#0000FF",
"green": "#00FF00",
"yellow": "#FFFF00",
"purple": "#800080",
"gray": "808080",
"pink": "FFC0CB",
"brown": "#A52A2A",
"orange": "#FFA500",
"violet": "#EE82EE"
};
return colors[color.toLowerCase()] || null;
}
};
export default color;