-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
255 lines (221 loc) · 4.51 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* rgb2hsl
*
* @param {number} r 红色颜色值 0~255
* @param {number} g 绿色颜色值 0~255
* @param {number} b 蓝色颜色值 0~255
*/
export function rgb2hsl(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const diff = max - min;
let h = 0;
let l = (max + min) / 2;
let s = 0;
if (max === min) {
h = 0;
} else if (max === r && g >= b) {
h = 60 * ((g - b) / diff);
} else if (max === r && g < b) {
h = 60 * ((g - b) / diff) + 360;
} else if (max === g) {
h = 60 * ((b - r) / diff) + 120;
} else if (max === b) {
h = 60 * ((r - g) / diff) + 240;
};
if (l === 0 || max === min) {
s = 0;
} else if (0 < l && l <= 0.5) {
s = diff / (2 * l);
} else if (l > 0.5) {
s = diff / (2 - 2 * l);
};
return [Math.round(h), Math.round(s * 100), Math.round(l * 100)];
}
/**
* rgb2hsv
*
* @param {number} r 红色颜色值 0~255
* @param {number} g 绿色颜色值 0~255
* @param {number} b 蓝色颜色值 0~255
*/
export function rgb2hsv(r, g, b) {
r /= 255;
g /= 255;
b /= 255;
const max = Math.max(r, g, b);
const min = Math.min(r, g, b);
const diff = max - min;
let h = 0;
let v = max;
let s = max === 0 ? 0 : diff / max;
// h
if (max === min) {
h = 0;
} else if (max === r && g >= b) {
h = 60 * ((g - b) / diff);
} else if (max === r && g < b) {
h = 60 * ((g - b) / diff) + 360;
} else if (max === g) {
h = 60 * ((b - r) / diff) + 120;
} else if (max === b) {
h = 60 * ((r - g) / diff) + 240;
};
return [Math.round(h), Math.round(s * 100), Math.round(v * 100)];
}
/**
* rgb2hex
*
* @param {number} r 红色颜色值 0~255
* @param {number} g 绿色颜色值 0~255
* @param {number} b 蓝色颜色值 0~255
* @param {number} a 透明度 0~100,默认100
*/
export function rgb2hex(r, g, b, a) {
if (typeof a === 'undefined') {
a = '';
}
else {
a = Math.round(255 * a / 100);
a = (a | 1 << 8).toString(16).slice(1);
}
const val = ((b | g << 8 | r << 16) | 1 << 24).toString(16).slice(1);
return '#' + val.toUpperCase() + a.toUpperCase();
}
/**
* hsl2rgb
*
* @param {number} h Hue 色调 0 ~ 360
* @param {number} s Saturation 饱和度 0 ~ 100
* @param {number} l Lightness 亮度 0 ~ 100
*/
export function hsl2rgb(h, s, l) {
h /= 360;
s /= 100;
l /= 100;
let r = 0;
let g = 0;
let b = 0;
let p = 0;
let q = 0;
function rgb(t, p, q) {
if (t < 1.0 / 6.0) {
return p + (q - p) * 6.0 * t;
} else if (t >= 1.0 / 6.0 && t < 1.0 / 2.0) {
return q;
} else if (t >= 1.0 / 2.0 && t < 2.0 / 3.0) {
return p + (q - p) * ((2.0 / 3.0) - t) * 6.0;
} else {
return p;
}
}
function _rgb(t) {
if (t < 0) {
return t + 1.0;
} else if (t > 1) {
return t - 1.0;
} else {
return t;
}
}
if (s === 0) {
r = g = b = l;
} else {
q = l < 0.5 ? l * (1.0 + s) : l + s - l * s;
p = 2.0 * l - q;
r = rgb(_rgb(h + 1.0 / 3.0), p, q);
g = rgb(_rgb(h), p, q);
b = rgb(_rgb(h - 1.0 / 3.0), p, q);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
/**
* hsv2rgb
*
* @param {number} h Hue 色调 0 ~ 360
* @param {number} s Saturation 饱和度 0 ~ 100
* @param {number} v Value 明度 0 ~ 100
*/
export function hsv2rgb(h, s, v) {
h /= 1;
s /= 100;
v /= 100;
let r = 0;
let g = 0;
let b = 0;
if (s === 0) {
r = g = b = v;
} else {
let _h = h / 60;
let i = Math.floor(_h);
let f = _h - i;
let p = v * (1 - s);
let q = v * (1 - f * s);
let t = v * (1 - (1 - f) * s);
switch (i) {
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
case 5:
r = v;
g = p;
b = q;
break;
}
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
/**
* hex2rgb
*
* @param {string} r hex颜色值 eg: #000、#325312、#b2c343
*/
export function hex2rgb(hex) {
hex = hex.replace(/^#/, '');
let a = null;
if (hex.length === 8) {
a = parseInt(hex.slice(6, 8), 16) / 255;
hex = hex.slice(0, 6);
}
if (hex.length === 4) {
a = parseInt(hex.slice(3, 4).repeat(2), 16) / 255;
hex = hex.slice(0, 3);
}
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
const num = parseInt(hex, 16);
const r = num >> 16;
const g = (num >> 8) & 255;
const b = num & 255;
const rgb = [r, g, b];
if (a !== null) {
rgb.push(Math.round(a * 100));
}
return rgb;
}