-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.js
383 lines (300 loc) · 13.9 KB
/
node.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
let gethue = h => {
h = h%360
let part = Math.floor(h / 60)
let is_start_part = part % 2 == 0
let result = {r:0,g:0,b:0}
if(is_start_part) {
let l = [{key:"r",value:120 * 0},{key:"g",value:120 * 1},{key:"b",value:120 * 2}][(part / 2) % 3]
let s = [{key:"r",value:120 * 0},{key:"g",value:120 * 1},{key:"b",value:120 * 2}][(part / 2 + 1) % 3]
result[l.key] = 255
result[s.key] = (h - l.value) / 60 * 255
} else {
let l = [{key:"r",value:120 * 0},{key:"g",value:120 * 1},{key:"b",value:120 * 2}][((part - 1) / 2) % 3]
let s = [{key:"r",value:120 * 0},{key:"g",value:120 * 1},{key:"b",value:120 * 2}][((part - 1) / 2 + 1) % 3]
result[l.key] = 255 - ((h - l.value - 60) / 60 * 255)
result[s.key] = 255
}
return result
}
function hsl_rgb(h,s,l,a,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) {
let addLigtenlightness = (l,rgb) => {
Object.entries(rgb).forEach(entry => {
let key = entry[0], value = entry[1]
let result = ((l-50)/50 * (l > 50 ? 255 - value : value) + value)
rgb[key] = result < 0 ? 0 : result > 255 ? 255 : result
})
return rgb
}
let addsaturation = (s,l,rgb) => {
let GrayScale = 255 * (l / 100)
Object.entries(rgb).forEach(entry => {
let key = entry[0], value = entry[1]
let result = value + ((GrayScale - value) * (100 - s)/100)
rgb[key] = result < 0 ? 0 : result > 255 ? 255 : result
})
return rgb
}
return details(
`rgb(${Object.values(addsaturation(s,l,addLigtenlightness(l,gethue(h)))).map(x=>Math.round(x)).join(',')}${a?`,${a}`:''})`,
options
)
}
function rgb_hsl(r,g,b,a,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) {
let sort = _ => {
let sorted = [{value:r,place:0}]
let rgb = [g,b]
rgb.forEach((color,index)=>{
sorted.forEach((sortedcolor,sortedindex)=>{
let save = sortedcolor
if(color < sortedcolor["value"]) {
sorted[sortedindex] = {value:color,place:index + 1}
sorted.push(save)
}
else if(sortedindex == sorted.length - 1) {
sorted.push({value:color,place:index + 1})
}
})
})
return sorted.reverse()
}
// h = (mid - min) / (borderBottom - borderTop) * 60 + (60 * part[0-6])
let
[max,mid,min] = sort()
part = ((min.place + 1) % 3 * 2) + (max.place == min.place + 2 % 3 ? 1 : 0),
hue = (part * 60) + (mid["value"] - min["value"]) / (max["value"] - min["value"]) * 60
// lightness = difference borders / 255 * 50
let
min_lightness = min["value"] / 255 * 50,
max_lightness = (max["value"] - 255) / 255 * 50,
lightness = 50 + min_lightness + max_lightness
// saturation = rate min from GrayScale : (lightness from 255)
let
borderbottom = (lightness - 50) / 50 * 255 > 0 ? (lightness - 50) / 50 * 255 : 0,
GrayScale = lightness / 100 * 255 ,
saturation = 100 - (min["value"] - borderbottom) / (GrayScale - borderbottom) * 100
return details(`hsl(${isNaN(hue) ? 0 : Math.round(hue)},${isNaN(saturation) ? 0 :Math.round(saturation)}%,${Math.round(lightness)}%${a?`,${a}`:''})`,options)
}
function hwb_rgb(h,w,b,a,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) {
// way 2 to handle + 100 b+w if(b + w >= 100) {w >= b ? b = 100 - w : w = 100 - b}
if(b + w > 100) {
if(w > b){w = 100 / (w / b) ; b = 100 - w }
else {b = 100 / (b / w) ; w = 100 - b }
}
let add_Whitenees_Darkness = (w,b,rgb) => {
Object.entries(rgb).forEach(entry => {
let key = entry[0], value = entry[1]
let whitevalue = (255 - value) * w/100
let blackvalue = value * b/100
let result = value + whitevalue - blackvalue
rgb[key] = result < 0 ? 0 : result > 255 ? 255 : result
})
return rgb
}
return details(
`rgb(${Object.values(add_Whitenees_Darkness(w,b,gethue(h))).map(x=>Math.round(x)).join(',')}${a?`,${a}`:''})`,
options
)
}
function rgb_hwb(r,g,b,a,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) {
let sort = _ => {
let sorted = [{value:r,place:0}]
let rgb = [g,b]
rgb.forEach((color,index)=>{
sorted.forEach((sortedcolor,sortedindex)=>{
let save = sortedcolor
if(color < sortedcolor["value"]) {
sorted[sortedindex] = {value:color,place:index + 1}
sorted.push(save)
}
else if(sortedindex == sorted.length - 1) {
sorted.push({value:color,place:index + 1})
}
})
})
return sorted.reverse()
}
// h = (mid - min) / (borderBottom - borderTop) * 60 + (60 * part[0-6])
let
[max,mid,min] = sort(),
part = ((min.place + 1) % 3 * 2) + (max.place == min.place + 2 % 3 ? 1 : 0),
hue = (part * 60) + (mid["value"] - min["value"]) / (max["value"] - min["value"]) * 60 ,
whiteness = (min["value"]) / 255 * 100,
blackness = (255 - max["value"]) / 255 * 100
return details(`hwb(${isNaN(hue) ? 0 : Math.round(hue)},${Math.round(whiteness)}%,${Math.round(blackness)}%${a?`,${a}`:''})`,options)
}
function hex_rgb(h,e,x,a,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) {
let convert = letter => {
if(letter.length == 1) {
if(isNaN(+letter)){letter = "abcdef".search(letter) + 10}else{letter=+letter}
return letter * 17
} else {
let letters = []
Array.from(letter).forEach(letter => {if(isNaN(+letter)){letters.push("abcdef".search(letter) + 10)}else{letters.push(+letter)}})
return letters[0] * 16 + letters[1]
}
}
return details(`rgb(${convert(h)},${convert(e)},${convert(x)}${a ? `,${convert(a)/255}` : ``})`,options)
}
function rgb_hex(r,g,b,a,options = {hex_length:2,use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) {
let Defult_options = {hex_length:2}
Object.entries(Defult_options).forEach(entry=>{
let OptionKey = entry[0] , OptionVlaue = entry[1]
if(options[OptionKey] == undefined){
options[OptionKey] = OptionVlaue
}
})
let convert = number => {
if(options.hex_length == 1) {
number = Math.round(number / 17) < 10 ? Math.round(number / 17)+'' : [..."abcdef"][Math.round(number / 17) - 10]
return number + ''
} else {
let num1 = Math.trunc(number/16) * 16 , num2 = number - num1
num1 = Math.round(num1/16) < 10 ? Math.round(num1/16) : [..."abcdef"][Math.round(num1/16) - 10]
num2 = num2 < 10 ? num2 : [..."abcdef"][num2 - 10]
return `${num1}${num2}`
}
}
return details(`#${convert(r)}${convert(g)}${convert(b)}${a ? convert(a*255) : ""}`,options)
}
function hex_hsl(h,e,x,a,options = {}) {
let rgb = hex_rgb(h,e,x,a,{return_color:false}).map(e=>+e)
rgb.length = 4
return rgb_hsl(...rgb,options)
}
function hsl_hex(h,s,l,a,options = {}) {
let rgb = hsl_rgb(h,s,l,a,{return_color:false}).map(e=>+e)
rgb.length = 4
return rgb_hex(...rgb,options)
}
function hsl_hwb(h,s,l,a,options = {}) {
let rgb = hsl_rgb(h,s,l,a,{return_color:false}).map(e=>+e)
rgb.length = 4
return rgb_hwb(...rgb,options)
}
function hwb_hsl(h,w,b,a,options = {}) {
let rgb = hwb_rgb(h,w,b,a,{return_color:false}).map(e=>+e)
rgb.length = 4
return rgb_hsl(...rgb,options)
}
function hex_hwb(h,e,x,a,options = {}) {
let rgb = hex_rgb(h,e,x,a,{return_color:false}).map(e=>+e)
rgb.length = 4
return rgb_hwb(...rgb,options)
}
function hwb_hex(h,w,b,a,options = {}) {
let rgb = hwb_rgb(h,w,b,a,{return_color:false}).map(e=>+e)
rgb.length = 4
return rgb_hex(...rgb,options)
}
function GetType(Color,func = Type => Type,error = Error => console.log(Error)) {
let matchs = {
rgb:/(rgb)a?\(\d{0,3}(,|\s)+\d{0,3}(,|\s)+\d{0,3}((,|((\s)*\/(\s)*))\d?\.{0,1}\d+)?\)/,
hsl:/(hsl)a?\(\d{0,3}(,|\s)+\d{0,3}%(,|\s)+\d{0,3}%((,|((\s)*\/(\s)*))\d?\.{0,1}\d+)?\)/,
hwb:/(hwb)\(\d{0,3}(,|\s)+\d{0,3}%(,|\s)+\d{0,3}%((,|((\s)*\/(\s)*))\d?\.{0,1}\d+)?\)/,
hex:/#(\w{8}|\w{6}|\w{4}|\w{3})/
}
let Color_Match = false
let Result
Object.entries(matchs).forEach(entry=>{
let key = entry[0],reg = entry[1]
if(reg.test(Color)){
Result = func(key)
Color_Match = true
}
})
if(Color_Match){return Result} else {error("Color did not Match")}
}
function details(Color,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false} ) {
let Defult_options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}
Object.entries(Defult_options).forEach(entry=>{
let OptionKey = entry[0] , OptionVlaue = entry[1]
if(options[OptionKey] == undefined){
options[OptionKey] = OptionVlaue
}
})
return Read(Color,GetType(Color))
function Read(Color,Type) {
if(Type == 'hex') {return Read_hex(Color)}
let values = Color.match(/(\d|\.|%)+/ig)
let is_alpha = values.length >= 4 || false
let Return_Values = options["Values_Type"] == "object" ? {} : []
let Return_ValuesArr = []
let ColorPatern = [...Type].concat("a")
values.forEach((value,index)=>{
if(!Array.isArray(Return_Values)) {
Return_Values[ColorPatern[index]] = value.match(/(\d|\.)/ig).join('')
} else {
Return_Values.push(value.match(/(\d|\.)/ig).join(''))
}
Return_ValuesArr.push(value)
})
if(options["add_alpha"] && !is_alpha){
if(!Array.isArray(Return_Values)) {
Return_Values['a'] = '1'
} else {
Return_Values.push('1')
}
Return_ValuesArr.push('1')
}
if((options["hwb_special_uncomma"] && Type == "hwb") || !options["use_comma"]){
Color = `${Type}(${Return_ValuesArr[0]} ${Return_ValuesArr[1]} ${Return_ValuesArr[2]}${Return_ValuesArr[3] ? ` / ${Return_ValuesArr[3]}` : ''})`
}else{
Color = `${Type}(${Return_ValuesArr.join(',')})`
}
if(options["return_values"] && options["return_color"]) {
return {color:Color,values:Return_Values}
} else if (options["return_color"]) {
return Color
} else if (options["return_values"]) {
return Return_Values
} else {
return `options close any return`
}
}
function Read_hex(Color) {
let values = Color.match(new RegExp(`[^#]{${Color.match(/\w/ig).length == 6 || Color.match(/\w/ig).length == 8 ? 2 : 1}}`,"ig"))
let is_alpha = values.length >= 4 || false
let Return_Values = options["Values_Type"] == "object" ? {} : []
let ColorPatern = ['r','g','b','a']
values.forEach((value,index)=>{
if(!Array.isArray(Return_Values)) {
Return_Values[ColorPatern[index]] = value
} else {
Return_Values.push(value)
}
})
if(options["add_alpha"] && !is_alpha){
if(!Array.isArray(Return_Values)) {
Return_Values['a'] = Color.match(/(\w)/ig).length == 6 || Color.match(/(\w)/ig).length == 8 ? 'ff' : 'f'
} else {
Return_Values.push(Color.match(/(\w)/ig).length == 6 || Color.match(/(\w)/ig).length == 8 ? 'ff' : 'f')
}
Color += Color.match(/(\w)/ig).length == 6 || Color.match(/(\w)/ig).length == 8 ? 'ff' : 'f'
}
if(options["return_values"] && options["return_color"]) {
return {color:Color,values:Return_Values}
} else if (options["return_color"]) {
return Color
} else if (options["return_values"]) {
return Return_Values
} else {
return `options close any return`
}
}
}
function Convert(Color,To,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) {
let Type = GetType(Color)
if(Type == To) {
return details(Color,options)
} else {
let values = details(Color,{return_color:false})
values.length = 3 ? values.push('') : ""
return eval(`${Type}_${To}(${values.map(x=>`'${x}'`)},options)`)
}
}
let
rgb = (Color,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) => Convert(Color,"rgb",options),
hsl = (Color,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) => Convert(Color,"hsl",options),
hwb = (Color,options = {use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) => Convert(Color,"hwb",options),
hex = (Color,options = {hex_length:2,use_alpha:true,add_alpha:false,return_color:true,return_values:true,Values_Type:"array",use_comma:true,hwb_special_uncomma:false}) => Convert(Color,"hex",options)
module.exports = {rgb,hsl,hwb,hex}