-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwarna.js
462 lines (354 loc) · 8.98 KB
/
warna.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*!
* Warna v0.2.3
* https://github.com/onebytelabs/warna/
*/
(function (undefined) {
'use strict';
// Define module, global and property
var warna = {},
global = typeof global !== 'undefined' ? global : this;
function clone(obj) {
if (null === obj || "object" !== typeof obj) {
return obj;
}
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = obj[attr];
}
}
return copy;
}
/**
* Converter tools
*/
/**
* RGB - HEX converter
* Original Source: http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb
*/
function hexToRgb(hex) {
// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
hex = hex.replace(shorthandRegex, function(m, r, g, b) {
return r + r + g + g + b + b;
});
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
red: parseInt(result[1], 16),
green: parseInt(result[2], 16),
blue: parseInt(result[3], 16)
} : null;
}
function rgbToHex(red, green, blue) {
return "#" + ((1 << 24) + (red << 16) + (green << 8) + blue).toString(16).slice(1);
}
/**
* RGB - HSV converter
* Original Source: http://design.geckotribe.com/colorwheel/
*/
function hsvToRgb(hue, saturation, value) {
var red, green, blue;
if (saturation === 0) {
red = green = blue = Math.round(value * 2.55);
} else {
hue /= 60;
saturation /= 100;
value /= 100;
var i = Math.floor(hue);
var f = hue - i;
var p = value * (1 - saturation);
var q = value * (1 - saturation * f);
var t = value * (1 - saturation * (1 - f));
switch(i) {
case 0:
red = hsv.value;
green = t;
blue = p;
break;
case 1:
red = q;
green = hsv.value;
blue = p;
break;
case 2:
red = p;
green = hsv.value;
blue = t;
break;
case 3:
red = p;
green = q;
blue = hsv.value;
break;
case 4:
red = t;
green = p;
blue = hsv.value;
break;
default:
red = hsv.value;
green = p;
blue = q;
}
red = Math.round(red * 255);
green = Math.round(green * 255);
blue = Math.round(blue * 255);
}
return {
red: red,
green: green,
blue: blue
};
}
function rgbToHsv(red, green, blue) {
var hue, saturation, value;
function min3(a,b,c) { return (a<b)?((a<c)?a:c):((b<c)?b:c); }
function max3(a,b,c) { return (a>b)?((a>c)?a:c):((b>c)?b:c); }
var max = max3(red, green, blue);
var dif = max - min3(red, green, blue);
saturation = (max === 0.0) ? 0 : (100 * dif / max);
if (saturation === 0) {
hue = 0;
} else if (red === max) {
hue = 60.0 * (green - blue) / dif;
} else if (green === max) {
hue = 120.0 + 60.0 * (blue - red) / dif;
} else if (blue === max) {
hue = 240.0 + 60.0 * (red - green) / dif;
}
if (hue < 0.0) {
hue += 360.0;
}
return {
hue: Math.round(hue),
saturation: Math.round(saturation),
value: Math.round(max * 100 / 255)
};
}
/**
* RGB - HSL converter
* Original Source: http://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion
*/
function hslToRgb(hue, saturation, luminosity){
var red, green, blue;
function hue2rgb(p, q, t) {
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1/6) return p + (q - p) * 6 * t;
if(t < 1/2) return q;
if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
if (saturation === 0) {
red = green = blue = luminosity;
} else {
var q = luminosity < 0.5 ? luminosity * (1 + saturation) : luminosity + saturation - luminosity * saturation;
var p = 2 * luminosity - q;
red = hue2rgb(p, q, hue + 1/3);
green = hue2rgb(p, q, hue);
blue = hue2rgb(p, q, hue - 1/3);
}
return {
red: Math.round(red * 255),
green: Math.round(green * 255),
blue: Math.round(blue * 255)
};
}
function rgbToHsl(red, green, blue){
red = red / 255;
green = green / 255;
blue = blue / 255;
var max = Math.max(red, green, blue),
min = Math.min(red, green, blue);
var hue,
saturation,
luminosity = (max + min) / 2;
if (max === min) {
hue = saturation = 0;
} else {
var d = max - min;
saturation = luminosity > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max){
case red: hue = (green - blue) / d + (green < blue ? 6 : 0); break;
case green: hue = (blue - red) / d + 2; break;
case blue: hue = (red - green) / d + 4; break;
}
hue /= 6;
}
return {
hue: hue,
saturation: saturation,
luminosity: luminosity
};
}
/**
* Parser utility
*/
warna.parse = function(color) {
var rgb, hex, hsv, alpha = 1;
// Convert all type to rgb object
if (typeof color === 'string') {
rgb = hexToRgb(color);
} else if (color instanceof Object) {
// Parse RGB object
if (
('red' in color) &&
('green' in color) &&
('blue' in color)
) {
rgb = {
red: color.red,
green: color.green,
blue: color.blue
};
}
// Convert HSV object
if (
('hue' in color) &&
('saturation' in color) &&
('value' in color)
) {
rgb = hsvToRgb(color.hue, color.saturation, color.value);
}
// Convert HSL object
if (
('hue' in color) &&
('saturation' in color) &&
('luminosity' in color)
) {
rgb = hslToRgb(color.hue, color.saturation, color.luminosity);
}
}
// RGB is not converted
if (!rgb) {
return null;
}
// Check alpha
if (color instanceof Object) {
if ('alpha' in color) {
alpha = color.alpha;
}
}
return {
rgb: rgb,
hex: rgbToHex(rgb.red, rgb.green, rgb.blue),
hsv: rgbToHsv(rgb.red, rgb.green, rgb.blue),
hsl: rgbToHsl(rgb.red, rgb.green, rgb.blue),
alpha: alpha
};
};
/**
* Gradient utility
*/
function Gradient(begin, end) {
// Parse color parameter
begin = warna.parse(begin);
end = warna.parse(end);
// Prepare gradient
var gradient = {
begin: begin.rgb,
end: end.rgb
};
// Add alpha value
gradient.begin.alpha = begin.alpha;
gradient.end.alpha = end.alpha;
if (!gradient.begin) {
throw Error('Beginning color is not formatted properly.');
}
if (!gradient.end) {
throw Error('Ending color is not formatted properly.');
}
this.gradient = gradient;
return this;
}
Gradient.prototype.getPosition = function(pos) {
var gradient = this.gradient;
// Check gradient color
if (!gradient.begin || !gradient.end) {
throw Error('Gradient color is not defined.');
}
// Redefine color
var begin = gradient.begin;
var end = gradient.end;
var color = {
red: begin.red + Math.floor(pos * (end.red - begin.red)),
green: begin.green + Math.floor(pos * (end.green - begin.green)),
blue: begin.blue + Math.floor(pos * (end.blue - begin.blue)),
alpha: begin.alpha + (pos * (end.alpha - begin.alpha))
};
return warna.parse(color);
};
Gradient.prototype.getSlices = function(slice, type) {
type = type || null;
var gradient = this.gradient;
// Check gradient color
if (!gradient.begin || !gradient.end) {
throw Error('Gradient color is not defined.');
}
if (slice < 1) {
throw Error('Slices cannot be less than 1.');
}
// Get position of the slices
var initialPos = 0,
range = 1/(slice - 1),
positions = [];
positions.push(0);
if (slice > 1 && range > 0 && range < 1) {
while (initialPos < 1) {
initialPos += range;
if (initialPos > 0.999) {
initialPos = 1;
}
positions.push(initialPos);
}
}
if (slice == 2) {
positions.push(1);
}
// Get color based on slices
var colors = [];
for (var a = 0; a < positions.length; a++) {
colors.push(this.getPosition(positions[a]));
}
function convert(type) {
var result = [];
for (var a = 0; a < colors.length; a++) {
var color = colors[a][type];
// Detect alpha value
if ((type === 'rgb' || type === 'hsv' || type === 'hsl') && colors[a].alpha !== 1) {
color.alpha = colors[a].alpha;
}
result.push(color);
}
return result;
}
// Return raw type
if (!type) {
return colors;
}
return convert(type);
};
warna.Gradient = Gradient;
/**
* Brightness utility
*/
warna.lighten = function(color, pos) {
var grad = new Gradient(color, '#ffffff');
return grad.getPosition(pos);
};
warna.darken = function(color, pos) {
var grad = new Gradient(color, '#000000');
return grad.getPosition(pos);
};
/**
* Exposing Warna
*/
// Browser use
if (typeof window === 'object') {
window.warna = warna;
}
// Node.js use
if (typeof module === 'object' && typeof module.exports === "object") {
module.exports = warna;
}
}).call(this);