-
Notifications
You must be signed in to change notification settings - Fork 25
/
tsl.js
81 lines (66 loc) · 1.58 KB
/
tsl.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
/**
* https://en.wikipedia.org/wiki/TSL_color_space
* http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.86.6037&rep=rep1&type=pdf
*
* Tint, Saturation, Lightness
*
* @module color-space/tsl
*/
import rgb from './rgb.js';
var tsl = {
name: 'tsl',
min: [0,0,0],
max: [1, 1, 1],
channel: ['tint','saturation','lightness'],
alias: ['TSL'],
};
export default tsl;
/**
* TSL to RGB
*
* @param {Array} tsl RGB values
*
* @return {Array} TSL values
*/
tsl.rgb = function(tsl) {
var T = tsl[0],
S = tsl[1],
L = tsl[2];
//wikipedia solution
/*
// var x = - 1 / Math.tan(Math.PI * 2 * T);
var x = -Math.sin(2*Math.PI*T);
if ( x != 0 ) x = Math.cos(2*Math.PI*T)/x;
var g = T > .5 ? -S * Math.sqrt( 5 / (9 * (x*x + 1)) ) :
T < .5 ? S * Math.sqrt( 5 / (9 * (x*x + 1)) ) : 0;
var r = T === 0 ? 0.7453559 * S : (x * g + 1/3);
var R = k * r, G = k * g, B = k * (1 - r - g);
*/
var x = Math.tan(2 * Math.PI * (T - 1/4));
x *= x;
var r = Math.sqrt(5 * S*S / (9 * (1/x + 1))) + 1/3;
var g = Math.sqrt(5 * S*S / (9 * (x + 1))) + 1/3;
var k = L / (.185 * r + .473 * g + .114);
var B = k * (1 - r - g);
var G = k * g;
var R = k * r;
return [
R * 255, G * 255, B * 255
];
};
/**
* RGB to TSL
*
* @param {Array} tsl TSL values
*
* @return {Array} RGB values
*/
rgb.tsl = function(rgb) {
var [r, g, b] = rgb;
var r_ = (r / (r + g + b) || 0) - 1/3,
g_ = (g / (r + g + b) || 0) - 1/3,
T = g_ != 0 ? 0.5 - Math.atan2(g_, r_) / 2 / Math.PI : 0,
S = Math.sqrt(9/5 * (r_*r_ + g_*g_)),
L = ((r * 0.299) + (g * 0.587) + (b * 0.114)) / 255;
return [T, S, L];
};