-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathColorSpaceUtilities.h
130 lines (107 loc) · 2.11 KB
/
ColorSpaceUtilities.h
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
static void HSL2RGB(float h, float s, float l, float* outR, float* outG, float* outB)
{
float temp1,
temp2;
float temp[3];
int i;
// Check for saturation. If there isn't any just return the luminance value for each, which results in gray.
if(s == 0.0) {
if(outR)
*outR = l;
if(outG)
*outG = l;
if(outB)
*outB = l;
return;
}
// Test for luminance and compute temporary values based on luminance and saturation
if(l < 0.5)
temp2 = l * (1.0 + s);
else
temp2 = l + s - l * s;
temp1 = 2.0 * l - temp2;
// Compute intermediate values based on hue
temp[0] = h + 1.0 / 3.0;
temp[1] = h;
temp[2] = h - 1.0 / 3.0;
for(i = 0; i < 3; ++i) {
// Adjust the range
if(temp[i] < 0.0)
temp[i] += 1.0;
if(temp[i] > 1.0)
temp[i] -= 1.0;
if(6.0 * temp[i] < 1.0)
temp[i] = temp1 + (temp2 - temp1) * 6.0 * temp[i];
else {
if(2.0 * temp[i] < 1.0)
temp[i] = temp2;
else {
if(3.0 * temp[i] < 2.0)
temp[i] = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - temp[i]) * 6.0;
else
temp[i] = temp1;
}
}
}
// Assign temporary values to R, G, B
if(outR)
*outR = temp[0];
if(outG)
*outG = temp[1];
if(outB)
*outB = temp[2];
}
static void RGB2HSL(float r, float g, float b, float* outH, float* outS, float* outL)
{
r = r/255.0f;
g = g/255.0f;
b = b/255.0f;
float h,s, l, v, m, vm, r2, g2, b2;
h = 0;
s = 0;
l = 0;
v = MAX(r, g);
v = MAX(v, b);
m = MIN(r, g);
m = MIN(m, b);
l = (m+v)/2.0f;
if (l <= 0.0){
if(outH)
*outH = h;
if(outS)
*outS = s;
if(outL)
*outL = l;
return;
}
vm = v - m;
s = vm;
if (s > 0.0f){
s/= (l <= 0.5f) ? (v + m) : (2.0 - v - m);
}else{
if(outH)
*outH = h;
if(outS)
*outS = s;
if(outL)
*outL = l;
return;
}
r2 = (v - r)/vm;
g2 = (v - g)/vm;
b2 = (v - b)/vm;
if (r == v){
h = (g == m ? 5.0f + b2 : 1.0f - g2);
}else if (g == v){
h = (b == m ? 1.0f + r2 : 3.0 - b2);
}else{
h = (r == m ? 3.0f + g2 : 5.0f - r2);
}
h/=6.0f;
if(outH)
*outH = h;
if(outS)
*outS = s;
if(outL)
*outL = l;
}