[go: up one dir, main page]

Menu

[r52]: / lpi_color.cpp  Maximize  Restore  History

Download this file

440 lines (381 with data), 11.2 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
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
Copyright (c) 2005-2007 Lode Vandevenne
All rights reserved.
This file is part of Lode's Programming Interface.
Lode's Programming Interface is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lode's Programming Interface is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lode's Programming Interface. If not, see <http://www.gnu.org/licenses/>.
*/
#include "lpi_color.h"
#include <cmath>
#include "lpi_general.h"
namespace lpi
{
using namespace std;
////////////////////////////////////////////////////////////////////////////////
//COLOR STRUCTS/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
ColorRGB::ColorRGB(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
{
this->r = r;
this->g = g;
this->b = b;
this->a = a;
}
ColorRGB::ColorRGB(unsigned char r, unsigned char g, unsigned char b)
{
this->r = r;
this->g = g;
this->b = b;
this->a = 255;
}
ColorRGB::ColorRGB()
{
this->r = 0;
this->g = 0;
this->b = 0;
this->a = 255;
}
//Add two colors
ColorRGB operator+(const ColorRGB& color, const ColorRGB& color2)
{
ColorRGB c;
c.r = color.r + color2.r;
c.g = color.g + color2.g;
c.b = color.b + color2.b;
return c;
}
//Subtract two colors
ColorRGB operator-(const ColorRGB& color, const ColorRGB& color2)
{
ColorRGB c;
c.r = color.r - color2.r;
c.g = color.g - color2.g;
c.b = color.b - color2.b;
return c;
}
//Multiplying with a constant does NOT affect the alpha channel, use the "&" operator on two colors instead if you need that
//Multiplies a color with an integer
ColorRGB operator*(const ColorRGB& color, int a)
{
ColorRGB c;
c.r = color.r * a;
c.g = color.g * a;
c.b = color.b * a;
c.a = color.a;
return c;
}
//Multiplies a color with an integer
ColorRGB operator*(int a, const ColorRGB& color)
{
ColorRGB c;
c.r = color.r * a;
c.g = color.g * a;
c.b = color.b * a;
c.a = color.a;
return c;
}
//Multiplies a color with a double
ColorRGB operator*(const ColorRGB& color, double a)
{
ColorRGB c;
c.r = int(color.r * a);
c.g = int(color.g * a);
c.b = int(color.b * a);
c.a = color.a;
return c;
}
//Multiplies a color with a double
ColorRGB operator*(double a, const ColorRGB& color)
{
ColorRGB c;
c.r = int(color.r * a);
c.g = int(color.g * a);
c.b = int(color.b * a);
c.a = color.a;
return c;
}
//Multiply two colors (component by component)
ColorRGB operator*(const ColorRGB& color1, const ColorRGB& color2)
{
ColorRGB c;
c.r = (color1.r * color2.r) / 256;
c.g = (color1.g * color2.g) / 256;
c.b = (color1.b * color2.b) / 256;
c.a = (color1.a * color2.a) / 256;
return c;
}
//Divides a color through an integer
ColorRGB operator/(const ColorRGB& color, int a)
{
if(a == 0) return color;
ColorRGB c;
c.r = color.r / a;
c.g = color.g / a;
c.b = color.b / a;
c.a = color.a;
return c;
}
//Add two colors including their alpha channel
ColorRGB operator|(const ColorRGB& color, const ColorRGB& color2)
{
ColorRGB c;
c.r = color.r + color2.r;
c.g = color.g + color2.g;
c.b = color.b + color2.b;
c.a = color.a + color2.a;
return c;
}
//Multiplies a color with an integer, including alpha channel
ColorRGB operator&(const ColorRGB& color, int a)
{
ColorRGB c;
c.r = color.r * a;
c.g = color.g * a;
c.b = color.b * a;
c.a = color.a * a;
return c;
}
//Multiplies a color with an integer, including alpha channel
ColorRGB operator&(int a, const ColorRGB& color)
{
ColorRGB c;
c.r = color.r * a;
c.g = color.g * a;
c.b = color.b * a;
c.a = color.a * a;
return c;
}
//Multiplies a color with a double, including alpha channel
ColorRGB operator&(const ColorRGB& color, double a)
{
ColorRGB c;
c.r = int(color.r * a);
c.g = int(color.g * a);
c.b = int(color.b * a);
c.a = int(color.a * a);
return c;
}
//Multiplies a color with a double, including alpha channel
ColorRGB operator&(double a, const ColorRGB& color)
{
ColorRGB c;
c.r = int(color.r * a);
c.g = int(color.g * a);
c.b = int(color.b * a);
c.a = int(color.a * a);
return c;
}
//Are both colors equal?
bool operator==(const ColorRGB& color, const ColorRGB& color2)
{
return(color.r == color2.r && color.g == color2.g && color.b == color2.b && color.a == color2.a);
}
//Are both colors not equal?
bool operator!=(const ColorRGB& color, const ColorRGB& color2)
{
return(!(color.r == color2.r && color.g == color2.g && color.b == color2.b && color.a == color2.a));
}
ColorHSL::ColorHSL(unsigned char h, unsigned char s, unsigned char l)
{
this->h = h;
this->s = s;
this->l = l;
}
ColorHSL::ColorHSL()
{
this->h = 0;
this->s = 0;
this->l = 0;
}
ColorHSV::ColorHSV(unsigned char h, unsigned char s, unsigned char v)
{
this->h = h;
this->s = s;
this->v = v;
}
ColorHSV::ColorHSV()
{
this->h = 0;
this->s = 0;
this->v = 0;
}
////////////////////////////////////////////////////////////////////////////////
//COLOR CONVERSIONS/////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/*
These functions convert colors from one color model to another
r=red g=green b=blue h=hue s=saturation l=lightness v=value
Color components from the color structs are unsigned char's between 0 and 255
Color components used in the calculations are normalized between 0.0 and 1.0
*/
//Converts an RGB color to HSL color
ColorHSL RGBtoHSL(const ColorRGB& colorRGB)
{
float r, g, b, h, s, l; //this function works with floats between 0 and 1
r = colorRGB.r / 256.0;
g = colorRGB.g / 256.0;
b = colorRGB.b / 256.0;
float maxColor = std::max(r, std::max(g, b));
float minColor = std::min(r, std::min(g, b));
if(minColor == maxColor) //R = G = B, so it's a shade of grey
{
h = 0; //it doesn't matter what value it has
s = 0;
l = r; //doesn't matter if you pick r, g, or b
}
else
{
l = (minColor + maxColor) / 2;
if(l < 0.5) s = (maxColor - minColor) / (maxColor + minColor);
else s = (maxColor - minColor) / (2.0 - maxColor - minColor); //l >= 0.5
if(r == maxColor) h = (g - b) / (maxColor - minColor);
else if(g == maxColor) h = 2.0 + (b - r) / (maxColor - minColor);
else h = 4.0 + (r - g) / (maxColor - minColor); //b == maxColor
h /= 6; //to bring it to a number between 0 and 1
if(h < 0) h += 1;
}
ColorHSL colorHSL;
colorHSL.h = int(h * 256.0);
colorHSL.s = int(s * 256.0);
colorHSL.l = int(l * 256.0);
return colorHSL;
}
//Converts an HSL color to RGB color
ColorRGB HSLtoRGB(const ColorHSL& colorHSL)
{
float r, g, b, h, s, l; //this function works with floats between 0 and 1
float temp1, temp2, tempr, tempg, tempb;
h = colorHSL.h / 256.0;
s = colorHSL.s / 256.0;
l = colorHSL.l / 256.0;
//If saturation is 0, the color is a shade of grey
if(s == 0) r = g = b = l;
//If saturation > 0, more complex calculations are needed
else
{
//set the temporary values
if(l < 0.5) temp2 = l * (1 + s);
else temp2 = (l + s) - (l * s);
temp1 = 2 * l - temp2;
tempr=h + 1.0 / 3.0;
if(tempr > 1.0) tempr--;
tempg=h;
tempb=h-1.0 / 3.0;
if(tempb < 0.0) tempb++;
//red
if(tempr < 1.0 / 6.0) r = temp1 + (temp2 - temp1) * 6.0 * tempr;
else if(tempr < 0.5) r = temp2;
else if(tempr < 2.0 / 3.0) r = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempr) * 6.0;
else r = temp1;
//green
if(tempg < 1.0 / 6.0) g = temp1 + (temp2 - temp1) * 6.0 * tempg;
else if(tempg < 0.5) g=temp2;
else if(tempg < 2.0 / 3.0) g = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempg) * 6.0;
else g = temp1;
//blue
if(tempb < 1.0 / 6.0) b = temp1 + (temp2 - temp1) * 6.0 * tempb;
else if(tempb < 0.5) b = temp2;
else if(tempb < 2.0 / 3.0) b = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - tempb) * 6.0;
else b = temp1;
}
ColorRGB colorRGB;
colorRGB.r = int(r * 256.0);
colorRGB.g = int(g * 256.0);
colorRGB.b = int(b * 256.0);
return colorRGB;
}
//Converts an RGB color to HSV color
ColorHSV RGBtoHSV(const ColorRGB& colorRGB)
{
float r, g, b, h, s, v; //this function works with floats between 0 and 1
r = colorRGB.r / 256.0;
g = colorRGB.g / 256.0;
b = colorRGB.b / 256.0;
float maxColor = std::max(r, std::max(g, b));
float minColor = std::min(r, std::min(g, b));
v = maxColor;
if(maxColor == 0.0) //avoid division by zero when the color is black
{
s = 0.0;
}
else
{
s = (maxColor - minColor) / maxColor;
}
if(s == 0.0)
{
h = 0.0; //it doesn't matter what value it has
}
else
{
if(r == maxColor) h = (g - b) / (maxColor - minColor);
else if(g == maxColor) h = 2.0 + (b - r) / (maxColor - minColor);
else h = 4.0 + (r - g) / (maxColor - minColor); //b == maxColor
h /= 6.0; //to bring it to a number between 0 and 1
if(h < 0.0) h++;
}
ColorHSV colorHSV;
colorHSV.h = int(h * 256.0);
colorHSV.s = int(s * 256.0);
colorHSV.v = int(v * 256.0);
return colorHSV;
}
//Converts an HSV color to RGB color
ColorRGB HSVtoRGB(const ColorHSV& colorHSV)
{
float r, g, b, h, s, v; //this function works with floats between 0 and 1
h = colorHSV.h / 256.0;
s = colorHSV.s / 256.0;
v = colorHSV.v / 256.0;
//if saturation is 0, the color is a shade of grey
if(s == 0.0) r = g = b = v;
//if saturation > 0, more complex calculations are needed
else
{
float f, p, q, t;
int i;
h *= 6.0; //to bring hue to a number between 0 and 6, better for the calculations
i = int(floor(h)); //e.g. 2.7 becomes 2 and 3.01 becomes 3 or 4.9999 becomes 4
f = h - i;//the fractional part of h
p = v * (1.0 - s);
q = v * (1.0 - (s * f));
t = v * (1.0 - (s * (1.0 - f)));
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;
default: r=v; g=p; b=q; break; //this be case 5, it's mathematically impossible for i to be something else
}
}
ColorRGB colorRGB;
colorRGB.r = int(r * 256.0);
colorRGB.g = int(g * 256.0);
colorRGB.b = int(b * 256.0);
return colorRGB;
}
//converts an RGB color from 3 unsigned char's to a single integer
unsigned long RGBtoINT(const ColorRGB& colorRGB)
{
return 65536 * colorRGB.r + 256 * colorRGB.g + colorRGB.b;
}
//converts an RGB color given as a single integer to three unsigned char's
ColorRGB INTtoRGB(unsigned long colorINT)
{
ColorRGB colorRGB;
colorRGB.r = (colorINT / 65536) % 256;
colorRGB.g = (colorINT / 256) % 256;
colorRGB.b = colorINT % 256;
return colorRGB;
}
}