-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (35 loc) · 1.22 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
var exports = module.exports = {};
exports.RGBtoHEX = (r, g, b) => {
if (typeof r === 'object') {
g = r.g || r[1];
b = r.b || r[2];
r = r.r || r[0];
}
r = r || 0, g = g || 0, b = b || 0;
if (typeof r !== 'number' || typeof g !== 'number' || typeof b !== 'number') {
throw new TypeError('Values for red, green and blue must be numbers.');
}
if (r > 255 || r < 0 || g > 255 || g < 0 || b > 255 || b < 0) {
throw new TypeError('Values for red, green and blue must be within 0-255.');
}
return ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};
exports.rgb2hex = exports.RGBtoHEX;
exports.HEXtoRGB = (hex) => {
if (hex[0] === '#') {
hex = hex.slice(1);
}
if (hex.length !== 3 && hex.length !== 6) {
throw new TypeError('The hex value must be 3 or 6 characters long.');
}
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2] + '';
}
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
};
exports.hex2rgb = exports.HEXtoRGB;