-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHSLColor.cpp
More file actions
180 lines (146 loc) · 3.69 KB
/
Copy pathHSLColor.cpp
File metadata and controls
180 lines (146 loc) · 3.69 KB
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
#include "HSLColor.h"
static const double D_EPSILON = 0.00000000000001;
////////////////////////////////////////////////////////////
HSL::HSL() :Hue(0), Saturation(0), Luminance(0) {}
////////////////////////////////////////////////////////////
HSL::HSL(int H, int S, int L)
{
///Range control for Hue.
if (H <= 360 && H >= 0) { Hue = H; }
else
{
if (H > 360) { Hue = H % 360; }
else if (H < 0 && H > -360) { Hue = -H; }
else if (H < 0 && H < -360) { Hue = -(H % 360); }
}
///Range control for Saturation.
if (S <= 100 && S >= 0) { Saturation = S; }
else
{
if (S > 100) { Saturation = S % 100; }
else if (S < 0 && S > -100) { Saturation = -S; }
else if (S < 0 && S < -100) { Saturation = -(S % 100); }
}
///Range control for Luminance
if (L <= 100 && L >= 0) { Luminance = L; }
else
{
if (L > 100) { Luminance = L % 100; }
if (L < 0 && L > -100) { Luminance = -L; }
if (L < 0 && L < -100) { Luminance = -(L % 100); }
}
}
////////////////////////////////////////////////////////////
double HSL::HueToRGB(double arg1, double arg2, double H)
{
if (H < 0) H += 1;
if (H > 1) H -= 1;
if ((6 * H) < 1) { return (arg1 + (arg2 - arg1) * 6 * H); }
if ((2 * H) < 1) { return arg2; }
if ((3 * H) < 2) { return (arg1 + (arg2 - arg1) * ((2.0 / 3.0) - H) * 6); }
return arg1;
}
////////////////////////////////////////////////////////////
sf::Color HSL::TurnToRGB()
{
///Reconvert to range [0,1]
double H = Hue / 360.0;
double S = Saturation / 100.0;
double L = Luminance / 100.0;
float arg1, arg2;
if (S <= D_EPSILON)
{
sf::Color C(static_cast<sf::Uint8>(L * 255), static_cast<sf::Uint8>(L * 255), static_cast<sf::Uint8>(L * 255));
return C;
}
else
{
if (L < 0.5) { arg2 = static_cast<float>(L * (1 + S)); }
else { arg2 = static_cast<float>((L + S) - (S * L)); }
arg1 = static_cast<float>(2 * L - arg2);
sf::Uint8 r = static_cast<sf::Uint8>(255 * HueToRGB(arg1, arg2, (H + 1.0 / 3.0)));
sf::Uint8 g = static_cast<sf::Uint8>(255 * HueToRGB(arg1, arg2, H));
sf::Uint8 b = static_cast<sf::Uint8>(255 * HueToRGB(arg1, arg2, (H - 1.0 / 3.0)));
sf::Color C(r, g, b);
return C;
}
}
////////////////////////////////////////////////////////////
HSL HSL::TurnToHSL(const sf::Color& C)
{
///Trivial cases.
if (C == sf::Color::White)
{
return HSL(0, 0, 100);
}
if (C == sf::Color::Black)
{
return HSL(0, 0, 0);
}
if (C == sf::Color::Red)
{
return HSL(0, 100, 50);
}
if (C == sf::Color::Yellow)
{
return HSL(60, 100, 50);
}
if (C == sf::Color::Green)
{
return HSL(120, 100, 50);
}
if (C == sf::Color::Cyan)
{
return HSL(180, 100, 50);
}
if (C == sf::Color::Blue)
{
return HSL(240, 100, 50);
}
if (C == sf::Color::Cyan)
{
return HSL(300, 100, 50);
}
double R, G, B;
R = C.r / 255.0;
G = C.g / 255.0;
B = C.b / 255.0;
///Casos no triviales.
double l{ 0 };
double s{0};
///Maximos
double max = std::max(std::max(R, G), B);
///Minimos
double min = std::min(std::min(R, G), B);
HSL A;
l = ((max + min) / 2.0);
if (max - min <= D_EPSILON)
{
A.Hue = 0;
A.Saturation = 0;
}
else
{
double diff = max - min;
if (A.Luminance < 0.5)
{
s = diff / (max + min);
}
else
{
s = diff / (2 - max - min);
}
double diffR = (((max - R) * 60) + (diff / 2.0)) / diff;
double diffG = (((max - G) * 60) + (diff / 2.0)) / diff;
double diffB = (((max - B) * 60) + (diff / 2.0)) / diff;
if (max - R <= D_EPSILON) { A.Hue = diffB - diffG; }
else if (max - G <= D_EPSILON) { A.Hue = (1 * 360) / 3.0 + (diffR - diffB); }
else if (max - B <= D_EPSILON) { A.Hue = (2 * 360) / 3.0 + (diffG - diffR); }
if (A.Hue <= 0 || A.Hue >= 360) { A.Hue = fmod(A.Hue, 360); }
s *= 100;
}
l *= 100;
A.Saturation = s;
A.Luminance = l;
return A;
}