-
Notifications
You must be signed in to change notification settings - Fork 25
/
luv.js
98 lines (72 loc) · 2.23 KB
/
luv.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
/**
* CIE LUV (C'est la vie)
*
* @module color-space/luv
*/
import xyz from './xyz.js';
export default {
name: 'luv',
//NOTE: luv has no rigidly defined limits
//easyrgb fails to get proper coords
//boronine states no rigid limits
//colorMine refers this ones:
min: [0,-134,-140],
max: [100,224,122],
channel: ['lightness', 'u', 'v'],
alias: ['LUV', 'cieluv', 'cie1976'],
xyz: function(arg, i, o){
var _u, _v, l, u, v, x, y, z, xn, yn, zn, un, vn;
l = arg[0], u = arg[1], v = arg[2];
if (l === 0) return [0,0,0];
//get constants
//var e = 0.008856451679035631; //(6/29)^3
var k = 0.0011070564598794539; //(3/29)^3
//get illuminant/observer
i = i || 'D65';
o = o || 2;
xn = xyz.whitepoint[o][i][0];
yn = xyz.whitepoint[o][i][1];
zn = xyz.whitepoint[o][i][2];
un = (4 * xn) / (xn + (15 * yn) + (3 * zn));
vn = (9 * yn) / (xn + (15 * yn) + (3 * zn));
// un = 0.19783000664283;
// vn = 0.46831999493879;
_u = u / (13 * l) + un || 0;
_v = v / (13 * l) + vn || 0;
y = l > 8 ? yn * Math.pow( (l + 16) / 116 , 3) : yn * l * k;
//wikipedia method
x = y * 9 * _u / (4 * _v) || 0;
z = y * (12 - 3 * _u - 20 * _v) / (4 * _v) || 0;
//boronine method
//https://github.com/boronine/husl/blob/master/husl.coffee#L201
// x = 0 - (9 * y * _u) / ((_u - 4) * _v - _u * _v);
// z = (9 * y - (15 * _v * y) - (_v * x)) / (3 * _v);
return [x, y, z];
}
};
// http://www.brucelindbloom.com/index.html?Equations.html
// https://github.com/boronine/husl/blob/master/husl.coffee
//i - illuminant
//o - observer
xyz.luv = function(arg, i, o) {
var _u, _v, l, u, v, x, y, z, xn, yn, zn, un, vn;
//get constants
var e = 0.008856451679035631; //(6/29)^3
var k = 903.2962962962961; //(29/3)^3
//get illuminant/observer coords
i = i || 'D65';
o = o || 2;
xn = xyz.whitepoint[o][i][0];
yn = xyz.whitepoint[o][i][1];
zn = xyz.whitepoint[o][i][2];
un = (4 * xn) / (xn + (15 * yn) + (3 * zn));
vn = (9 * yn) / (xn + (15 * yn) + (3 * zn));
x = arg[0], y = arg[1], z = arg[2];
_u = (4 * x) / (x + (15 * y) + (3 * z)) || 0;
_v = (9 * y) / (x + (15 * y) + (3 * z)) || 0;
var yr = y/yn;
l = yr <= e ? k * yr : 116 * Math.pow(yr, 1/3) - 16;
u = 13 * l * (_u - un);
v = 13 * l * (_v - vn);
return [l, u, v];
};