-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
114 lines (90 loc) · 4.23 KB
/
util.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
// the following functions are based off of the pseudocode
// found on www.easyrgb.com
function lab2rgb(lab){
var y = (lab[0] + 16) / 116,
x = lab[1] / 500 + y,
z = y - lab[2] / 200,
r, g, b;
x = 0.95047 * ((x * x * x > 0.008856) ? x * x * x : (x - 16/116) / 7.787);
y = 1.00000 * ((y * y * y > 0.008856) ? y * y * y : (y - 16/116) / 7.787);
z = 1.08883 * ((z * z * z > 0.008856) ? z * z * z : (z - 16/116) / 7.787);
r = x * 3.2406 + y * -1.5372 + z * -0.4986;
g = x * -0.9689 + y * 1.8758 + z * 0.0415;
b = x * 0.0557 + y * -0.2040 + z * 1.0570;
r = (r > 0.0031308) ? (1.055 * Math.pow(r, 1/2.4) - 0.055) : 12.92 * r;
g = (g > 0.0031308) ? (1.055 * Math.pow(g, 1/2.4) - 0.055) : 12.92 * g;
b = (b > 0.0031308) ? (1.055 * Math.pow(b, 1/2.4) - 0.055) : 12.92 * b;
return [Math.max(0, Math.min(1, r)) * 255,
Math.max(0, Math.min(1, g)) * 255,
Math.max(0, Math.min(1, b)) * 255]
}
function rgb2lab(rgb){
var r = rgb[0] / 255,
g = rgb[1] / 255,
b = rgb[2] / 255,
x, y, z;
r = (r > 0.04045) ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
g = (g > 0.04045) ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
b = (b > 0.04045) ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
x = (r * 0.4124 + g * 0.3576 + b * 0.1805) / 0.95047;
y = (r * 0.2126 + g * 0.7152 + b * 0.0722) / 1.00000;
z = (r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883;
x = (x > 0.008856) ? Math.pow(x, 1/3) : (7.787 * x) + 16/116;
y = (y > 0.008856) ? Math.pow(y, 1/3) : (7.787 * y) + 16/116;
z = (z > 0.008856) ? Math.pow(z, 1/3) : (7.787 * z) + 16/116;
return [(116 * y) - 16, 500 * (x - y), 200 * (y - z)]
}
// calculate the perceptual distance between colors in CIELAB
// https://github.com/THEjoezack/ColorMine/blob/master/ColorMine/ColorSpaces/Comparisons/Cie94Comparison.cs
function deltaE(labA, labB){
var deltaL = labA[0] - labB[0];
var deltaA = labA[1] - labB[1];
var deltaB = labA[2] - labB[2];
var c1 = Math.sqrt(labA[1] * labA[1] + labA[2] * labA[2]);
var c2 = Math.sqrt(labB[1] * labB[1] + labB[2] * labB[2]);
var deltaC = c1 - c2;
var deltaH = deltaA * deltaA + deltaB * deltaB - deltaC * deltaC;
deltaH = deltaH < 0 ? 0 : Math.sqrt(deltaH);
var sc = 1.0 + 0.045 * c1;
var sh = 1.0 + 0.015 * c1;
var deltaLKlsl = deltaL / (1.0);
var deltaCkcsc = deltaC / (sc);
var deltaHkhsh = deltaH / (sh);
var i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh;
return i < 0 ? 0 : Math.sqrt(i);
}
/**!
* @preserve $.parseColor
* Copyright 2011 THEtheChad Elliott
* Released under the MIT and GPL licenses.
*/
// Parse hex/rgb{a} color syntax.
// @input string
// @returns array [r,g,b{,o}]
$.parseColor = function(color) {
var cache,
p = parseInt, // Use p as a byte saving reference to parseInt
color = color.replace(/\s/g,''); // Remove all spaces
// Checks for 6 digit hex and converts string to integer
if (cache = /#([\da-fA-F]{2})([\da-fA-F]{2})([\da-fA-F]{2})/.exec(color))
cache = [p(cache[1], 16), p(cache[2], 16), p(cache[3], 16)];
// Checks for 3 digit hex and converts string to integer
else if (cache = /#([\da-fA-F])([\da-fA-F])([\da-fA-F])/.exec(color))
cache = [p(cache[1], 16) * 17, p(cache[2], 16) * 17, p(cache[3], 16) * 17];
// Checks for rgba and converts string to
// integer/float using unary + operator to save bytes
else if (cache = /rgba\(([\d]+),([\d]+),([\d]+),([\d]+|[\d]*.[\d]+)\)/.exec(color))
cache = [+cache[1], +cache[2], +cache[3], +cache[4]];
// Checks for rgb and converts string to
// integer/float using unary + operator to save bytes
else if (cache = /rgb\(([\d]+),([\d]+),([\d]+)\)/.exec(color))
cache = [+cache[1], +cache[2], +cache[3]];
// Otherwise throw an exception to make debugging easier
else throw color + ' is not supported by $.parseColor';
// Performs RGBA conversion by default
isNaN(cache[3]) && (cache[3] = 1);
// Adds or removes 4th value based on rgba support
// Support is flipped twice to prevent erros if
// it's not defined
return cache.slice(0,3 + !!$.support.rgba);
}