forked from DavidBuchanan314/iPodWizard-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.cpp
More file actions
426 lines (382 loc) · 26 KB
/
Color.cpp
File metadata and controls
426 lines (382 loc) · 26 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/****************************************************************************\
Datei : Color.cpp
Projekt: Farbverwaltung
Inhalt : CColor Implementierung
Datum : 10.01.1999
Autor : Christian Rodemeyer
Hinweis: © 1999 by Christian Rodemeyer
Info ber HLS Konvertierungsfunktion
- Foley and Van Dam: "Fundamentals of Interactive Computer Graphics"
- MSDN: 'HLS Color Spaces'
- MSDN: 'Converting Colors Between RGB and HLS'
\****************************************************************************/
#include "StdAfx.h"
#include "Color.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/****************************************************************************\
CColor: Implementierung
\****************************************************************************/
const CColor::DNamedColor CColor::m_namedColor[CColor::numNamedColors] =
{
{aliceblue , _T("aliceblue")},
{antiquewhite , _T("antiquewhite")},
{aqua , _T("aqua")},
{aquamarine , _T("aquamarine")},
{azure , _T("azure")},
{beige , _T("beige")},
{bisque , _T("bisque")},
{black , _T("black")},
{blanchedalmond , _T("blanchedalmond")},
{blue , _T("blue")},
{blueviolet , _T("blueviolet")},
{brown , _T("brown")},
{burlywood , _T("burlywood")},
{cadetblue , _T("cadetblue")},
{chartreuse , _T("chartreuse")},
{chocolate , _T("chocolate")},
{coral , _T("coral")},
{cornflower , _T("cornflower")},
{cornsilk , _T("cornsilk")},
{crimson , _T("crimson")},
{cyan , _T("cyan")},
{darkblue , _T("darkblue")},
{darkcyan , _T("darkcyan")},
{darkgoldenrod , _T("darkgoldenrod")},
{darkgray , _T("darkgray")},
{darkgreen , _T("darkgreen")},
{darkkhaki , _T("darkkhaki")},
{darkmagenta , _T("darkmagenta")},
{darkolivegreen , _T("darkolivegreen")},
{darkorange , _T("darkorange")},
{darkorchid , _T("darkorchid")},
{darkred , _T("darkred")},
{darksalmon , _T("darksalmon")},
{darkseagreen , _T("darkseagreen")},
{darkslateblue , _T("darkslateblue")},
{darkslategray , _T("darkslategray")},
{darkturquoise , _T("darkturquoise")},
{darkviolet , _T("darkviolet")},
{deeppink , _T("deeppink")},
{deepskyblue , _T("deepskyblue")},
{dimgray , _T("dimgray")},
{dodgerblue , _T("dodgerblue")},
{firebrick , _T("firebrick")},
{floralwhite , _T("floralwhite")},
{forestgreen , _T("forestgreen")},
{fuchsia , _T("fuchsia")},
{gainsboro , _T("gainsboro")},
{ghostwhite , _T("ghostwhite")},
{gold , _T("gold")},
{goldenrod , _T("goldenrod")},
{gray , _T("gray")},
{green , _T("green")},
{greenyellow , _T("greenyellow")},
{honeydew , _T("honeydew")},
{hotpink , _T("hotpink")},
{indianred , _T("indianred")},
{indigo , _T("indigo")},
{ivory , _T("ivory")},
{khaki , _T("khaki")},
{lavender , _T("lavender")},
{lavenderblush , _T("lavenderblush")},
{lawngreen , _T("lawngreen")},
{lemonchiffon , _T("lemonchiffon")},
{lightblue , _T("lightblue")},
{lightcoral , _T("lightcoral")},
{lightcyan , _T("lightcyan")},
{lightgoldenrodyellow , _T("lightgoldenrodyellow")},
{lightgreen , _T("lightgreen")},
{lightgrey , _T("lightgrey")},
{lightpink , _T("lightpink")},
{lightsalmon , _T("lightsalmon")},
{lightseagreen , _T("lightseagreen")},
{lightskyblue , _T("lightskyblue")},
{lightslategray , _T("lightslategray")},
{lightsteelblue , _T("lightsteelblue")},
{lightyellow , _T("lightyellow")},
{lime , _T("lime")},
{limegreen , _T("limegreen")},
{linen , _T("linen")},
{magenta , _T("magenta")},
{maroon , _T("maroon")},
{mediumaquamarine , _T("mediumaquamarine")},
{mediumblue , _T("mediumblue")},
{mediumorchid , _T("mediumorchid")},
{mediumpurple , _T("mediumpurple")},
{mediumseagreen , _T("mediumseagreen")},
{mediumslateblue , _T("mediumslateblue")},
{mediumspringgreen , _T("mediumspringgreen")},
{mediumturquoise , _T("mediumturquoise")},
{mediumvioletred , _T("mediumvioletred")},
{midnightblue , _T("midnightblue")},
{mintcream , _T("mintcream")},
{mistyrose , _T("mistyrose")},
{moccasin , _T("moccasin")},
{navajowhite , _T("navajowhite")},
{navy , _T("navy")},
{oldlace , _T("oldlace")},
{olive , _T("olive")},
{olivedrab , _T("olivedrab")},
{orange , _T("orange")},
{orangered , _T("orangered")},
{orchid , _T("orchid")},
{palegoldenrod , _T("palegoldenrod")},
{palegreen , _T("palegreen")},
{paleturquoise , _T("paleturquoise")},
{palevioletred , _T("palevioletred")},
{papayawhip , _T("papayawhip")},
{peachpuff , _T("peachpuff")},
{peru , _T("peru")},
{pink , _T("pink")},
{plum , _T("plum")},
{powderblue , _T("powderblue")},
{purple , _T("purple")},
{red , _T("red")},
{rosybrown , _T("rosybrown")},
{royalblue , _T("royalblue")},
{saddlebrown , _T("saddlebrown")},
{salmon , _T("salmon")},
{sandybrown , _T("sandybrown")},
{seagreen , _T("seagreen")},
{seashell , _T("seashell")},
{sienna , _T("sienna")},
{silver , _T("silver")},
{skyblue , _T("skyblue")},
{slateblue , _T("slateblue")},
{slategray , _T("slategray")},
{snow , _T("snow")},
{springgreen , _T("springgreen")},
{steelblue , _T("steelblue")},
{tan , _T("tan")},
{teal , _T("teal")},
{thistle , _T("thistle")},
{tomato , _T("tomato")},
{turquoise , _T("turquoise")},
{violet , _T("violet")},
{wheat , _T("wheat")},
{white , _T("white")},
{whitesmoke , _T("whitesmoke")},
{yellow , _T("yellow")},
{yellowgreen , _T("yellowgreen")}
};
LPCTSTR CColor::GetNameFromIndex(int i)
{
ASSERT(0 <= i && i < numNamedColors);
return m_namedColor[i].name;
}
CColor CColor::GetColorFromIndex(int i)
{
ASSERT(0 <= i && i < numNamedColors);
return m_namedColor[i].color;
}
CColor CColor::FromString(LPCTSTR pcColor)
{
CColor t;
t.SetString(pcColor);
return t;
}
CColor::CColor(COLORREF cr)
: m_bIsRGB(true), m_bIsHLS(false), m_colorref(cr)
{}
CColor::operator COLORREF() const
{
const_cast<CColor*>(this)->ToRGB();
return m_colorref;
}
// RGB
void CColor::SetRed(int red)
{
ASSERT(0 <= red && red <= 255);
ToRGB();
m_color[c_red] = static_cast<unsigned char>(red);
m_bIsHLS = false;
}
void CColor::SetGreen(int green)
{
ASSERT(0 <= green && green <= 255);
ToRGB();
m_color[c_green] = static_cast<unsigned char>(green);
m_bIsHLS = false;
}
void CColor::SetBlue(int blue)
{
ASSERT(0 <= blue && blue <= 255);
ToRGB();
m_color[c_blue] = static_cast<unsigned char>(blue);
m_bIsHLS = false;
}
void CColor::SetRGB(int red, int blue, int green)
{
ASSERT(0 <= red && red <= 255);
ASSERT(0 <= green && green <= 255);
ASSERT(0 <= blue && blue <= 255);
m_color[c_red] = static_cast<unsigned char>(red);
m_color[c_green] = static_cast<unsigned char>(green);
m_color[c_blue] = static_cast<unsigned char>(blue);
m_bIsHLS = false;
m_bIsRGB = true;
}
int CColor::GetRed() const
{
const_cast<CColor*>(this)->ToRGB();
return m_color[c_red];
}
int CColor::GetGreen() const
{
const_cast<CColor*>(this)->ToRGB();
return m_color[c_green];
}
int CColor::GetBlue() const
{
const_cast<CColor*>(this)->ToRGB();
return m_color[c_blue];
}
// HSL
void CColor::SetHue(float hue)
{
ASSERT(hue >= 0.0 && hue <= 360.0);
ToHLS();
m_hue = hue;
m_bIsRGB = false;
}
void CColor::SetSaturation(float saturation)
{
ASSERT(saturation >= 0.0 && saturation <= 1.0); // 0.0 ist undefiniert
ToHLS();
m_saturation = saturation;
m_bIsRGB = false;
}
void CColor::SetLuminance(float luminance)
{
ASSERT(luminance >= 0.0 && luminance <= 1.0);
ToHLS();
m_luminance = luminance;
m_bIsRGB = false;
}
void CColor::SetHLS(float hue, float luminance, float saturation)
{
ASSERT(hue >= 0.0 && hue <= 360.0);
ASSERT(luminance >= 0.0 && luminance <= 1.0);
ASSERT(saturation >= 0.0 && saturation <= 1.0); // 0.0 ist undefiniert
m_hue = hue;
m_luminance = luminance;
m_saturation = saturation;
m_bIsRGB = false;
m_bIsHLS = true;
}
float CColor::GetHue() const
{
const_cast<CColor*>(this)->ToHLS();
return m_hue;
}
float CColor::GetSaturation() const
{
const_cast<CColor*>(this)->ToHLS();
return m_saturation;
}
float CColor::GetLuminance() const
{
const_cast<CColor*>(this)->ToHLS();
return m_luminance;
}
// Konvertierung
void CColor::ToHLS()
{
if (!m_bIsHLS)
{
// Konvertierung
unsigned char minval = min(m_color[c_red], min(m_color[c_green], m_color[c_blue]));
unsigned char maxval = max(m_color[c_red], max(m_color[c_green], m_color[c_blue]));
float mdiff = float(maxval) - float(minval);
float msum = float(maxval) + float(minval);
m_luminance = msum / 510.0f;
if (maxval == minval)
{
m_saturation = 0.0f;
m_hue = 0.0f;
}
else
{
float rnorm = (maxval - m_color[c_red] ) / mdiff;
float gnorm = (maxval - m_color[c_green]) / mdiff;
float bnorm = (maxval - m_color[c_blue] ) / mdiff;
m_saturation = (m_luminance <= 0.5f) ? (mdiff / msum) : (mdiff / (510.0f - msum));
if (m_color[c_red] == maxval) m_hue = 60.0f * (6.0f + bnorm - gnorm);
if (m_color[c_green] == maxval) m_hue = 60.0f * (2.0f + rnorm - bnorm);
if (m_color[c_blue] == maxval) m_hue = 60.0f * (4.0f + gnorm - rnorm);
if (m_hue > 360.0f) m_hue = m_hue - 360.0f;
}
m_bIsHLS = true;
}
}
void CColor::ToRGB()
{
if (!m_bIsRGB)
{
if (m_saturation == 0.0) // Grauton, einfacher Fall
{
m_color[c_red] = m_color[c_green] = m_color[c_blue] = unsigned char(m_luminance * 255.0);
}
else
{
float rm1, rm2;
if (m_luminance <= 0.5f) rm2 = m_luminance + m_luminance * m_saturation;
else rm2 = m_luminance + m_saturation - m_luminance * m_saturation;
rm1 = 2.0f * m_luminance - rm2;
m_color[c_red] = ToRGB1(rm1, rm2, m_hue + 120.0f);
m_color[c_green] = ToRGB1(rm1, rm2, m_hue);
m_color[c_blue] = ToRGB1(rm1, rm2, m_hue - 120.0f);
}
m_bIsRGB = true;
}
}
unsigned char CColor::ToRGB1(float rm1, float rm2, float rh)
{
if (rh > 360.0f) rh -= 360.0f;
else if (rh < 0.0f) rh += 360.0f;
if (rh < 60.0f) rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;
else if (rh < 180.0f) rm1 = rm2;
else if (rh < 240.0f) rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f;
return static_cast<unsigned char>(rm1 * 255);
}
// Stringkonvertierung im Format RRGGBB
CString CColor::GetString() const
{
CString color;
color.Format(_T("%02X%02X%02X"), GetRed(), GetGreen(), GetBlue());
return color;
}
bool CColor::SetString(LPCTSTR pcColor)
{
ASSERT(pcColor);
int r, g, b;
if (_stscanf(pcColor, _T("%2x%2x%2x"), &r, &g, &b) != 3)
{
m_color[c_red] = m_color[c_green] = m_color[c_blue] = 0;
return false;
}
else
{
m_color[c_red] = static_cast<unsigned char>(r);
m_color[c_green] = static_cast<unsigned char>(g);
m_color[c_blue] = static_cast<unsigned char>(b);
m_bIsRGB = true;
m_bIsHLS = false;
return true;
}
}
CString CColor::GetName() const
{
const_cast<CColor*>(this)->ToRGB();
int i = numNamedColors;
while (i-- && m_colorref != m_namedColor[i].color);
if (i < 0)
{
return "#" + GetString();
}
else return m_namedColor[i].name;
}