ADTF Display Toolbox
Loading...
Searching...
No Matches
color.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <adtf_base.h>
12
13namespace adtf
14{
15
16namespace disptb
17{
18
19namespace graphicslib
20{
21
22namespace dengar
23{
24
30class cColor : public tColor, public IColor
31{
32public:
37 {
38 this->nRed = 0;
39 this->nGreen = 0;
40 this->nBlue = 0;
41 this->nAlpha = 0;
42 }
43
49 cColor(const IColor* pColor)
50 {
51 this->nRed = pColor->GetRed();
52 this->nGreen = pColor->GetGreen();
53 this->nBlue = pColor->GetBlue();
54 this->nAlpha = pColor->GetAlpha();
55 }
56
62 cColor(const tColor& oColor)
63 {
64 this->nRed = oColor.nRed;
65 this->nGreen = oColor.nGreen;
66 this->nBlue = oColor.nBlue;
67 this->nAlpha = oColor.nAlpha;
68 }
69
77 cColor(tInt nRedLocal, tInt nGreenLocal, tInt nBlueLocal, tInt nAlphaLocal = 255)
78 {
79 this->nRed = (tUInt8) nRedLocal;
80 this->nGreen = (tUInt8) nGreenLocal;
81 this->nBlue = (tUInt8) nBlueLocal;
82 this->nAlpha = (tUInt8) nAlphaLocal;
83 }
84
92 cColor(tUInt8 nRed, tUInt8 nGreen, tUInt8 nBlue, tUInt8 nAlpha = 255)
93 {
94 this->nRed = (tUInt8) nRed;
95 this->nGreen = (tUInt8) nGreen;
96 this->nBlue = (tUInt8) nBlue;
97 this->nAlpha = (tUInt8) nAlpha;
98 }
99
107 cColor(tFloat32 fRed, tFloat32 fGreen, tFloat32 fBlue, tFloat32 fAlpha = 1.0f)
108 {
109 this->nRed = (tUInt8) (fRed * 255.0f);
110 this->nGreen = (tUInt8) (fGreen * 255.0f);
111 this->nBlue = (tUInt8) (fBlue * 255.0f);
112 this->nAlpha = (tUInt8) (fAlpha * 255.0f);
113 }
114
122 cColor(tUInt32 nRGBA)
123 {
124 SetRGBA(nRGBA);
125 }
126
135 cColor(tUInt32 nRGBA, tInt oPixelFormat)
136 {
137 SetRGBA(nRGBA, oPixelFormat);
138 }
139
143 tVoid Release()
144 {
145 delete this;
146 }
147
160 tVoid Set(tInt nRedLocal, tInt nGreenLocal, tInt nBlueLocal, tInt nAlphaLocal = 255)
161 {
162 this->nRed = (tUInt8) nRedLocal;
163 this->nGreen = (tUInt8) nGreenLocal;
164 this->nBlue = (tUInt8) nBlueLocal;
165 this->nAlpha = (tUInt8) nAlphaLocal;
166 }
167
180 tVoid Set(tUInt8 nRedLocal, tUInt8 nGreenLocal, tUInt8 nBlueLocal, tUInt8 nAlphaLocal = 255)
181 {
182 this->nRed = nRedLocal;
183 this->nGreen = nGreenLocal;
184 this->nBlue = nBlueLocal;
185 this->nAlpha = nAlphaLocal;
186 }
187
198 tVoid Set(tFloat32 fRed, tFloat32 fGreen, tFloat32 fBlue, tFloat32 fAlpha = 1.0f)
199 {
200 this->nRed = (tUInt8) (fRed * 255.0f);
201 this->nGreen = (tUInt8) (fGreen * 255.0f);
202 this->nBlue = (tUInt8) (fBlue * 255.0f);
203 this->nAlpha = (tUInt8) (fAlpha * 255.0f);
204 }
205
213 tVoid SetRGBA(tUInt32 nRGBA)
214 {
216 }
217
227 tVoid SetRGBA(tUInt32 nRGBA, tInt oPixelFormat)
228 {
229 switch (oPixelFormat)
230 {
232 nBlue = (tUInt8) ((nRGBA & 0xf) << 4);
233 nGreen = (tUInt8) (nRGBA & 0xf0);
234 nRed = (tUInt8) ((nRGBA & 0xf00) >> 4);
235 nAlpha = 0xff;
236 break;
237
239 nBlue = (tUInt8) ((nRGBA & 0xf) << 4);
240 nGreen = (tUInt8) (nRGBA & 0xf0);
241 nRed = (tUInt8) ((nRGBA & 0xf00) >> 4);
242 nAlpha = (tUInt8) ((nRGBA & 0xf000) >> 8);
243 break;
244
246 nBlue = (tUInt8) ((nRGBA & 0x1f) << 3);
247 nGreen = (tUInt8) ((nRGBA & 0x3e0) >> 2);
248 nRed = (tUInt8) ((nRGBA & 0x7c00) >> 7);
249 nAlpha = 0xff;
250 break;
251
253 nBlue = (tUInt8) ((nRGBA & 0x1f) << 3);
254 nGreen = (tUInt8) ((nRGBA & 0x7e0) >> 3);
255 nRed = (tUInt8) ((nRGBA & 0xf800) >> 8);
256 nAlpha = 0xff;
257 break;
258
260 nBlue = ((tUInt8*) &nRGBA)[0];
261 nGreen = ((tUInt8*) &nRGBA)[1];
262 nRed = ((tUInt8*) &nRGBA)[2];
263 nAlpha = 0xff;
264 break;
265
267 nBlue = ((tUInt8*) &nRGBA)[2];
268 nGreen = ((tUInt8*) &nRGBA)[1];
269 nRed = ((tUInt8*) &nRGBA)[0];
270 nAlpha = 0xff;
271 break;
272
274 nBlue = ((tUInt8*) &nRGBA)[0];
275 nGreen = ((tUInt8*) &nRGBA)[1];
276 nRed = ((tUInt8*) &nRGBA)[2];
277 nAlpha = ((tUInt8*) &nRGBA)[3];
278 break;
279
281 nBlue = ((tUInt8*) &nRGBA)[2];
282 nGreen = ((tUInt8*) &nRGBA)[1];
283 nRed = ((tUInt8*) &nRGBA)[0];
284 nAlpha = ((tUInt8*) &nRGBA)[3];
285 break;
286
288 nBlue = ((tUInt8*) &nRGBA)[1];
289 nGreen = ((tUInt8*) &nRGBA)[2];
290 nRed = ((tUInt8*) &nRGBA)[3];
291 nAlpha = ((tUInt8*) &nRGBA)[0];
292 break;
293
295 nBlue = ((tUInt8*) &nRGBA)[3];
296 nGreen = ((tUInt8*) &nRGBA)[2];
297 nRed = ((tUInt8*) &nRGBA)[1];
298 nAlpha = ((tUInt8*) &nRGBA)[0];
299 break;
300 }
301 }
302
312 tUInt32 GetRGBA() const
313 {
315 }
316
324 tUInt32 GetRGBA(tInt oPixelFormat) const
325 {
326 tUInt32 nResult = 0;
327 tUInt8* pResult = (tUInt8*) &nResult;
328
329 switch (oPixelFormat)
330 {
332 nResult = tUInt32(nBlue >> 4) | tUInt32(nGreen & 0xf0) | tUInt32((nRed & 0xf0) << 4);
333 break;
334
336 nResult = tUInt32(nBlue >> 4) | tUInt32(nGreen & 0xf0) | tUInt32((nRed & 0xf0) << 4) |
337 tUInt32((nAlpha & 0xf0) << 8);
338 break;
339
341 nResult = tUInt32(nBlue >> 3) | tUInt32((nGreen & 0xf8) << 2) | tUInt32((nRed & 0xf8) << 7);
342 break;
343
345 nResult = tUInt32(nBlue >> 3) | tUInt32((nGreen & 0xfc) << 3) | tUInt32((nRed & 0xf8) << 8);
346 break;
347
349 pResult[0] = nBlue;
350 pResult[1] = nGreen;
351 pResult[2] = nRed;
352 break;
353
355 pResult[0] = nRed;
356 pResult[1] = nGreen;
357 pResult[2] = nBlue;
358 break;
359
361 pResult[0] = nBlue;
362 pResult[1] = nGreen;
363 pResult[2] = nRed;
364 pResult[3] = nAlpha;
365 break;
366
368 pResult[0] = nRed;
369 pResult[1] = nGreen;
370 pResult[2] = nBlue;
371 pResult[3] = nAlpha;
372 break;
373
375 pResult[0] = nAlpha;
376 pResult[1] = nBlue;
377 pResult[2] = nGreen;
378 pResult[3] = nRed;
379 break;
380
382 pResult[0] = nAlpha;
383 pResult[1] = nRed;
384 pResult[2] = nGreen;
385 pResult[3] = nBlue;
386 break;
387 }
388
389 return nResult;
390 }
391
399 operator tUInt32()
400 {
402 }
403
411 tVoid SetRed(tUInt8 nRedValue)
412 {
413 this->nRed = nRedValue;
414 }
415
423 tUInt8 GetRed() const
424 {
425 return nRed;
426 }
427
435 tVoid SetGreen(tUInt8 nGreenValue)
436 {
437 this->nGreen = nGreenValue;
438 }
439
447 tUInt8 GetGreen() const
448 {
449 return nGreen;
450 }
451
459 tVoid SetBlue(tUInt8 nBlueValue)
460 {
461 this->nBlue = nBlueValue;
462 }
463
471 tUInt8 GetBlue() const
472 {
473 return nBlue;
474 }
475
483 tVoid SetAlpha(tUInt8 nAlphaValue)
484 {
485 this->nAlpha = nAlphaValue;
486 }
487
495 tUInt8 GetAlpha() const
496 {
497 return nAlpha;
498 }
499
507 tUInt8 GetBrightness() const
508 {
509 return (tUInt8) ((116 * nRed + 602 * nGreen + 306 * nBlue) >> 10);
510 }
511
512public:
520 static cColor Scale(const cColor& c, tInt nAlpha)
521 {
522 return cColor(c.nRed * nAlpha / 255, c.nGreen * nAlpha / 255, c.nBlue * nAlpha / 255);
523 }
524
532 static cColor Scale(const cColor& c, tFloat32 fAlpha)
533 {
534 return cColor(c.nRed * fAlpha, c.nGreen * fAlpha, c.nBlue * fAlpha);
535 }
536
545 static cColor Blend(const cColor& c1, const cColor& c2, tInt nAlpha)
546 {
547 tUInt8 nInvAlpha = (tUInt8) (255 - nAlpha);
548
549 return cColor((c1.nRed * nAlpha + c2.nRed * nInvAlpha) / 255,
550 (c1.nGreen * nAlpha + c2.nGreen * nInvAlpha) / 255,
551 (c1.nBlue * nAlpha + c2.nBlue * nInvAlpha) / 255);
552 }
553
562 static cColor Blend(const cColor& c1, const cColor& c2, tFloat32 fAlpha)
563 {
564 tUInt8 nAlpha = (tUInt8) (fAlpha * 255.0f);
565 tUInt8 nInvAlpha = (tUInt8) (255 - nAlpha);
566
567 return cColor((c1.nRed * nAlpha + c2.nRed * nInvAlpha) / 255,
568 (c1.nGreen * nAlpha + c2.nGreen * nInvAlpha) / 255,
569 (c1.nBlue * nAlpha + c2.nBlue * nInvAlpha) / 255);
570 }
571
583 static tResult ColorFromString(const tChar* strColor, cColor& oColor)
584 {
585 RETURN_IF_POINTER_NULL(strColor);
586
587 if (strColor[0] != '#')
588 {
589 RETURN_ERROR(ERR_INVALID_ARG);
590 }
591
592 tInt r, g, b, a;
593 r = g = b = 0;
594 a = 255;
595
596 for (tUInt nIdx = 1; nIdx < strlen(strColor); nIdx++)
597 {
598 tInt nVal = 0;
599 tChar cChar = strColor[nIdx];
600
601 switch (cChar)
602 {
603 case '0':
604 case '1':
605 case '2':
606 case '3':
607 case '4':
608 case '5':
609 case '6':
610 case '7':
611 case '8':
612 case '9': nVal = cChar - '0'; break;
613 case 'a':
614 case 'b':
615 case 'c':
616 case 'd':
617 case 'e':
618 case 'f': nVal = cChar - 'a' + 10; break;
619 case 'A':
620 case 'B':
621 case 'C':
622 case 'D':
623 case 'E':
624 case 'F': nVal = cChar - 'A' + 10; break;
625 default: RETURN_ERROR(ERR_INVALID_ARG);
626 }
627
628 switch (nIdx)
629 {
630 case 1: r = nVal * 16; break;
631 case 2: r = r + nVal; break;
632 case 3: g = nVal * 16; break;
633 case 4: g = g + nVal; break;
634 case 5: b = nVal * 16; break;
635 case 6: b = b + nVal; break;
636 case 7: a = nVal * 16; break;
637 case 8: a = a + nVal; break;
638 }
639 }
640
641 oColor.Set(r, g, b, a);
642 RETURN_NOERROR;
643 }
644
650 explicit operator IColor*()
651 {
652 return static_cast<IColor*>(this);
653 }
654
660 explicit operator const IColor*() const
661 {
662 return static_cast<const IColor*>(this);
663 }
664
665public:
666 static const cColor Black;
667 static const cColor White;
668 static const cColor Red;
669 static const cColor Green;
670 static const cColor Blue;
671 static const cColor Yellow;
672 static const cColor Cyan;
673 static const cColor Purple;
674};
675
676inline const cColor cColor::Black(0, 0, 0);
677inline const cColor cColor::White(255, 255, 255);
678inline const cColor cColor::Red(255, 0, 0);
679inline const cColor cColor::Green(0, 255, 0);
680inline const cColor cColor::Blue(0, 0, 255);
681inline const cColor cColor::Yellow(255, 255, 0);
682inline const cColor cColor::Cyan(0, 255, 255);
683inline const cColor cColor::Purple(255, 0, 255);
684
685} // namespace dengar
686
687using dengar::cColor;
688} // namespace graphicslib
689
690} // namespace disptb
691
692} // namespace adtf
cColor()
default constructor
Definition color.h:36
Interface for color representation.
Definition color_intf.h:27
virtual tUInt8 GetRed() const =0
Returns the value for the red channel.
virtual tUInt8 GetAlpha() const =0
This function returns the value for the alpha channel.
virtual tUInt8 GetBlue() const =0
Returns the value for the blue channel.
virtual tUInt8 GetGreen() const =0
Returns the value for the green channel.
@ PF_RGB_888
24 bit RGB (R: 8 bit, G: 8 bit, B: 8 bit) - default format for 24 bpp
Definition image_intf.h:111
@ PF_RGBA_8888
32 bit RGBA (R: 8 bit, G: 8 bit, B: 8 bit, A: 8 bit) with alpha value - default format for 32 bpp
Definition image_intf.h:123
@ PF_RGB_555
15 bit RGB (R: 5 bit, G: 5 bit, B: 5 bit) - default format for 16 bpp
Definition image_intf.h:88
@ PF_RGB_565
16 bit RGB (R: 5 bit, G: 6 bit, B: 5 bit)
Definition image_intf.h:90
@ PF_BGRA_8888
32 bit BGRA (B: 8 bit, G: 8 bit, R: 8 bit, A: 8 bit) with alpha value - inverted RGB + A
Definition image_intf.h:125
@ PF_ARGB_8888
32 bit ARGB (A: 8 bit, R: 8 bit, G: 8 bit, B: 8 bit) with alpha value
Definition image_intf.h:119
@ PF_ABGR_8888
32 bit ABGR (A: 8 bit, B: 8 bit, G: 8 bit, R: 8 bit) with alpha value - inverted RGBA
Definition image_intf.h:121
@ PF_RGB_444
12 bit RGB (R: 4 bit, G: 4 bit, B: 4 bit)
Definition image_intf.h:86
@ PF_BGR_888
24 bit BGR (B: 8 bit, G: 8 bit, R: 8 bit) - inverted RGB
Definition image_intf.h:113
@ PF_RGBA_4444
16 bit RGBA (R: 4 bit, G: 4 bit, B: 4 bit, A: 4 bit) with alpha value
Definition image_intf.h:92
static cColor Blend(const cColor &c1, const cColor &c2, tFloat32 fAlpha)
blends two colors with an alpha channel
Definition color.h:562
tVoid SetAlpha(tUInt8 nAlphaValue)
Sets the value of the alpha channel.
Definition color.h:483
tVoid SetBlue(tUInt8 nBlueValue)
Sets the value of the blue channel.
Definition color.h:459
static const cColor Yellow
definition of color yellow
Definition color.h:671
tVoid SetRGBA(tUInt32 nRGBA, tInt oPixelFormat)
Method which decodes the rgba-values stored in nRGBA depending on the given pixelformat.
Definition color.h:227
static const cColor Red
definition of color red
Definition color.h:668
tVoid Release()
Releases this color.
Definition color.h:143
cColor(tFloat32 fRed, tFloat32 fGreen, tFloat32 fBlue, tFloat32 fAlpha=1.0f)
constructor with several init parameters.
Definition color.h:107
tUInt8 GetBlue() const
Returns the value for the green channel.
Definition color.h:471
tUInt8 GetRed() const
Returns the value for the red channel.
Definition color.h:423
tUInt8 GetBrightness() const
This function returns the brightness value.
Definition color.h:507
cColor(const IColor *pColor)
constructor from IColor
Definition color.h:49
static const cColor Purple
definition of color purple
Definition color.h:673
static const cColor White
definition of color white
Definition color.h:667
tVoid SetGreen(tUInt8 nGreenValue)
Sets the value of the green channel.
Definition color.h:435
tUInt8 GetAlpha() const
This function returns the value for the alpha channel.
Definition color.h:495
tVoid Set(tInt nRedLocal, tInt nGreenLocal, tInt nBlueLocal, tInt nAlphaLocal=255)
Method to set color and transparency information.
Definition color.h:160
static cColor Scale(const cColor &c, tFloat32 fAlpha)
scales a color with an alpha channel
Definition color.h:532
tVoid SetRGBA(tUInt32 nRGBA)
method to set color and transparency with a combined parameter (3 color channels and the alphachannel...
Definition color.h:213
cColor(const tColor &oColor)
Constructor.
Definition color.h:62
tUInt32 GetRGBA(tInt oPixelFormat) const
Gets a four byte balue containing the current rgba-values encoded in the given pixel format.
Definition color.h:324
tVoid SetRed(tUInt8 nRedValue)
Sets the value of the red channel.
Definition color.h:411
tVoid Set(tFloat32 fRed, tFloat32 fGreen, tFloat32 fBlue, tFloat32 fAlpha=1.0f)
Method to set color and transparency information.
Definition color.h:198
static cColor Scale(const cColor &c, tInt nAlpha)
scales a color with an alpha channel
Definition color.h:520
cColor(tUInt8 nRed, tUInt8 nGreen, tUInt8 nBlue, tUInt8 nAlpha=255)
constructor with several init parameters.
Definition color.h:92
static cColor Blend(const cColor &c1, const cColor &c2, tInt nAlpha)
blends two colors with an alpha channel
Definition color.h:545
static const cColor Black
definition of color black
Definition color.h:666
static const cColor Blue
definition of color blue
Definition color.h:670
cColor(tUInt32 nRGBA)
Constructor with a bitfield containing the encoded rgba-values.
Definition color.h:122
static const cColor Cyan
definition of color cyan
Definition color.h:672
tVoid Set(tUInt8 nRedLocal, tUInt8 nGreenLocal, tUInt8 nBlueLocal, tUInt8 nAlphaLocal=255)
Method to set color and transparency information.
Definition color.h:180
tUInt32 GetRGBA() const
Gets a four Byte value containing the 3 color channels and the alphachannel.
Definition color.h:312
cColor(tUInt32 nRGBA, tInt oPixelFormat)
Constructor with a bitfield containing the encoded rgba-values and another parameter which describes ...
Definition color.h:135
cColor(tInt nRedLocal, tInt nGreenLocal, tInt nBlueLocal, tInt nAlphaLocal=255)
constructor with several init parameters.
Definition color.h:77
static const cColor Green
definition of color green
Definition color.h:669
tUInt8 GetGreen() const
Returns the value for the green channel.
Definition color.h:447
static tResult ColorFromString(const tChar *strColor, cColor &oColor)
Converts a hexadecimal color value into a cColor object The input format is like the HTML color defin...
Definition color.h:583
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Namespace for functionality provided by the Display Toolbox.
Main namespace.