-
Notifications
You must be signed in to change notification settings - Fork 3
/
colour_conversion.cpp
195 lines (164 loc) · 4.49 KB
/
colour_conversion.cpp
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* Copyright (c) The Grit Game Engine authors 2016
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include "colour_conversion.h"
void RGBtoHSL (float R, float G, float B, float &H, float &S, float &L)
{
float max_intensity = std::max(std::max(R,G),B);
float min_intensity = std::min(std::min(R,G),B);
float delta = max_intensity - min_intensity;
L = 0.5f * (max_intensity + min_intensity);
if (delta == 0) {
// all channels the same (colour is grey)
S = 0.0f;
H = 0.0f;
return;
}
if (L < 0.5f) {
S = (max_intensity - min_intensity)/(max_intensity + min_intensity);
} else {
S = (max_intensity - min_intensity)/(2 - max_intensity - min_intensity);
}
if (max_intensity == R) {
H = (G - B)/delta;
}
if (max_intensity == G) {
H = 2 + (B - R)/delta;
}
if (max_intensity == B) {
H = 4 + (R - G)/delta;
}
H /= 6;
if (H < 0) H += 1;
}
static float HSLtoRGB_aux (float temp1, float temp2, float temp3)
{
if (temp3 < 1) return temp2 + (temp1-temp2) * temp3;
else if (temp3 < 3) return temp1;
else if (temp3 < 4) return temp2 + (temp1-temp2) * (4 - temp3);
else return temp2;
}
void HSLtoRGB (float H, float S, float L, float &R, float &G, float &B)
{
if (S == 0) {
// grey
R = L;
G = L;
B = L;
return;
}
float temp1 = L<0.5f ? L + L*S : L + S - L*S;
float temp2 = 2*L - temp1;
R = HSLtoRGB_aux(temp1, temp2, fmodf(6*H + 2, 6));
G = HSLtoRGB_aux(temp1, temp2, 6*H);
B = HSLtoRGB_aux(temp1, temp2, fmodf(6*H + 4, 6));
}
void HSVtoHSL (float h, float s, float v, float &hh, float &ss, float &ll)
{
hh = h;
ll = (2 - s) * v;
ss = s * v;
ss /= (ll <= 1) ? ll : 2 - ll;
ll /= 2;
}
void HSLtoHSV (float hh, float ss, float ll, float &h, float &s, float &v)
{
h = hh;
ss *= (ll <= 0.5) ? ll : 1 - ll;
v = ll + ss;
s = 2 * ss / (ll + ss);
}
void RGBtoHSV (float R, float G, float B, float &H, float &S, float &V)
{
float max_intensity = std::max(std::max(R,G),B);
float min_intensity = std::min(std::min(R,G),B);
V = max_intensity;
float delta = max_intensity - min_intensity;
if (delta == 0) {
// grey
H = 0;
S = 0;
return;
}
S = (delta / max_intensity);
if (max_intensity == R) {
H = (G - B)/delta;
}
if (max_intensity == G) {
H = 2 + (B - R)/delta;
}
if (max_intensity == B) {
H = 4 + (R - G)/delta;
}
H /= 6;
if (H < 0) H += 1;
}
void HSVtoRGB (float H, float S, float V, float &R, float &G, float &B)
{
if (S == 0.0) {
// grey
R = V;
G = V;
B = V;
return;
}
float hh = fmodf(H * 6, 6);
long i = (long)hh;
float ff = hh - i;
float p = V * (1.0 - S);
float q = V * (1.0 - (S * ff));
float t = V * (1.0 - (S * (1.0 - ff)));
switch (i) {
case 0:
R = V;
G = t;
B = p;
break;
case 1:
R = q;
G = V;
B = p;
break;
case 2:
R = p;
G = V;
B = t;
break;
case 3:
R = p;
G = q;
B = V;
break;
case 4:
R = t;
G = p;
B = V;
break;
case 5:
default:
R = V;
G = p;
B = q;
break;
}
}