-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobj.color.js
27 lines (26 loc) · 975 Bytes
/
obj.color.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
const Color = {
random: function () {
return '#' + (((1 << 24) * Math.random()) | 0).toString(16).padStart(6, 0);
},
fade: function (col, amount) {
const min = Math.min, max = Math.max;
const num = parseInt(col.replace(/#/g, ''), 16);
const r = min(255, max((num >> 16) + amount, 0));
const g = min(255, max((num & 0x0000FF) + amount, 0));
const b = min(255, max(((num >> 8) & 0x00FF) + amount, 0));
return '#' + (g | (b << 8) | (r << 16)).toString(16).padStart(6, 0);
},
hexToRgb: function (hex) {
const match = hex.replace(/#/, '').match(/.{1,2}/g);
return {
r: parseInt(match[0], 16),
g: parseInt(match[1], 16),
b: parseInt(match[2], 16)
};
},
rgbToHex: function (r, g, b) {
return '#' + [r, g, b].map(x => {
return x.toString(16).padStart(2, 0);
}).join('');
}
};