-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (86 loc) · 2.42 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* HEX TO _____ Codes */
const hexToHSL = (hex) => {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
var r = parseInt(result[1], 16);
var g = parseInt(result[2], 16);
var b = parseInt(result[3], 16);
r /= 255;
g /= 255;
b /= 255;
var max = Math.max(r, g, b),
min = Math.min(r, g, b);
var h,
s,
l = (max + min) / 2;
if (max === min) {
h = s = 0; // achromatic
} else {
var d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r:
h = (g - b) / d + (g < b ? 6 : 0);
break;
case g:
h = (b - r) / d + 2;
break;
case b:
h = (r - g) / d + 4;
break;
default:
break;
}
h /= 6;
}
s = s * 100;
s = Math.round(s);
l = l * 100;
l = Math.round(l);
h = Math.round(360 * h);
var colorInHSL = 'hsl(' + h + ',' + s + '%,' + l + '%)';
return colorInHSL;
};
const hexToRGB = (hex) => {
// remove the # in the beginning
hex = hex.replace('#', '');
var r = parseInt(hex.substring(0, 2), 16);
var g = parseInt(hex.substring(2, 4), 16);
var b = parseInt(hex.substring(4, 6), 16);
return 'rgb(' + r + ',' + g + ',' + b + ')';
};
const hexToCMYK = (hex) => {
var computedC = 0;
var computedM = 0;
var computedY = 0;
var computedK = 0;
hex = (hex.charAt(0)=="#") ? hex.substring(1,7) : hex;
if (hex.length != 6) {
alert ('Invalid length of the input hex value!');
return;
}
if (/[0-9a-f]{6}/i.test(hex) != true) {
alert ('Invalid digits in the input hex value!');
return;
}
var r = parseInt(hex.substring(0,2),16);
var g = parseInt(hex.substring(2,4),16);
var b = parseInt(hex.substring(4,6),16);
// BLACK
if (r==0 && g==0 && b==0) {
computedK = 1;
return [0,0,0,1];
}
computedC = 1 - (r/255);
computedM = 1 - (g/255);
computedY = 1 - (b/255);
var minCMY = Math.min(computedC,Math.min(computedM,computedY));
computedC = (computedC - minCMY) / (1 - minCMY) ;
computedM = (computedM - minCMY) / (1 - minCMY) ;
computedY = (computedY - minCMY) / (1 - minCMY) ;
computedK = minCMY;
return `cmyk(${computedC.toFixed(2)},${computedM.toFixed(2)},${computedY.toFixed(2)},${computedK.toFixed(2)})`;
}
exports.hexToRGB = hexToRGB;
exports.hexToHSL = hexToHSL ;
exports.hexToCMYK = hexToCMYK ;
/* RGB TO _____ Codes */