-
Notifications
You must be signed in to change notification settings - Fork 13
/
colorgen.cpp
196 lines (169 loc) · 4.48 KB
/
colorgen.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
195
196
/*--------------------------------------------------------------------------
GraphSlick (c) Elias Bachaalany
-------------------------------------
Color generation module
This module implements the color generator
History
--------
10/24/2013 - eliasb - First version
10/24/2013 - eliasb - Added Rewind() method
10/25/2013 - eliasb - Added get_color_anyway() method
--------------------------------------------------------------------------*/
#include "colorgen.h"
//----------------------------------------------------------------------
// Conversion between the HSL(Hue, Saturation, and Luminosity)
// and RBG color model.
//----------------------------------------------------------------------
// The conversion algorithms presented here come from the book by
// Fundamentals of Interactive Computer Graphics by Foley and van Dam.
// In the example code, HSL values are represented as floating point
// number in the range 0 to 1. RGB tridrants use the Windows convention
// of 0 to 255 of each element.
//----------------------------------------------------------------------
//--------------------------------------------------------------------------
/**
* @brief
*/
inline unsigned int make_rgb(
bool bRealRgb,
unsigned char r,
unsigned char g,
unsigned char b)
{
if (bRealRgb)
{
return (r << 16) | (g << 8) | b;
}
else
{
return (b << 16) | (g << 8) | r;
}
}
// ------------------------------------------------------------------------------
/**
* @brief
*/
static unsigned char to_rgb(double rm1, double rm2, double rh)
{
if (rh > 360.0)
rh -= 360.0;
else if (rh < 0.0)
rh += 360.0;
if (rh < 60.0)
rm1 = rm1 + (rm2 - rm1) * rh / 60.0;
else if (rh < 180.0)
rm1 = rm2;
else if (rh < 240.0)
rm1 = rm1 + (rm2 - rm1) * (240.0 - rh) / 60.0;
return (unsigned char)(rm1 * 255.0);
}
//--------------------------------------------------------------------------
static unsigned int HSLtoRGB(
bool bRealRgb,
unsigned int H,
unsigned int S,
unsigned int L)
{
if (S == 0)
{
// achromatic
return make_rgb(bRealRgb, L, L, L);
}
double h = (double)H*360/255;
double s = (double)S / 255;
double l = (double)L / 255;
double rm1, rm2;
if (l <= 0.5)
rm2 = l + l * s;
else
rm2 = l + s - l * s;
rm1 = 2.0 * l - rm2;
return make_rgb(bRealRgb,
to_rgb(rm1, rm2, h + 120.0),
to_rgb(rm1, rm2, h),
to_rgb(rm1, rm2, h - 120.0) );
}
//--------------------------------------------------------------------------
unsigned int colorvargen_t::get_color()
{
if (l <= L_END)
return 0;
unsigned int old_l = l;
l += L_INT;
return HSLtoRGB(bRealRgb, h, s, old_l);
}
//--------------------------------------------------------------------------
bool colorgen_t::get_colorvar(colorvargen_t &cv)
{
unsigned int old_h = h, old_s = s;
// Done with the Hue?
if (h > H_END)
{
// Done with the Saturation?
if (s < S_END)
{
// Nothing else to do
return false;
}
else
{
// Advance Saturation so we get a whole new set of Hue
s += S_INT;
old_s = s;
}
// Start Hue all over again
old_h = h = H_START;
}
else
{
// Advance to next Hue
h += H_INT;
}
cv.bRealRgb = bRealRgb;
cv.h = old_h;
cv.s = old_s;
cv.l = L_START;
cv.L_END = L_END;
cv.L_INT = L_INT;
return true;
}
//--------------------------------------------------------------------------
colorgen_t::colorgen_t(
bool bRealRgb,
unsigned int h_start, unsigned int h_end, unsigned int h_int,
unsigned int s_start, unsigned int s_end, unsigned int s_int,
unsigned int l_start, unsigned int l_end, unsigned int l_int) :
bRealRgb(bRealRgb),
H_START(h_start), H_END(h_end), H_INT(h_int),
S_START(s_start), S_END(s_end), S_INT(s_int),
L_START(l_start), L_END(l_end), L_INT(l_int)
{
h = H_START;
s = S_START;
}
//--------------------------------------------------------------------------
void colorgen_t::rewind()
{
h = H_START;
s = S_START;
}
//--------------------------------------------------------------------------
unsigned int colorgen_t::get_color_anyway(colorvargen_t &cv)
{
unsigned int clr;
while (true)
{
// Get a color variant
clr = cv.get_color();
if (clr != 0)
break;
// No variant? Pick a new color
if (!get_colorvar(cv))
{
// No more colors, just rewind
rewind();
get_colorvar(cv);
}
}
return clr;
}