-
Notifications
You must be signed in to change notification settings - Fork 5
/
basis_3x4_proper_gamma.js
97 lines (76 loc) · 2.04 KB
/
basis_3x4_proper_gamma.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
function sum(a, b) {
return a + b;
}
function zip(a, b, f) {
return a.map(function(ai,i){return f(ai, b[i]);});
}
function mapConst(arr, c, f) {
return arr.map(function(ai,i){return f(ai, c, i);});
}
function dotSS(a, b) {
return a * b;
}
//vector * scalar
function dotVS(v, s) {
return mapConst(v, s, dotSS);
}
//vector . vector
function dotVV(a, b) {
return zip(a, b, dotSS).reduce(sum);
}
//matrix . vector
function dotMV(A, v) {
return mapConst(A, v, dotVV);
}
function adj(C) {
return C < 0.0031308 ? (12.92 * C) : (1.055 * Math.pow(C, 0.41666) - 0.055);
}
function labF(t) {
return t > 0.00885645 ? Math.pow(t,1.0/3.0) : (0.137931 + 7.787 * t);
}
function invLabF(t) {
return t > 0.2069 ? (t*t*t) : (0.12842 * (t - 0.137931));
}
function XYZ_to_Lab(XYZ) {
var lfY = labF(XYZ[1]);
return [(116.0 * lfY - 16)/100,
5 * (labF(XYZ[0]) - lfY),
2 * (lfY - labF(XYZ[2]))];
}
function Lab_to_XYZ(Lab) {
var YL = (100*Lab[0] + 16)/116;
return [invLabF(YL + Lab[1]/5.0),
invLabF(YL),
invLabF(YL - Lab[2]/2.0)];
}
function XYZ_to_sRGBlin(xyz) {
return dotMV([[3.240, -1.537, -0.499], [-0.969, 1.876, 0.042], [0.056, -0.204, 1.057]], xyz);
}
function XYZ_to_sRGB(xyz) {
return XYZ_to_sRGBlin(xyz).map(adj);
}
function Lab_to_sRGB(Lab) {
return XYZ_to_sRGB(Lab_to_XYZ(Lab));
}
function getSolarIrr() {
return [0.986*B01, B02, 0.939*B03, 0.779*B04];
}
function S2_to_XYZ(rad, T, gain) {
return dotVS(dotMV(T, rad), gain);
}
function ProperGamma_S2_to_sRGB(rad, T, gg, gamma, gL) {
var XYZ = S2_to_XYZ(rad, T, gg);
var Lab = XYZ_to_Lab(XYZ);
var L = Math.pow(gL * Lab[0], gamma);
return Lab_to_sRGB([L, Lab[1], Lab[2]]);
}
var T = [
[0.141, 0.019, 0.544, 0.325],
[-0.016, 0.247, 0.629, 0.154],
[0.714, 0.315, -0.03, 0.002]
];
// Gamma and gain parameters
var gain = 2.5;
var gammaAdj = 2.2;
var gainL = 1;
return ProperGamma_S2_to_sRGB(getSolarIrr(), T, gain, gammaAdj, gainL);