Irrlicht 3D Engine
 
Loading...
Searching...
No Matches
SColor.h
Go to the documentation of this file.
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#ifndef __COLOR_H_INCLUDED__
6#define __COLOR_H_INCLUDED__
7
8#include "irrTypes.h"
9#include "irrMath.h"
10
11namespace irr
12{
13namespace video
14{
16
57
58
60 inline u16 RGBA16(u32 r, u32 g, u32 b, u32 a=0xFF)
61 {
62 return (u16)((a & 0x80) << 8 |
63 (r & 0xF8) << 7 |
64 (g & 0xF8) << 2 |
65 (b & 0xF8) >> 3);
66 }
67
68
70 inline u16 RGB16(u32 r, u32 g, u32 b)
71 {
72 return RGBA16(r,g,b);
73 }
74
75
77 inline u16 RGB16from16(u16 r, u16 g, u16 b)
78 {
79 return (0x8000 |
80 (r & 0x1F) << 10 |
81 (g & 0x1F) << 5 |
82 (b & 0x1F));
83 }
84
85
88 {
89 return (u16)(0x8000 |
90 ( color & 0x00F80000) >> 9 |
91 ( color & 0x0000F800) >> 6 |
92 ( color & 0x000000F8) >> 3);
93 }
94
95
98 {
99 return (u16)(( color & 0x80000000) >> 16|
100 ( color & 0x00F80000) >> 9 |
101 ( color & 0x0000F800) >> 6 |
102 ( color & 0x000000F8) >> 3);
103 }
104
105
108 {
109 return (u16)(( color & 0x00F80000) >> 8 |
110 ( color & 0x0000FC00) >> 5 |
111 ( color & 0x000000F8) >> 3);
112 }
113
114
116
118 {
119 return ( (( -( (s32) color & 0x00008000 ) >> (s32) 31 ) & 0xFF000000 ) |
120 (( color & 0x00007C00 ) << 9) | (( color & 0x00007000 ) << 4) |
121 (( color & 0x000003E0 ) << 6) | (( color & 0x00000380 ) << 1) |
122 (( color & 0x0000001F ) << 3) | (( color & 0x0000001C ) >> 2)
123 );
124 }
125
126
129 {
130 return 0xFF000000 |
131 ((color & 0xF800) << 8)|
132 ((color & 0x07E0) << 5)|
133 ((color & 0x001F) << 3);
134 }
135
136
139 {
140 return 0x8000 | (((color & 0xFFC0) >> 1) | (color & 0x1F));
141 }
142
143
146 {
147 return (((color & 0x7FE0) << 1) | (color & 0x1F));
148 }
149
150
151
153
155 inline u32 getAlpha(u16 color)
156 {
157 return ((color >> 15)&0x1);
158 }
159
160
162
163 inline u32 getRed(u16 color)
164 {
165 return ((color >> 10)&0x1F);
166 }
167
168
170
171 inline u32 getGreen(u16 color)
172 {
173 return ((color >> 5)&0x1F);
174 }
175
176
178
179 inline u32 getBlue(u16 color)
180 {
181 return (color & 0x1F);
182 }
183
184
186 inline s32 getAverage(s16 color)
187 {
188 return ((getRed(color)<<3) + (getGreen(color)<<3) + (getBlue(color)<<3)) / 3;
189 }
190
191
193
201 class SColor
202 {
203 public:
204
206
208
210
211 SColor (u32 a, u32 r, u32 g, u32 b)
212 : color(((a & 0xff)<<24) | ((r & 0xff)<<16) | ((g & 0xff)<<8) | (b & 0xff)) {}
213
216 : color(clr) {}
217
219
221 u32 getAlpha() const { return color>>24; }
222
224
226 u32 getRed() const { return (color>>16) & 0xff; }
227
229
231 u32 getGreen() const { return (color>>8) & 0xff; }
232
234
236 u32 getBlue() const { return color & 0xff; }
237
240 {
242 }
243
246 {
247 return 0.3f*getRed() + 0.59f*getGreen() + 0.11f*getBlue();
248 }
249
252 {
253 return ( getRed() + getGreen() + getBlue() ) / 3;
254 }
255
257
259 void setAlpha(u32 a) { color = ((a & 0xff)<<24) | (color & 0x00ffffff); }
260
262
264 void setRed(u32 r) { color = ((r & 0xff)<<16) | (color & 0xff00ffff); }
265
267
269 void setGreen(u32 g) { color = ((g & 0xff)<<8) | (color & 0xffff00ff); }
270
272
274 void setBlue(u32 b) { color = (b & 0xff) | (color & 0xffffff00); }
275
277
279
281
284 void toOpenGLColor(u8* dest) const
285 {
286 *dest = (u8)getRed();
287 *++dest = (u8)getGreen();
288 *++dest = (u8)getBlue();
289 *++dest = (u8)getAlpha();
290 }
291
293
307 void set(u32 a, u32 r, u32 g, u32 b)
308 {
309 color = (((a & 0xff)<<24) | ((r & 0xff)<<16) | ((g & 0xff)<<8) | (b & 0xff));
310 }
311 void set(u32 col) { color = col; }
312
314
315 bool operator==(const SColor& other) const { return other.color == color; }
316
318
319 bool operator!=(const SColor& other) const { return other.color != color; }
320
322
323 bool operator<(const SColor& other) const { return (color < other.color); }
324
326
328 SColor operator+(const SColor& other) const
329 {
330 return SColor(core::min_(getAlpha() + other.getAlpha(), 255u),
331 core::min_(getRed() + other.getRed(), 255u),
332 core::min_(getGreen() + other.getGreen(), 255u),
333 core::min_(getBlue() + other.getBlue(), 255u));
334 }
335
337
340 SColor getInterpolated(const SColor &other, f32 d) const
341 {
342 d = core::clamp(d, 0.f, 1.f);
343 const f32 inv = 1.0f - d;
344 return SColor((u32)core::round32(other.getAlpha()*inv + getAlpha()*d),
345 (u32)core::round32(other.getRed()*inv + getRed()*d),
346 (u32)core::round32(other.getGreen()*inv + getGreen()*d),
347 (u32)core::round32(other.getBlue()*inv + getBlue()*d));
348 }
349
351
354 SColor getInterpolated_quadratic(const SColor& c1, const SColor& c2, f32 d) const
355 {
356 // this*(1-d)*(1-d) + 2 * c1 * (1-d) + c2 * d * d;
357 d = core::clamp(d, 0.f, 1.f);
358 const f32 inv = 1.f - d;
359 const f32 mul0 = inv * inv;
360 const f32 mul1 = 2.f * d * inv;
361 const f32 mul2 = d * d;
362
363 return SColor(
365 getAlpha() * mul0 + c1.getAlpha() * mul1 + c2.getAlpha() * mul2 ), 0, 255 ),
367 getRed() * mul0 + c1.getRed() * mul1 + c2.getRed() * mul2 ), 0, 255 ),
369 getGreen() * mul0 + c1.getGreen() * mul1 + c2.getGreen() * mul2 ), 0, 255 ),
371 getBlue() * mul0 + c1.getBlue() * mul1 + c2.getBlue() * mul2 ), 0, 255 ));
372 }
373
375
378 void setData(const void *data, ECOLOR_FORMAT format)
379 {
380 switch (format)
381 {
382 case ECF_A1R5G5B5:
383 color = A1R5G5B5toA8R8G8B8(*(u16*)data);
384 break;
385 case ECF_R5G6B5:
386 color = R5G6B5toA8R8G8B8(*(u16*)data);
387 break;
388 case ECF_A8R8G8B8:
389 color = *(u32*)data;
390 break;
391 case ECF_R8G8B8:
392 {
393 u8* p = (u8*)data;
394 set(255, p[0],p[1],p[2]);
395 }
396 break;
397 default:
398 color = 0xffffffff;
399 break;
400 }
401 }
402
404
407 void getData(void *data, ECOLOR_FORMAT format)
408 {
409 switch(format)
410 {
411 case ECF_A1R5G5B5:
412 {
413 u16 * dest = (u16*)data;
415 }
416 break;
417
418 case ECF_R5G6B5:
419 {
420 u16 * dest = (u16*)data;
422 }
423 break;
424
425 case ECF_R8G8B8:
426 {
427 u8* dest = (u8*)data;
428 dest[0] = (u8)getRed();
429 dest[1] = (u8)getGreen();
430 dest[2] = (u8)getBlue();
431 }
432 break;
433
434 case ECF_A8R8G8B8:
435 {
436 u32 * dest = (u32*)data;
437 *dest = color;
438 }
439 break;
440
441 default:
442 break;
443 }
444 }
445
448 };
449
450
452
459 {
460 public:
462
463 SColorf() : r(0.0f), g(0.0f), b(0.0f), a(1.0f) {}
464
466
476 SColorf(f32 r, f32 g, f32 b, f32 a = 1.0f) : r(r), g(g), b(b), a(a) {}
477
479
482 {
483 const f32 inv = 1.0f / 255.0f;
484 r = c.getRed() * inv;
485 g = c.getGreen() * inv;
486 b = c.getBlue() * inv;
487 a = c.getAlpha() * inv;
488 }
489
492 {
493 return SColor((u32)core::round32(a*255.0f), (u32)core::round32(r*255.0f), (u32)core::round32(g*255.0f), (u32)core::round32(b*255.0f));
494 }
495
497
503 void set(f32 rr, f32 gg, f32 bb) {r = rr; g =gg; b = bb; }
504
506
514 void set(f32 aa, f32 rr, f32 gg, f32 bb) {a = aa; r = rr; g =gg; b = bb; }
515
517
520 SColorf getInterpolated(const SColorf &other, f32 d) const
521 {
522 d = core::clamp(d, 0.f, 1.f);
523 const f32 inv = 1.0f - d;
524 return SColorf(other.r*inv + r*d,
525 other.g*inv + g*d, other.b*inv + b*d, other.a*inv + a*d);
526 }
527
529
533 f32 d) const
534 {
535 d = core::clamp(d, 0.f, 1.f);
536 // this*(1-d)*(1-d) + 2 * c1 * (1-d) + c2 * d * d;
537 const f32 inv = 1.f - d;
538 const f32 mul0 = inv * inv;
539 const f32 mul1 = 2.f * d * inv;
540 const f32 mul2 = d * d;
541
542 return SColorf (r * mul0 + c1.r * mul1 + c2.r * mul2,
543 g * mul0 + c1.g * mul1 + c2.g * mul2,
544 b * mul0 + c1.b * mul1 + c2.b * mul2,
545 a * mul0 + c1.a * mul1 + c2.a * mul2);
546 }
547
548
550 void setColorComponentValue(s32 index, f32 value)
551 {
552 switch(index)
553 {
554 case 0: r = value; break;
555 case 1: g = value; break;
556 case 2: b = value; break;
557 case 3: a = value; break;
558 }
559 }
560
562 f32 getAlpha() const { return a; }
563
565 f32 getRed() const { return r; }
566
568 f32 getGreen() const { return g; }
569
571 f32 getBlue() const { return b; }
572
575
578
581
584 };
585
586
588
593 {
594 public:
595 SColorHSL ( f32 h = 0.f, f32 s = 0.f, f32 l = 0.f )
596 : Hue ( h ), Saturation ( s ), Luminance ( l ) {}
597
598 void fromRGB(const SColorf &color);
599 void toRGB(SColorf &color) const;
600
604
605 private:
606 inline f32 toRGB1(f32 rm1, f32 rm2, f32 rh) const;
607
608 };
609
610 inline void SColorHSL::fromRGB(const SColorf &color)
611 {
612 const f32 maxVal = core::max_(color.getRed(), color.getGreen(), color.getBlue());
613 const f32 minVal = (f32)core::min_(color.getRed(), color.getGreen(), color.getBlue());
614 Luminance = (maxVal+minVal)*50;
615 if (core::equals(maxVal, minVal))
616 {
617 Hue=0.f;
618 Saturation=0.f;
619 return;
620 }
621
622 const f32 delta = maxVal-minVal;
623 if ( Luminance <= 50 )
624 {
625 Saturation = (delta)/(maxVal+minVal);
626 }
627 else
628 {
629 Saturation = (delta)/(2-maxVal-minVal);
630 }
631 Saturation *= 100;
632
633 if (core::equals(maxVal, color.getRed()))
634 Hue = (color.getGreen()-color.getBlue())/delta;
635 else if (core::equals(maxVal, color.getGreen()))
636 Hue = 2+((color.getBlue()-color.getRed())/delta);
637 else // blue is max
638 Hue = 4+((color.getRed()-color.getGreen())/delta);
639
640 Hue *= 60.0f;
641 while ( Hue < 0.f )
642 Hue += 360;
643 }
644
645
646 inline void SColorHSL::toRGB(SColorf &color) const
647 {
648 const f32 l = Luminance/100;
649 if (core::iszero(Saturation)) // grey
650 {
651 color.set(l, l, l);
652 return;
653 }
654
655 f32 rm2;
656
657 if ( Luminance <= 50 )
658 {
659 rm2 = l + l * (Saturation/100);
660 }
661 else
662 {
663 rm2 = l + (1 - l) * (Saturation/100);
664 }
665
666 const f32 rm1 = 2.0f * l - rm2;
667
668 const f32 h = Hue / 360.0f;
669 color.set( toRGB1(rm1, rm2, h + 1.f/3.f),
670 toRGB1(rm1, rm2, h),
671 toRGB1(rm1, rm2, h - 1.f/3.f)
672 );
673 }
674
675
676 // algorithm from Foley/Van-Dam
677 inline f32 SColorHSL::toRGB1(f32 rm1, f32 rm2, f32 rh) const
678 {
679 if (rh<0)
680 rh += 1;
681 if (rh>1)
682 rh -= 1;
683
684 if (rh < 1.f/6.f)
685 rm1 = rm1 + (rm2 - rm1) * rh*6.f;
686 else if (rh < 0.5f)
687 rm1 = rm2;
688 else if (rh < 2.f/3.f)
689 rm1 = rm1 + (rm2 - rm1) * ((2.f/3.f)-rh)*6.f;
690
691 return rm1;
692 }
693
694} // end namespace video
695} // end namespace irr
696
697#endif
Class representing a color in HSL format.
Definition SColor.h:593
void toRGB(SColorf &color) const
Definition SColor.h:646
void fromRGB(const SColorf &color)
Definition SColor.h:610
SColorHSL(f32 h=0.f, f32 s=0.f, f32 l=0.f)
Definition SColor.h:595
Class representing a 32 bit ARGB color.
Definition SColor.h:202
u32 color
color in A8R8G8B8 Format
Definition SColor.h:447
void setBlue(u32 b)
Sets the blue component of the Color.
Definition SColor.h:274
u32 getGreen() const
Returns the green component of the color.
Definition SColor.h:231
SColor getInterpolated(const SColor &other, f32 d) const
Interpolates the color with a f32 value to another color.
Definition SColor.h:340
bool operator!=(const SColor &other) const
Compares the color to another color.
Definition SColor.h:319
void setData(const void *data, ECOLOR_FORMAT format)
set the color by expecting data in the given format
Definition SColor.h:378
SColor operator+(const SColor &other) const
Adds two colors, result is clamped to 0..255 values.
Definition SColor.h:328
void toOpenGLColor(u8 *dest) const
Converts color to OpenGL color format.
Definition SColor.h:284
void set(u32 col)
Definition SColor.h:311
SColor(u32 clr)
Constructs the color from a 32 bit value. Could be another color.
Definition SColor.h:215
u32 getAverage() const
Get average intensity of the color in the range [0,255].
Definition SColor.h:251
bool operator==(const SColor &other) const
Compares the color to another color.
Definition SColor.h:315
u32 getRed() const
Returns the red component of the color.
Definition SColor.h:226
f32 getLuminance() const
Get luminance of the color in the range [0,255].
Definition SColor.h:245
void setAlpha(u32 a)
Sets the alpha component of the Color.
Definition SColor.h:259
void getData(void *data, ECOLOR_FORMAT format)
Write the color to data in the defined format.
Definition SColor.h:407
u32 getAlpha() const
Returns the alpha component of the color.
Definition SColor.h:221
void set(u32 a, u32 r, u32 g, u32 b)
Sets all four components of the color at once.
Definition SColor.h:307
void setRed(u32 r)
Sets the red component of the Color.
Definition SColor.h:264
f32 getLightness() const
Get lightness of the color in the range [0,255].
Definition SColor.h:239
SColor(u32 a, u32 r, u32 g, u32 b)
Constructs the color from 4 values representing the alpha, red, green and blue component.
Definition SColor.h:211
u16 toA1R5G5B5() const
Calculates a 16 bit A1R5G5B5 value of this color.
Definition SColor.h:278
SColor()
Constructor of the Color. Does nothing.
Definition SColor.h:207
SColor getInterpolated_quadratic(const SColor &c1, const SColor &c2, f32 d) const
Returns interpolated color. ( quadratic )
Definition SColor.h:354
bool operator<(const SColor &other) const
comparison operator
Definition SColor.h:323
void setGreen(u32 g)
Sets the green component of the Color.
Definition SColor.h:269
u32 getBlue() const
Returns the blue component of the color.
Definition SColor.h:236
Class representing a color with four floats.
Definition SColor.h:459
f32 getRed() const
Returns the red component of the color in the range 0.0 to 1.0.
Definition SColor.h:565
SColorf getInterpolated(const SColorf &other, f32 d) const
Interpolates the color with a f32 value to another color.
Definition SColor.h:520
f32 getGreen() const
Returns the green component of the color in the range 0.0 to 1.0.
Definition SColor.h:568
SColorf()
Default constructor for SColorf.
Definition SColor.h:463
f32 getBlue() const
Returns the blue component of the color in the range 0.0 to 1.0.
Definition SColor.h:571
SColorf(SColor c)
Constructs a color from 32 bit Color.
Definition SColor.h:481
void setColorComponentValue(s32 index, f32 value)
Sets a color component by index. R=0, G=1, B=2, A=3.
Definition SColor.h:550
f32 b
blue component
Definition SColor.h:580
void set(f32 rr, f32 gg, f32 bb)
Sets three color components to new values at once.
Definition SColor.h:503
f32 getAlpha() const
Returns the alpha component of the color in the range 0.0 (transparent) to 1.0 (opaque)
Definition SColor.h:562
f32 r
red color component
Definition SColor.h:574
f32 g
green color component
Definition SColor.h:577
SColor toSColor() const
Converts this color to a SColor without floats.
Definition SColor.h:491
SColorf(f32 r, f32 g, f32 b, f32 a=1.0f)
Constructs a color from up to four color values: red, green, blue, and alpha.
Definition SColor.h:476
f32 a
alpha color component
Definition SColor.h:583
void set(f32 aa, f32 rr, f32 gg, f32 bb)
Sets all four color components to new values at once.
Definition SColor.h:514
SColorf getInterpolated_quadratic(const SColorf &c1, const SColorf &c2, f32 d) const
Returns interpolated color. ( quadratic )
Definition SColor.h:532
const T & max_(const T &a, const T &b)
returns maximum of two values. Own implementation to get rid of the STL (VS6 problems)
Definition irrMath.h:137
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.h:166
const T & min_(const T &a, const T &b)
returns minimum of two values. Own implementation to get rid of the STL (VS6 problems)
Definition irrMath.h:123
bool equals(const f64 a, const f64 b, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals b, taking possible rounding errors into account
Definition irrMath.h:185
REALINLINE s32 floor32(f32 x)
Definition irrMath.h:613
REALINLINE s32 round32(f32 x)
Definition irrMath.h:680
bool iszero(const f64 a, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals zero, taking rounding errors into account
Definition irrMath.h:270
u16 A8R8G8B8toR5G6B5(u32 color)
Converts a 32bit (A8R8G8B8) color to a 16bit R5G6B5 color.
Definition SColor.h:107
ECOLOR_FORMAT
An enum for the color format of textures used by the Irrlicht Engine.
Definition SColor.h:18
@ ECF_A1R5G5B5
16 bit color format used by the software driver.
Definition SColor.h:23
@ ECF_UNKNOWN
Unknown color format:
Definition SColor.h:55
@ ECF_A32B32G32R32F
128 bit floating point format. 32 bits are used for the red, green, blue and alpha channels.
Definition SColor.h:52
@ ECF_R8G8B8
24 bit color, no alpha channel, but 8 bit for red, green and blue.
Definition SColor.h:29
@ ECF_R5G6B5
Standard 16 bit color format.
Definition SColor.h:26
@ ECF_A8R8G8B8
Default 32 bit color format. 8 bits are used for every component: red, green, blue and alpha.
Definition SColor.h:32
@ ECF_A16B16G16R16F
64 bit floating point format 16 bits are used for the red, green, blue and alpha channels.
Definition SColor.h:43
@ ECF_G32R32F
64 bit floating point format using 32 bits for the red channel and 32 bits for the green channel.
Definition SColor.h:49
@ ECF_R16F
16 bit floating point format using 16 bits for the red channel.
Definition SColor.h:37
@ ECF_R32F
32 bit floating point format using 32 bits for the red channel.
Definition SColor.h:46
@ ECF_G16R16F
32 bit floating point format using 16 bits for the red channel and 16 bits for the green channel.
Definition SColor.h:40
u16 RGB16from16(u16 r, u16 g, u16 b)
Creates a 16bit A1R5G5B5 color, based on 16bit input values.
Definition SColor.h:77
u32 getAlpha(u16 color)
Returns the alpha component from A1R5G5B5 color.
Definition SColor.h:155
u32 getGreen(u16 color)
Returns the green component from A1R5G5B5 color.
Definition SColor.h:171
u32 R5G6B5toA8R8G8B8(u16 color)
Returns A8R8G8B8 Color from R5G6B5 color.
Definition SColor.h:128
u16 A1R5G5B5toR5G6B5(u16 color)
Returns R5G6B5 Color from A1R5G5B5 color.
Definition SColor.h:145
u16 RGB16(u32 r, u32 g, u32 b)
Creates a 16 bit A1R5G5B5 color.
Definition SColor.h:70
u16 RGBA16(u32 r, u32 g, u32 b, u32 a=0xFF)
Creates a 16 bit A1R5G5B5 color.
Definition SColor.h:60
u16 X8R8G8B8toA1R5G5B5(u32 color)
Converts a 32bit (X8R8G8B8) color to a 16bit A1R5G5B5 color.
Definition SColor.h:87
u32 A1R5G5B5toA8R8G8B8(u16 color)
Convert A8R8G8B8 Color from A1R5G5B5 color.
Definition SColor.h:117
u16 A8R8G8B8toA1R5G5B5(u32 color)
Converts a 32bit (A8R8G8B8) color to a 16bit A1R5G5B5 color.
Definition SColor.h:97
u32 getRed(u16 color)
Returns the red component from A1R5G5B5 color.
Definition SColor.h:163
u16 R5G6B5toA1R5G5B5(u16 color)
Returns A1R5G5B5 Color from R5G6B5 color.
Definition SColor.h:138
s32 getAverage(s16 color)
Returns the average from a 16 bit A1R5G5B5 color.
Definition SColor.h:186
u32 getBlue(u16 color)
Returns the blue component from A1R5G5B5 color.
Definition SColor.h:179
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
float f32
32 bit floating point variable.
Definition irrTypes.h:104
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
signed short s16
16 bit signed variable.
Definition irrTypes.h:48
unsigned char u8
8 bit unsigned variable.
Definition irrTypes.h:18
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
unsigned short u16
16 bit unsigned variable.
Definition irrTypes.h:40