TGX 1.1.2
A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.
Loading...
Searching...
No Matches
Color.h
Go to the documentation of this file.
1
5//
6// Copyright 2020 Arvind Singh
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11//version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; If not, see <http://www.gnu.org/licenses/>.
20
21#ifndef _TGX_COLOR_H_
22#define _TGX_COLOR_H_
23
24// only C++, no plain C
25#ifdef __cplusplus
26
27#include "Misc.h"
28
29#include <stdint.h>
30#include <math.h>
31#include <type_traits>
32
33#include "Vec3.h"
34#include "Vec4.h"
35
36namespace tgx
37{
38
39
40// ********************************************************************************
41// Color types.
42//
43// The following color types are available for use with the tgx library:
44//
45// - RGB565: 16bits, R:5 G:6 B:5 colors
46// no alpha channel wrapper around a uint16_t integer, aligned as uint16_t
47//
48// - RGB24: 24bits, R:8 G:8 B:8 colors.
49// no alpha channel, not aligned in memory
50//
51// - RGB32: 32bits, R:8 G:8 B:8 A:8 colors.
52// alpha channel, wrapper around a uint32_t integer, aligned as uint32_t
53//
54// - RGB64: 64bits, R:16 G:16 B:16 A:16 colors.
55// alpha channel, wrapper around a uint64_t integer, aligned as uint64_t
56//
57// - RGBf: 96bits, R:float G:float, B:float
58// no alpha channel, aligned as float.
59//
60// - HSV: 96bits, H:float, S:float, V:float
61// hue / saturation / value color space: very slow
62//
63//
64// REMARKS:
65//
66// 1. RGB565, RGB32 and RGB64 are wrappers around basic integer types they can be
67// used as drop in remplacment of uint16_t, uint32_t and uint64_t without any
68// speed penalty.
69//
70// For example:
71// tgx::RGB565 col(12,63,30)
72// uint16_t v = (uint16_t)col; // <- conversion to uint16_t
73// tgx::RGB565 col2 = tgx::RGB565(v) // <- conversion back to RGB565
74//
75// 2. RGB32 and RGB64 have an alpha channel. The color are always assumed to have
76// pre-multiplied alpha.
77//
78// 3. Fast conversion is implemented between color types (and also integer types)
79// except when converting from/to HSV which is slow.
80//
81// ********************************************************************************
82
83
84
85
86// For each RGB color type, we decide separately whether color components
87// are ordered in memory as R,G,B or B,G,R.
88// The default ordering below can overridden be #definining the constants
89// before including this header
90
91#ifndef TGX_RGB565_ORDER_BGR
92#define TGX_RGB565_ORDER_BGR 1
93#endif
94
95#ifndef TGX_RGB24_ORDER_BGR
96#define TGX_RGB24_ORDER_BGR 0
97#endif
98
99#ifndef TGX_RGB32_ORDER_BGR
100#define TGX_RGB32_ORDER_BGR 1
101#endif
102
103#ifndef TGX_RGB64_ORDER_BGR
104#define TGX_RGB64_ORDER_BGR 0
105#endif
106
107#ifndef TGX_RGBf_ORDER_BGR
108#define TGX_RGBf_ORDER_BGR 0
109#endif
110
111
112
113// Forward declarations
114
115struct RGB565; // color in 16-bit R5/G6/B5 format (2 bytes aligned) - convertible to/from uint16_t
116
117struct RGB24; // color in 24-bit R8/G8/B8/ format (unaligned !).
118
119struct RGB32; // color in 32-bit R8/G8/B8/(A8) format (4 bytes aligned) - convertible to/from uint32_t
120
121struct RGB64; // color in 64 bit R16/G16/B16/(A16) format (8 bytes aligned) - convertible to/from uint64_t
122
123struct RGBf; // color in RGB float format (4 bytes aligned).
124
125struct HSV; // color in H/S/V float format (4 bytes aligned).
126
127
128
129
133template<typename T> struct id_color_type
134 {
135 static const int value =
136 (std::is_same<T, RGB565>::value) ? 1 : (
137 (std::is_same<T, RGB24>::value) ? 2 : (
138 (std::is_same<T, RGB32>::value) ? 3 : (
139 (std::is_same<T, RGB64>::value) ? 4 : (
140 (std::is_same<T, RGBf>::value) ? 5 : (
141 (std::is_same<T, HSV>::value) ? 6 : 0)))));
142 };
143
144
148template<typename T> struct is_color
149 {
150 static const bool value = (id_color_type<T>::value != 0);
151 };
152
153
154
155
156
157
158// Predefined colors in RGB32 format
159
160extern const RGB32 RGB32_Black;
161extern const RGB32 RGB32_White;
162extern const RGB32 RGB32_Red;
163extern const RGB32 RGB32_Blue;
164extern const RGB32 RGB32_Green;
165extern const RGB32 RGB32_Purple;
166extern const RGB32 RGB32_Orange;
167extern const RGB32 RGB32_Cyan;
168extern const RGB32 RGB32_Lime;
169extern const RGB32 RGB32_Salmon;
170extern const RGB32 RGB32_Maroon;
171extern const RGB32 RGB32_Yellow;
172extern const RGB32 RGB32_Magenta;
173extern const RGB32 RGB32_Olive;
174extern const RGB32 RGB32_Teal;
175extern const RGB32 RGB32_Gray;
176extern const RGB32 RGB32_Silver;
177extern const RGB32 RGB32_Navy;
178extern const RGB32 RGB32_Transparent;
179
180
181
182// Predefined colors in RGB565 format
183
184extern const RGB565 RGB565_Black;
185extern const RGB565 RGB565_White;
186extern const RGB565 RGB565_Red;
187extern const RGB565 RGB565_Blue;
188extern const RGB565 RGB565_Green;
189extern const RGB565 RGB565_Purple;
190extern const RGB565 RGB565_Orange;
191extern const RGB565 RGB565_Cyan;
192extern const RGB565 RGB565_Lime;
193extern const RGB565 RGB565_Salmon;
194extern const RGB565 RGB565_Maroon;
195extern const RGB565 RGB565_Yellow;
196extern const RGB565 RGB565_Magenta;
197extern const RGB565 RGB565_Olive;
198extern const RGB565 RGB565_Teal;
199extern const RGB565 RGB565_Gray;
200extern const RGB565 RGB565_Silver;
201extern const RGB565 RGB565_Navy;
202
203
204
205
215struct RGB565
216 {
217
218 // mtools extension (if available).
219 #if (MTOOLS_TGX_EXTENSIONS)
220 #include <mtools/extensions/tgx/tgx_ext_Color_RGB565.inl>
221 #endif
222
223
224 // dual memory representation
225 union
226 {
227 uint16_t val;
228
229 struct {
230#if TGX_RGB565_ORDER_BGR
231 uint16_t B : 5,
232 G : 6,
233 R : 5;
234#else
235 uint16_t R : 5,
236 G : 6,
237 B : 5;
238#endif
239 };
240 };
241
242
246 RGB565() = default;
247
248
256 constexpr RGB565(int r, int g, int b) :
258 B((uint8_t)b), G((uint8_t)g), R((uint8_t)r)
259 #else
260 R((uint8_t)r), G((uint8_t)g), B((uint8_t)b)
261 #endif
262 {}
263
264
272 constexpr RGB565(iVec3 v) : RGB565(v.x, v.y, v.z)
273 {}
274
275
284 constexpr RGB565(iVec4 v) : RGB565(v.x, v.y, v.z)
285 {}
286
287
291 RGB565(float r, float g, float b) :
293 B((uint8_t)(b * 31)),
294 G((uint8_t)(g * 63)),
295 R((uint8_t)(r * 31))
296 #else
297 R((uint8_t)(r * 31)),
298 G((uint8_t)(g * 63)),
299 B((uint8_t)(b * 31))
300 #endif
301 {}
302
303
307 RGB565(fVec3 v) : RGB565(v.x, v.y, v.z)
308 {}
309
310
314 RGB565(fVec4 v) : RGB565(v.x, v.y, v.z)
315 {}
316
317
321 constexpr RGB565(uint16_t c) : val(c) {}
322
323
327 constexpr inline RGB565(uint32_t val);
328
329
333 constexpr inline RGB565(uint64_t val);
334
335
339 constexpr RGB565(const RGB565&) = default;
340
341
345 constexpr inline RGB565(const RGB24& c);
346
347
351 constexpr inline RGB565(const RGB32& c);
352
353
357 constexpr inline RGB565(const RGB64& c);
358
359
363 constexpr inline RGB565(const RGBf & c);
364
365
369 RGB565(const HSV& c);
370
371
375 explicit operator uint16_t&() { return val; }
376
377
381 explicit operator const uint16_t& () const { return val; }
382
383
387 explicit operator iVec3() const { return iVec3(R, G, B); }
388
389
393 explicit operator fVec3() const { return fVec3(R / 31.0f, G / 63.0f , B / 31.0f); }
394
395
399 RGB565& operator=(const RGB565&) = default;
400
401
405 inline RGB565& operator=(const RGB24& c);
406
407
411 inline RGB565& operator=(const RGB32& c);
412
413
417 inline RGB565& operator=(const RGB64& c);
418
419
423 inline RGB565& operator=(const RGBf& c);
424
425
429 RGB565& operator=(const HSV& c);
430
431
436
437
442
443
448
449
454
455
459 void operator+=(const RGB565& c)
460 {
461 R += c.R;
462 G += c.G;
463 B += c.B;
464 }
465
466
470 void operator-=(const RGB565& c)
471 {
472 R -= c.R;
473 G -= c.G;
474 B -= c.B;
475 }
476
477
481 constexpr bool operator==(const RGB565& c) const
482 {
483 return(val == c.val);
484 }
485
486
490 constexpr bool operator!=(const RGB565& c) const
491 {
492 return !(*this == c);
493 }
494
495
503 inline void blend(RGB565 fg_col, float alpha)
504 {
505 blend256(fg_col, (uint32_t)(alpha * 256));
506 }
507
508
516 inline void blend256(const RGB565 & fg_col, uint32_t alpha)
517 {
518 const uint32_t a = (alpha >> 3); // map to 0 - 32.
519 const uint32_t bg = (val | (val << 16)) & 0b00000111111000001111100000011111;
520 const uint32_t fg = (fg_col.val | (fg_col.val << 16)) & 0b00000111111000001111100000011111;
521 const uint32_t result = ((((fg - bg) * a) >> 5) + bg) & 0b00000111111000001111100000011111;
522 val = (uint16_t)((result >> 16) | result); // contract result
523 }
524
525
529 inline void mult256(int mr, int mg, int mb)
530 {
531 R = (R * mr) >> 8;
532 G = (G * mg) >> 8;
533 B = (B * mb) >> 8;
534 }
535
536
542 inline void mult256(int mr, int mg, int mb, int ma)
543 {
544 mult256(mr, mg, mb);
545 }
546
547
553 inline void premultiply()
554 {
555 // nothing here.
556 return;
557 }
558
559
565 float opacity() const
566 {
567 return 1.0f;
568 }
569
570
576 void setOpacity(float op)
577 {
578 // nothing here.
579 return;
580 }
581
582
583 };
584
585
591 TGX_INLINE inline RGB565 interpolateColorsTriangle(const RGB565 & col1, int32_t C1, const RGB565 & col2, int32_t C2, const RGB565 & col3, const int32_t totC)
592 {
593 C1 <<= 5;
594 C1 /= totC;
595 C2 <<= 5;
596 C2 /= totC;
597 const uint32_t bg1 = (col1.val | (col1.val << 16)) & 0b00000111111000001111100000011111;
598 const uint32_t bg2 = (col2.val | (col2.val << 16)) & 0b00000111111000001111100000011111;
599 const uint32_t bg3 = (col3.val | (col3.val << 16)) & 0b00000111111000001111100000011111;
600 const uint32_t result = ((bg1*C1 + bg2 * C2 + bg3*(32 - C1 - C2)) >> 5) & 0b00000111111000001111100000011111;
601 return RGB565((uint16_t)((result >> 16) | result)); // contract result
602 }
603
604
621 TGX_INLINE inline RGB565 interpolateColorsBilinear(const RGB565 & C00, const RGB565 & C10, const RGB565 & C01, const RGB565 & C11, const float ax, const float ay)
622 {
623 /* floating point version, slower...
624 const float rax = 1.0f - ax;
625 const float ray = 1.0f - ay;
626 const int R = (int)(rax*(ray*C00.R + ay*C01.R) + ax*(ray*C10.R + ay*C11.R));
627 const int G = (int)(rax*(ray*C00.G + ay*C01.G) + ax*(ray*C10.G + ay*C11.G));
628 const int B = (int)(rax*(ray*C00.B + ay*C01.B) + ax*(ray*C10.B + ay*C11.B));
629 return RGB565(R,G,B);
630 */
631 const int iax = (int)(ax * 256);
632 const int iay = (int)(ay * 256);
633 const int rax = 256 - iax;
634 const int ray = 256 - iay;
635 const int R = rax*(ray*C00.R + iay*C01.R) + iax*(ray*C10.R + iay*C11.R);
636 const int G = rax*(ray*C00.G + iay*C01.G) + iax*(ray*C10.G + iay*C11.G);
637 const int B = rax*(ray*C00.B + iay*C01.B) + iax*(ray*C10.B + iay*C11.B);
638 return RGB565(R >> 16,G >> 16,B >> 16);
639 }
640
641
647 inline RGB565 meanColor(RGB565 colA, RGB565 colB)
648 {
649 return RGB565((uint16_t)((colA.val & colB.val) + (((colA.val ^ colB.val) & 0xF7DEu) >> 1)));
650 }
651
652
658 inline RGB565 meanColor(RGB565 colA, RGB565 colB, RGB565 colC, RGB565 colD)
659 {
660 return RGB565(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
661 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
662 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2);
663 }
664
665
666
667
668
678struct RGB24
679 {
680
681
682 // mtools extension (if available).
683 #if (MTOOLS_TGX_EXTENSIONS)
684 #include <mtools/extensions/tgx/tgx_ext_Color_RGB24.inl>
685 #endif
686
687
688 // no dual memory represention
689 // no alignment, just 3 consecutive uint8_t
690
691#if TGX_RGB24_ORDER_BGR
692 uint8_t B;
693 uint8_t G;
694 uint8_t R;
695#else
696 uint8_t R;
697 uint8_t G;
698 uint8_t B;
699#endif
700
701
702
706 RGB24() = default;
707
708
712 constexpr RGB24(int r, int g, int b) :
714 B((uint8_t)b), G((uint8_t)g), R((uint8_t)r)
715 #else
716 R((uint8_t)r), G((uint8_t)g), B((uint8_t)b)
717 #endif
718
719 {}
720
721
725 constexpr RGB24(iVec3 v) : RGB24(v.x, v.y, v.z)
726 {}
727
728
732 constexpr RGB24(iVec4 v) : RGB24(v.x, v.y, v.z)
733 {}
734
735
739 RGB24(float r, float g, float b) :
741 B((uint8_t)(b * 255)),
742 G((uint8_t)(g * 255)),
743 R((uint8_t)(r * 255))
744 #else
745 R((uint8_t)(r * 255)),
746 G((uint8_t)(g * 255)),
747 B((uint8_t)(b * 255))
748 #endif
749 {}
750
751
755 RGB24(fVec3 v) : RGB24(v.x, v.y, v.z)
756 {}
757
758
762 RGB24(fVec4 v) : RGB24(v.x, v.y, v.z)
763 {}
764
765
769 constexpr RGB24(uint8_t * p) : R(p[0]), G(p[1]), B(p[2]) {}
770
771
775 RGB24(const RGB24&) = default;
776
777
781 constexpr inline RGB24(uint16_t c);
782
783
787 constexpr inline RGB24(uint32_t c);
788
789
793 constexpr inline RGB24(uint64_t c);
794
795
799 constexpr inline RGB24(const RGB565& c);
800
801
805 constexpr inline RGB24(const RGB32& c);
806
807
811 constexpr inline RGB24(const RGB64& c);
812
813
817 constexpr inline RGB24(const RGBf& c);
818
819
823 RGB24(const HSV& c);
824
825
829 explicit operator iVec3() const { return iVec3(R, G, B); }
830
831
835 explicit operator fVec3() const { return fVec3(R / 255.0f, G / 255.0f, B / 255.0f); }
836
837
841 RGB24& operator=(const RGB24&) = default;
842
843
847 inline RGB24& operator=(const RGB565& c);
848
849
853 inline RGB24& operator=(const RGB32& c);
854
855
859 inline RGB24& operator=(const RGB64& c);
860
861
865 inline RGB24& operator=(const RGBf& c);
866
867
871 RGB24& operator=(const HSV& c);
872
873
878
879
884
885
890
891
896
897
901 void operator+=(const RGB24 & c)
902 {
903 R += c.R;
904 G += c.G;
905 B += c.B;
906 }
907
908
912 void operator-=(const RGB24& c)
913 {
914 R -= c.R;
915 G -= c.G;
916 B -= c.B;
917 }
918
919
923 void operator+=(uint8_t v)
924 {
925 R += v;
926 G += v;
927 B += v;
928 }
929
930
934 void operator-=(uint8_t v)
935 {
936 R -= v;
937 G -= v;
938 B -= v;
939 }
940
941
945 void operator*=(uint8_t v)
946 {
947 R *= v;
948 G *= v;
949 B *= v;
950 }
951
952
956 void operator*=(float v)
957 {
958 R = (uint8_t)(R * v);
959 G = (uint8_t)(G * v);
960 B = (uint8_t)(B * v);
961 }
962
966 void operator/=(uint8_t v)
967 {
968 R /= v;
969 G /= v;
970 B /= v;
971 }
972
976 void operator/=(float v)
977 {
978 R = (uint8_t)(R / v);
979 G = (uint8_t)(G / v);
980 B = (uint8_t)(B / v);
981 }
982
983
987 constexpr bool operator==(const RGB24& c) const
988 {
989 return((R == c.R) && (G == c.G) && (B == c.B));
990 }
991
992
996 constexpr bool operator!=(const RGB24& c) const
997 {
998 return !(*this == c);
999 }
1000
1001
1009 inline void blend(const RGB24 & fg_col, float alpha)
1010 {
1011 blend256(fg_col, (uint32_t)(alpha * 256));
1012 }
1013
1014
1022 inline void blend256(const RGB24& fg_col, uint32_t alpha)
1023 {
1024 const uint16_t a = (uint16_t)alpha;
1025 const uint16_t ia = (uint16_t)(256 - alpha);
1026 R = ((fg_col.R * a) + (R * ia)) >> 8;
1027 G = ((fg_col.G * a) + (G * ia)) >> 8;
1028 B = ((fg_col.B * a) + (B * ia)) >> 8;
1029 }
1030
1031
1035 inline void mult256(int mr, int mg, int mb)
1036 {
1037 R = (R * mr) >> 8;
1038 G = (G * mg) >> 8;
1039 B = (B * mb) >> 8;
1040 }
1041
1042
1048 inline void mult256(int mr, int mg, int mb, int ma)
1049 {
1050 mult256(mr, mg, mb);
1051 }
1052
1053
1059 inline void premultiply()
1060 {
1061 // nothing here.
1062 return;
1063 }
1064
1065
1071 float opacity() const
1072 {
1073 return 1.0f;
1074 }
1075
1076
1082 void setOpacity(float op)
1083 {
1084 // nothing here.
1085 return;
1086 }
1087
1088
1089 };
1090
1091
1092
1098 TGX_INLINE inline RGB24 interpolateColorsTriangle(const RGB24& col1, int32_t C1, const RGB24& col2, int32_t C2, const RGB24& col3, const int32_t totC)
1099 {
1100 return RGB24((int)(col3.R + (C1 * (col1.R - col3.R) + C2 * (col2.R - col3.R)) / totC),
1101 (int)(col3.G + (C1 * (col1.G - col3.G) + C2 * (col2.G - col3.G)) / totC),
1102 (int)(col3.B + (C1 * (col1.B - col3.B) + C2 * (col2.B - col3.B)) / totC));
1103 }
1104
1105
1122 TGX_INLINE inline RGB24 interpolateColorsBilinear(const RGB24 & C00, const RGB24 & C10, const RGB24 & C01, const RGB24 & C11, const float ax, const float ay)
1123 {
1124 const int iax = (int)(ax * 256);
1125 const int iay = (int)(ay * 256);
1126 const int rax = 256 - iax;
1127 const int ray = 256 - iay;
1128 const int R = rax*(ray*C00.R + iay*C01.R) + iax*(ray*C10.R + iay*C11.R);
1129 const int G = rax*(ray*C00.G + iay*C01.G) + iax*(ray*C10.G + iay*C11.G);
1130 const int B = rax*(ray*C00.B + iay*C01.B) + iax*(ray*C10.B + iay*C11.B);
1131 return RGB24(R >> 16,G >> 16,B >> 16);
1132 }
1133
1134
1138 inline RGB24 meanColor(const RGB24 & colA, const RGB24 & colB)
1139 {
1140 return RGB24(((int)colA.R + (int)colB.R) >> 1,
1141 ((int)colA.G + (int)colB.G) >> 1,
1142 ((int)colA.B + (int)colB.B) >> 1);
1143 }
1144
1145
1149 inline RGB24 meanColor(const RGB24& colA, const RGB24& colB, const RGB24& colC, const RGB24& colD)
1150 {
1151 return RGB24(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
1152 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
1153 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2);
1154 }
1155
1156
1157
1158
1159
1173struct RGB32
1174 {
1175
1176
1177 // mtools extension (if available).
1178 #if (MTOOLS_TGX_EXTENSIONS)
1179 #include <mtools/extensions/tgx/tgx_ext_Color_RGB32.inl>
1180 #endif
1181
1182
1183 static const uint8_t DEFAULT_A = 255;
1184
1185 // dual memory representation
1186 union
1187 {
1188 uint32_t val;
1189 struct
1190 {
1191#if TGX_RGB32_ORDER_BGR
1192 uint8_t B;
1193 uint8_t G;
1194 uint8_t R;
1195 uint8_t A;
1196
1197#else
1198 uint8_t R;
1199 uint8_t G;
1200 uint8_t B;
1201 uint8_t A;
1202#endif
1203 };
1204 };
1205
1206
1210 RGB32() = default;
1211
1212
1216 constexpr RGB32(int r, int g, int b, int a = DEFAULT_A) :
1218 B((uint8_t)b), G((uint8_t)g), R((uint8_t)r), A((uint8_t)a)
1219 #else
1220 R((uint8_t)r), G((uint8_t)g), B((uint8_t)b), A((uint8_t)a)
1221 #endif
1222 {}
1223
1224
1229 constexpr RGB32(iVec3 v) : RGB32(v.x, v.y, v.z)
1230 {}
1231
1232
1236 constexpr RGB32(iVec4 v) : RGB32(v.x, v.y, v.z, v.w)
1237 {}
1238
1239
1243 RGB32(float r, float g, float b, float a = -1.0f) :
1245 B((uint8_t)(b * 255)),
1246 G((uint8_t)(g * 255)),
1247 R((uint8_t)(r * 255)),
1248 A((uint8_t)((a < 0.0f) ? DEFAULT_A : ((uint8_t)roundf(a * 255.0f))))
1249 #else
1250 R((uint8_t)(r * 255.0f)),
1251 G((uint8_t)(g * 255.0f)),
1252 B((uint8_t)(b * 255.0f)),
1253 A((uint8_t)((a < 0.0f) ? DEFAULT_A : ((uint8_t)roundf(a * 255.0f))))
1254 #endif
1255 {}
1256
1257
1262 RGB32(fVec3 v) : RGB32(v.x, v.y, v.z)
1263 {}
1264
1265
1269 RGB32(fVec4 v) : RGB32(v.x, v.y, v.z, v.w)
1270 {}
1271
1272
1276 constexpr RGB32(uint32_t c) : val(c) {}
1277
1278
1282 constexpr inline RGB32(uint16_t val);
1283
1284
1288 constexpr inline RGB32(uint64_t val);
1289
1290
1294 RGB32(const RGB32&) = default;
1295
1296
1300 constexpr inline RGB32(const RGB565& c);
1301
1302
1306 constexpr inline RGB32(const RGB24& c);
1307
1308
1312 constexpr inline RGB32(const RGB64& c);
1313
1314
1318 constexpr inline RGB32(const RGBf& c);
1319
1320
1324 RGB32(const HSV& c);
1325
1326
1330 explicit operator uint32_t&() { return val; }
1331
1332
1336 explicit operator const uint32_t& () const { return val; }
1337
1338
1342 explicit operator iVec3() const { return iVec3(R, G, B); }
1343
1344
1348 explicit operator fVec3() const { return fVec3(R / 255.0f, G / 255.0f, B / 255.0f); }
1349
1350
1354 explicit operator iVec4() const { return iVec4(R, G, B, A); }
1355
1356
1360 explicit operator fVec4() const { return fVec4(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f); }
1361
1362
1366 RGB32& operator=(const RGB32&) = default;
1367
1368
1372 inline RGB32& operator=(const RGB565& c);
1373
1374
1378 inline RGB32& operator=(const RGB24& c);
1379
1380
1384 inline RGB32& operator=(const RGB64& c);
1385
1386
1390 inline RGB32& operator=(const RGBf& c);
1391
1392
1396 RGB32& operator=(const HSV& c);
1397
1398
1403
1404
1409
1410
1415
1416
1421
1422
1426 void operator+=(const RGB32& c)
1427 {
1428 R += c.R;
1429 G += c.G;
1430 B += c.B;
1431 A += c.A;
1432 }
1433
1434
1438 void operator-=(const RGB32& c)
1439 {
1440 R -= c.R;
1441 G -= c.G;
1442 B -= c.B;
1443 A -= c.A;
1444 }
1445
1446
1450 void operator+=(uint8_t v)
1451 {
1452 R += v;
1453 G += v;
1454 B += v;
1455 A += v;
1456 }
1457
1458
1462 void operator-=(uint8_t v)
1463 {
1464 R -= v;
1465 G -= v;
1466 B -= v;
1467 A -= v;
1468 }
1469
1470
1474 void operator*=(uint8_t v)
1475 {
1476 R *= v;
1477 G *= v;
1478 B *= v;
1479 A *= v;
1480 }
1481
1482
1486 void operator*=(float v)
1487 {
1488 R = (uint8_t)(R * v);
1489 G = (uint8_t)(G * v);
1490 B = (uint8_t)(B * v);
1491 A = (uint8_t)(A * v);
1492 }
1493
1497 void operator/=(uint8_t v)
1498 {
1499 R /= v;
1500 G /= v;
1501 B /= v;
1502 A /= v;
1503 }
1504
1508 void operator/=(float v)
1509 {
1510 R = (uint8_t)(R / v);
1511 G = (uint8_t)(G / v);
1512 B = (uint8_t)(B / v);
1513 A = (uint8_t)(A / v);
1514 }
1515
1516
1520 constexpr bool operator==(const RGB32& c) const
1521 {
1522 return(val == c.val);
1523 }
1524
1525
1529 constexpr bool operator!=(const RGB32& c) const
1530 {
1531 return !(*this == c);
1532 }
1533
1534
1544 inline void blend(const RGB32 & fg_col, float alpha)
1545 {
1546 blend256(fg_col, (uint32_t)(alpha * 256));
1547 }
1548
1549
1559 inline void blend256(const RGB32 & fg_col, uint32_t alpha)
1560 {
1561 // below is the correct alpha blending with pre-multiplied alpha
1562 // we do 'real' interpolate with eternal 'alpha' but not with the 'A' component of fg_col
1563 // since it is already pre-multiplied
1564 const uint32_t inv_alpha = (65536 - (alpha * (fg_col.A + (fg_col.A > 127)))) >> 8;
1565 const uint32_t ag = ((fg_col.val & 0xFF00FF00) >> 8)*alpha + ((val & 0xFF00FF00) >> 8)*inv_alpha;
1566 const uint32_t rb = (fg_col.val & 0x00FF00FF) * alpha + (val & 0x00FF00FF) * inv_alpha;
1567 val = (ag & 0xFF00FF00) | ((rb >> 8) & 0x00FF00FF);
1568 }
1569
1570
1579 inline void blend(RGB32 fg_col)
1580 {
1581 blend256(fg_col, 256);
1582 }
1583
1584
1588 inline void mult256(int mr, int mg, int mb)
1589 {
1590 R = (R * mr) >> 8;
1591 G = (G * mg) >> 8;
1592 B = (B * mb) >> 8;
1593 }
1594
1595
1599 inline void mult256(int mr, int mg, int mb, int ma)
1600 {
1601 R = (R * mr) >> 8;
1602 G = (G * mg) >> 8;
1603 B = (B * mb) >> 8;
1604 A = (A * ma) >> 8;
1605 }
1606
1607
1615 inline void premultiply()
1616 {
1617 R = (uint8_t)((((uint16_t)R) * A) / 255);
1618 G = (uint8_t)((((uint16_t)G) * A) / 255);
1619 B = (uint8_t)((((uint16_t)B) * A) / 255);
1620 }
1621
1622
1627 float opacity() const
1628 {
1629 return (((float)A)/ 255.0f);
1630 }
1631
1632
1640 void setOpacity(float op)
1641 {
1642 // slow version
1643 float mo = op * 255.0f;
1644 float mult = (A == 0) ? 0.0f : (mo / ((float)A));
1645 (*this) = RGB32((int)(R * mult), (int)(G * mult), (int)(B * mult), (int)mo);
1646 }
1647
1648
1654 void multOpacity(float op)
1655 {
1656 *this = getMultOpacity(op);
1657 }
1658
1659
1665 RGB32 getMultOpacity(float op) const
1666 {
1667 // faster
1668 uint32_t o = (uint32_t)(256 * op);
1669 uint32_t ag = (val & 0xFF00FF00) >> 8;
1670 uint32_t rb = val & 0x00FF00FF;
1671 uint32_t sag = o * ag;
1672 uint32_t srb = o * rb;
1673 sag = sag & 0xFF00FF00;
1674 srb = (srb >> 8) & 0x00FF00FF;
1675 return RGB32(sag | srb);
1676 }
1677
1678
1683 {
1684 setOpacity(1.0f);
1685 }
1686
1687
1692 {
1693 R = 0;
1694 G = 0;
1695 B = 0;
1696 A = 0;
1697 }
1698
1699
1700 };
1701
1702
1703
1709 TGX_INLINE inline RGB32 interpolateColorsTriangle(const RGB32& col1, int32_t C1, const RGB32& col2, int32_t C2, const RGB32& col3, const int32_t totC)
1710 {
1711 return RGB32((int)(col3.R + (C1 * (col1.R - col3.R) + C2 * (col2.R - col3.R)) / totC),
1712 (int)(col3.G + (C1 * (col1.G - col3.G) + C2 * (col2.G - col3.G)) / totC),
1713 (int)(col3.B + (C1 * (col1.B - col3.B) + C2 * (col2.B - col3.B)) / totC),
1714 (int)(col3.A + (C1 * (col1.A - col3.A) + C2 * (col2.A - col3.A)) / totC));
1715 }
1716
1717
1734 TGX_INLINE inline RGB32 interpolateColorsBilinear(const RGB32 & C00, const RGB32 & C10, const RGB32 & C01, const RGB32 & C11, const float ax, const float ay)
1735 {
1736 const int iax = (int)(ax * 256);
1737 const int iay = (int)(ay * 256);
1738 const int rax = 256 - iax;
1739 const int ray = 256 - iay;
1740 const int R = rax*(ray*C00.R + iay*C01.R) + iax*(ray*C10.R + iay*C11.R);
1741 const int G = rax*(ray*C00.G + iay*C01.G) + iax*(ray*C10.G + iay*C11.G);
1742 const int B = rax*(ray*C00.B + iay*C01.B) + iax*(ray*C10.B + iay*C11.B);
1743 const int A = rax*(ray*C00.A + iay*C01.A) + iax*(ray*C10.A + iay*C11.A);
1744 return RGB32(R >> 16,G >> 16,B >> 16,A >> 16);
1745 }
1746
1747
1751 inline RGB32 meanColor(RGB32 colA, RGB32 colB)
1752 {
1753 return RGB32(((int)colA.R + (int)colB.R) >> 1,
1754 ((int)colA.G + (int)colB.G) >> 1,
1755 ((int)colA.B + (int)colB.B) >> 1,
1756 ((int)colA.A + (int)colB.A) >> 1);
1757 }
1758
1759
1763 inline RGB32 meanColor(RGB32 colA, RGB32 colB, RGB32 colC, RGB32 colD)
1764 {
1765 return RGB32(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
1766 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
1767 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2,
1768 ((int)colA.A + (int)colB.A + (int)colC.A + (int)colD.A) >> 2);
1769 }
1770
1771
1772
1773
1774
1788struct RGB64
1789 {
1790
1791
1792 // mtools extension (if available).
1793 #if (MTOOLS_TGX_EXTENSIONS)
1794 #include <mtools/extensions/tgx/tgx_ext_Color_RGB64.inl>
1795 #endif
1796
1797
1798 static const uint16_t DEFAULT_A = 65535;
1799
1800
1801 // dual memory representation
1802 union
1803 {
1804 uint64_t val;
1805 struct
1806 {
1807
1808#if TGX_RGB64_ORDER_BGR
1809 uint16_t B;
1810 uint16_t G;
1811 uint16_t R;
1812 uint16_t A;
1813
1814#else
1815 uint16_t R;
1816 uint16_t G;
1817 uint16_t B;
1818 uint16_t A;
1819#endif
1820 };
1821 };
1822
1823
1827 RGB64() = default;
1828
1829
1833 constexpr RGB64(int r, int g, int b, int a = DEFAULT_A) :
1835 B((uint16_t)b), G((uint16_t)g), R((uint16_t)r), A((uint16_t)a)
1836 #else
1837 R((uint16_t)r), G((uint16_t)g), B((uint16_t)b), A((uint16_t)a)
1838 #endif
1839 {}
1840
1841
1845 constexpr RGB64(iVec3 v) : RGB64(v.x, v.y, v.z)
1846 {}
1847
1848
1852 constexpr RGB64(iVec4 v) : RGB64(v.x, v.y, v.z, v.w)
1853 {}
1854
1855
1859 RGB64(float r, float g, float b, float a = -1.0f) :
1861 B((uint16_t)(b * 65535)),
1862 G((uint16_t)(g * 65535)),
1863 R((uint16_t)(r * 65535)),
1864 A((uint16_t)((a < 0.0f) ? DEFAULT_A : a * 65535))
1865 #else
1866 R((uint16_t)(r * 65535)),
1867 G((uint16_t)(g * 65535)),
1868 B((uint16_t)(b * 65535)),
1869 A((uint16_t)((a < 0.0f) ? DEFAULT_A : a * 65535))
1870 #endif
1871 {}
1872
1873
1877 RGB64(fVec3 v) : RGB64(v.x, v.y, v.z)
1878 {}
1879
1880
1884 RGB64(fVec4 v) : RGB64(v.x, v.y, v.z, v.w)
1885 {}
1886
1887
1891 constexpr inline RGB64(uint64_t c) : val(c) {}
1892
1893
1897 inline RGB64(uint16_t val);
1898
1899
1903 inline RGB64(uint32_t val);
1904
1905
1909 RGB64(const RGB64&) = default;
1910
1911
1915 inline RGB64(const RGB565& c);
1916
1917
1921 inline RGB64(const RGB24& c);
1922
1923
1927 inline RGB64(const RGB32& c);
1928
1929
1933 inline RGB64(const RGBf& c);
1934
1935
1939 RGB64(const HSV& c);
1940
1941
1945 explicit operator uint64_t&() { return val; }
1946
1947
1951 explicit operator const uint64_t&() const { return val; }
1952
1953
1957 explicit operator iVec3() const { return iVec3(R, G, B); }
1958
1959
1963 explicit operator fVec3() const { return fVec3(R / 65535.0f, G / 65535.0f, B / 65535.0f); }
1964
1965
1969 explicit operator iVec4() const { return iVec4(R, G, B, A); }
1970
1971
1975 explicit operator fVec4() const { return fVec4(R / 65535.0f, G / 65535.0f, B / 65535.0f, A / 65535.0f); }
1976
1977
1981 RGB64& operator=(const RGB64&) = default;
1982
1983
1987 inline RGB64& operator=(const RGB565& c);
1988
1989
1993 inline RGB64& operator=(const RGB24& c);
1994
1995
1999 inline RGB64& operator=(const RGB32& c);
2000
2001
2005 inline RGB64& operator=(const RGBf& c);
2006
2007
2011 RGB64& operator=(const HSV& c);
2012
2013
2018
2019
2024
2025
2030
2031
2036
2037
2041 void operator+=(const RGB64& c)
2042 {
2043 R += c.R;
2044 G += c.G;
2045 B += c.B;
2046 A += c.A;
2047 }
2048
2049
2053 void operator-=(const RGB64& c)
2054 {
2055 R -= c.R;
2056 G -= c.G;
2057 B -= c.B;
2058 A -= c.A;
2059 }
2060
2061
2065 void operator+=(uint16_t v)
2066 {
2067 R += v;
2068 G += v;
2069 B += v;
2070 A += v;
2071 }
2072
2073
2077 void operator-=(uint16_t v)
2078 {
2079 R -= v;
2080 G -= v;
2081 B -= v;
2082 A -= v;
2083 }
2084
2085
2089 void operator*=(uint16_t v)
2090 {
2091 R *= v;
2092 G *= v;
2093 B *= v;
2094 A *= v;
2095 }
2096
2097
2101 void operator*=(float v)
2102 {
2103 R = (uint16_t)(R * v);
2104 G = (uint16_t)(G * v);
2105 B = (uint16_t)(B * v);
2106 A = (uint16_t)(A * v);
2107 }
2108
2112 void operator/=(uint16_t v)
2113 {
2114 R /= v;
2115 G /= v;
2116 B /= v;
2117 A /= v;
2118 }
2119
2123 void operator/=(float v)
2124 {
2125 R = (uint16_t)(R / v);
2126 G = (uint16_t)(G / v);
2127 B = (uint16_t)(B / v);
2128 A = (uint16_t)(A / v);
2129 }
2130
2131
2135 constexpr bool operator==(const RGB64& c) const
2136 {
2137 return(val == c.val);
2138 }
2139
2140
2144 constexpr bool operator!=(const RGB64& c) const
2145 {
2146 return !(*this == c);
2147 }
2148
2149
2159 inline void blend(const RGB64& fg_col, float alpha)
2160 {
2161 blend65536(fg_col, (uint32_t)(alpha * 65536));
2162 }
2163
2164
2174 inline void blend256(const RGB64 & fg_col, uint32_t alpha)
2175 {
2176 blend65536(fg_col, (alpha << 8));
2177 }
2178
2179
2189 inline void blend65536(RGB64 fg_col, uint32_t alpha)
2190 {
2191 // correct version where 'alpha' is interpolated normally but 'A' is only multiplied
2192 // with background because the color is assumed pre-multiplied
2193 alpha >>= 1; // alpha in [0,32768]
2194 const uint32_t inv_alpha = (2147483648 - (alpha * (((uint32_t)fg_col.A) + (fg_col.A > 32767)))) >> 16;
2195 R = (uint16_t)((((uint32_t)fg_col.R) * alpha + ((uint32_t)R) * inv_alpha) >> 15);
2196 G = (uint16_t)((((uint32_t)fg_col.G) * alpha + ((uint32_t)G) * inv_alpha) >> 15);
2197 B = (uint16_t)((((uint32_t)fg_col.B) * alpha + ((uint32_t)B) * inv_alpha) >> 15);
2198 A = (uint16_t)((((uint32_t)fg_col.A) * alpha + ((uint32_t)A) * inv_alpha) >> 15);
2199 }
2200
2201
2209 inline void blend(const RGB64 & fg_col)
2210 {
2211 blend65536(fg_col, 65536);
2212 }
2213
2214
2218 inline void mult256(int mr, int mg, int mb)
2219 {
2220 R = (R * mr) >> 8;
2221 G = (G * mg) >> 8;
2222 B = (B * mb) >> 8;
2223 }
2224
2225
2229 inline void mult256(int mr, int mg, int mb, int ma)
2230 {
2231 R = (R * mr) >> 8;
2232 G = (G * mg) >> 8;
2233 B = (B * mb) >> 8;
2234 A = (A * ma) >> 8;
2235 }
2236
2237
2245 inline void premultiply()
2246 {
2247 R = (uint16_t)((((uint32_t)R) * A) / 65535);
2248 G = (uint16_t)((((uint32_t)G) * A) / 65535);
2249 B = (uint16_t)((((uint32_t)B) * A) / 65535);
2250 }
2251
2252
2257 float opacity() const
2258 {
2259 return (((float)A) / 65535.0f);
2260 }
2261
2262
2270 void setOpacity(float op)
2271 {
2272 // slow version
2273 float mo = op * 65535.0f;
2274 float mult = (A == 0) ? 0.0f : (mo / ((float)A));
2275 (*this) = RGB64((int)(R * mult), (int)(G * mult), (int)(B * mult), (int)mo);
2276 }
2277
2278
2284 void multOpacity(float op)
2285 {
2286 *this = getMultOpacity(op);
2287 }
2288
2289
2295 RGB64 getMultOpacity(float op) const
2296 {
2297 return RGB64((int)(R * op), (int)(G * op), (int)(B * op), (int)(A * op));
2298 }
2299
2300
2305 {
2306 setOpacity(1.0f);
2307 }
2308
2309
2314 {
2315 R = 0;
2316 G = 0;
2317 B = 0;
2318 A = 0;
2319 }
2320
2321
2322 };
2323
2324
2325
2326
2332 TGX_INLINE inline RGB64 interpolateColorsTriangle(const RGB64& col1, int32_t C1, const RGB64& col2, int32_t C2, const RGB64& col3, const int32_t totC)
2333 {
2334 // forward to RGB32 (, maybe improve this but is it worth it, the method is never used ?)
2335 return RGB64(interpolateColorsTriangle(RGB32(col1), C1, RGB32(col2), C2, RGB32(col3), totC));
2336 }
2337
2338
2355 TGX_INLINE inline RGB64 interpolateColorsBilinear(const RGB64 & C00, const RGB64 & C10, const RGB64 & C01, const RGB64 & C11, const float ax, const float ay)
2356 {
2357 // let's use floating point version for max accuraccy, RGB64 is slow anyway...
2358 const float rax = 1.0f - ax;
2359 const float ray = 1.0f - ay;
2360 const int R = (int)roundf(rax*(ray*C00.R + ay*C01.R) + ax*(ray*C10.R + ay*C11.R));
2361 const int G = (int)roundf(rax*(ray*C00.G + ay*C01.G) + ax*(ray*C10.G + ay*C11.G));
2362 const int B = (int)roundf(rax*(ray*C00.B + ay*C01.B) + ax*(ray*C10.B + ay*C11.B));
2363 const int A = (int)roundf(rax*(ray*C00.A + ay*C01.A) + ax*(ray*C10.A + ay*C11.A));
2364 return RGB64(R,G,B,A);
2365 }
2366
2367
2371 inline RGB64 meanColor(const RGB64 & colA, const RGB64 & colB)
2372 {
2373 return RGB64(((int)colA.R + (int)colB.R) >> 1,
2374 ((int)colA.G + (int)colB.G) >> 1,
2375 ((int)colA.B + (int)colB.B) >> 1,
2376 ((int)colA.A + (int)colB.A) >> 1);
2377 }
2378
2379
2383 inline RGB64 meanColor(const RGB64& colA, const RGB64& colB, const RGB64& colC, const RGB64& colD)
2384 {
2385 return RGB64(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
2386 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
2387 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2,
2388 ((int)colA.A + (int)colB.A + (int)colC.A + (int)colD.A) >> 2);
2389 }
2390
2391
2392
2393
2394
2404 struct RGBf
2405 {
2406
2407 // mtools extension (if available).
2408 #if (MTOOLS_TGX_EXTENSIONS)
2409 #include <mtools/extensions/tgx/tgx_ext_Color_RGBf.inl>
2410 #endif
2411
2412
2413#if TGX_RGBf_ORDER_BGR
2414 float B;
2415 float G;
2416 float R;
2417#else
2418 float R;
2419 float G;
2420 float B;
2421#endif
2422
2423
2427 RGBf() = default;
2428
2429
2433 constexpr RGBf(float r, float g, float b) :
2435 B(b), G(g), R(r)
2436 #else
2437 R(r), G(g), B(b)
2438 #endif
2439 {}
2440
2441
2445 constexpr RGBf(const fVec3 & v) : RGBf(v.x, v.y, v.z)
2446 {}
2447
2448
2452 constexpr RGBf(const fVec4 & v) : RGBf(v.x, v.y, v.z)
2453 {}
2454
2455
2459 constexpr inline RGBf(uint16_t val);
2460
2461
2465 constexpr inline RGBf(uint32_t val);
2466
2467
2471 constexpr inline RGBf(uint64_t val);
2472
2473
2477 constexpr RGBf(const RGBf&) = default;
2478
2479
2483 constexpr inline RGBf(const RGB565& c);
2484
2485
2489 constexpr inline RGBf(const RGB24& c);
2490
2491
2495 constexpr inline RGBf(const RGB32& c);
2496
2497
2501 constexpr inline RGBf(const RGB64& c);
2502
2503
2507 RGBf(const HSV& c);
2508
2509
2513 explicit operator fVec3() const { return fVec3(R, G, B); }
2514
2515
2519 RGBf& operator=(const RGBf&) = default;
2520
2521
2525 inline RGBf& operator=(const RGB565& c);
2526
2527
2531 inline RGBf& operator=(const RGB24& c);
2532
2533
2537 inline RGBf& operator=(const RGB32& c);
2538
2539
2543 inline RGBf& operator=(const RGB64& c);
2544
2545
2549 RGBf& operator=(const HSV& c);
2550
2551
2555 inline RGBf& operator=(const fVec3 & v);
2556
2557
2561 inline RGBf& operator=(const fVec4 & v);
2562
2563
2567 TGX_INLINE inline void operator+=(const RGBf& c)
2568 {
2569 R += c.R;
2570 G += c.G;
2571 B += c.B;
2572 }
2573
2574
2578 TGX_INLINE inline void operator-=(const RGBf& c)
2579 {
2580 R -= c.R;
2581 G -= c.G;
2582 B -= c.B;
2583 }
2584
2585
2589 TGX_INLINE inline void operator*=(const RGBf & c)
2590 {
2591 R *= c.R;
2592 G *= c.G;
2593 B *= c.B;
2594 }
2595
2599 TGX_INLINE inline RGBf operator*(const RGBf & c) const
2600 {
2601 return RGBf(R * c.R, G * c.G, B * c.B);
2602 }
2603
2607 TGX_INLINE inline void operator*=(float a)
2608 {
2609 R *= a;
2610 G *= a;
2611 B *= a;
2612 }
2613
2614
2618 TGX_INLINE inline RGBf operator*(float a) const
2619 {
2620 return RGBf(R * a, G * a, B * a);
2621 }
2622
2623
2627 constexpr bool operator==(const RGBf & c) const
2628 {
2629 return((R == c.R)&&(G == c.G)&&(B == c.B));
2630 }
2631
2632
2636 constexpr bool operator!=(const RGBf & c) const
2637 {
2638 return !(*this == c);
2639 }
2640
2641
2645 TGX_INLINE inline void clamp()
2646 {
2647 R = tgx::clamp(R, 0.0f, 1.0f);
2648 G = tgx::clamp(G, 0.0f, 1.0f);
2649 B = tgx::clamp(B, 0.0f, 1.0f);
2650 }
2651
2652
2660 TGX_INLINE inline void blend256(const RGBf& fg_col, uint32_t alpha)
2661 {
2662 blend(fg_col, alpha / 256.0f);
2663 }
2664
2665
2673 TGX_INLINE inline void blend(const RGBf & fg_col, float alpha)
2674 {
2675 R += (fg_col.R - R) * alpha;
2676 G += (fg_col.G - G) * alpha;
2677 B += (fg_col.B - B) * alpha;
2678 }
2679
2680
2684 TGX_INLINE inline void mult256(int mr, int mg, int mb)
2685 {
2686 R = (R * mr)/256;
2687 G = (G * mg)/256;
2688 B = (B * mb)/256;
2689 }
2690
2691
2696 TGX_INLINE inline void mult256(int mr, int mg, int mb, int ma)
2697 {
2698 mult256(mr, mg, mb);
2699 }
2700
2701
2707 TGX_INLINE inline void premultiply()
2708 {
2709 // nothing here.
2710 return;
2711 }
2712
2713
2719 TGX_INLINE inline float opacity() const
2720 {
2721 return 1.0f;
2722 }
2723
2724
2730 TGX_INLINE inline void setOpacity(float op)
2731 {
2732 // nothing here.
2733 return;
2734 }
2735
2736
2737 };
2738
2739
2740
2746 TGX_INLINE inline RGBf interpolate(const RGBf& col1, const RGBf& col2, float alpha)
2747 {
2748 return RGBf(col1.R + alpha * (col2.R - col1.R),
2749 col1.G + alpha * (col2.G - col1.G),
2750 col1.B + alpha * (col2.B - col1.B));
2751 }
2752
2753
2759 TGX_INLINE inline RGBf interpolateColorsTriangle(const RGBf & col1, int32_t C1, const RGBf & col2, int32_t C2, const RGBf & col3, int32_t totC)
2760 {
2761 return RGBf(col3.R + (C1 * (col1.R - col3.R) + C2 * (col2.R - col3.R)) / totC,
2762 col3.G + (C1 * (col1.G - col3.G) + C2 * (col2.G - col3.G)) / totC,
2763 col3.B + (C1 * (col1.B - col3.B) + C2 * (col2.B - col3.B)) / totC);
2764 }
2765
2766
2783 TGX_INLINE inline RGBf interpolateColorsBilinear(const RGBf & C00, const RGBf & C10, const RGBf & C01, const RGBf & C11, const float ax, const float ay)
2784 {
2785 const float rax = 1.0f - ax;
2786 const float ray = 1.0f - ay;
2787 const float R = rax*(ray*C00.R + ay*C01.R) + ax*(ray*C10.R + ay*C11.R);
2788 const float G = rax*(ray*C00.G + ay*C01.G) + ax*(ray*C10.G + ay*C11.G);
2789 const float B = rax*(ray*C00.B + ay*C01.B) + ax*(ray*C10.B + ay*C11.B);
2790 return RGBf(R,G,B);
2791 }
2792
2793
2797 TGX_INLINE inline RGBf meanColor(const RGBf & colA, const RGBf & colB)
2798 {
2799 return RGBf((colA.R + colB.R)/2, (colA.G + colB.G)/2, (colA.B + colB.B)/2);
2800 }
2801
2802
2806 TGX_INLINE inline RGBf meanColor(const RGBf & colA, const RGBf & colB, const RGBf & colC, const RGBf & colD)
2807 {
2808 return RGBf((colA.R + colB.R + colC.R + colD.R) / 4, (colA.G + colB.G + colC.G + colD.G) / 4, (colA.B + colB.B + colC.B + colD.B) / 4);
2809 }
2810
2811
2812
2813
2814
2826struct HSV
2827 {
2828
2829 // mtools extension (if available).
2830 #if (MTOOLS_TGX_EXTENSIONS)
2831 #include <mtools/extensions/tgx/tgx_ext_Color_HSV.inl>
2832 #endif
2833
2834 // color components
2835 float H;
2836 float S;
2837 float V;
2838
2839
2843 HSV() = default;
2844
2845
2849 constexpr HSV(float h, float s, float v) : H(h), S(s), V(v) {}
2850
2851
2855 constexpr HSV(fVec3 v) : HSV(v.x, v.y, v.z)
2856 {}
2857
2858
2862 constexpr HSV(fVec4 v) : HSV(v.x, v.y, v.z)
2863 {}
2864
2865
2869 HSV(const HSV&) = default;
2870
2871
2875 HSV(uint16_t val);
2876
2877
2881 HSV(uint32_t val);
2882
2883
2887 HSV(uint64_t val);
2888
2889
2893 HSV(const RGB565& c);
2894
2895
2899 HSV(const RGB24 & c);
2900
2901
2905 HSV(const RGB32 & c);
2906
2907
2911 HSV(const RGB64 & c);
2912
2913
2917 HSV(const RGBf& c);
2918
2919
2923 explicit operator fVec3() const { return fVec3(H, S, V); }
2924
2925
2929 HSV& operator=(const HSV&) = default;
2930
2931
2936
2937
2941 HSV& operator=(const RGB24 & c);
2942
2943
2947 HSV& operator=(const RGB32 & c);
2948
2949
2953 HSV& operator=(const RGB64 & c);
2954
2955
2959 HSV& operator=(const RGBf& c);
2960
2961
2966 {
2967 H = v.x;
2968 S = v.y;
2969 V = v.z;
2970 return *this;
2971 }
2972
2973
2978 {
2979 H = v.x;
2980 S = v.y;
2981 V = v.z;
2982 return *this;
2983 }
2984
2985
2989 constexpr bool operator==(const HSV & c) const
2990 {
2991 return((H == c.H)&&(S == c.S)&&(V == c.V));
2992 }
2993
2994
2998 constexpr bool operator!=(const HSV & c) const
2999 {
3000 return !(*this == c);
3001 }
3002
3003
3014 inline void blend(const HSV & fg_col, float alpha)
3015 {
3016 // is there a natural blending formula in HSV space ?
3017 // let us do it in RGB space for the time being
3018 RGBf c(*this);
3019 c.blend(RGBf(fg_col), alpha);
3020 *this = HSV(c);
3021 }
3022
3023
3034 inline void blend256(const HSV & fg_col, uint32_t alpha)
3035 {
3036 blend(fg_col, (alpha / 256.0f));
3037 }
3038
3039
3046 inline void mult256(int mr, int mg, int mb)
3047 {
3048 RGBf c = RGBf(*this);
3049 c.mult256(mr, mg, mb);
3050 *this = HSV(c);
3051 }
3052
3053
3061 inline void mult256(int mr, int mg, int mb, int ma)
3062 {
3063 mult256(mr, mg, mb);
3064 }
3065
3066
3072 inline void premultiply()
3073 {
3074 // nothing here.
3075 return;
3076 }
3077
3078
3084 float opacity() const
3085 {
3086 return 1.0f;
3087 }
3088
3089
3095 void setOpacity(float op)
3096 {
3097 // nothing here.
3098 return;
3099 }
3100
3101 };
3102
3103
3104
3110 inline HSV interpolateColorsTriangle(HSV col1, int32_t C1, HSV col2, int32_t C2, HSV col3, int32_t totC)
3111 {
3112 return HSV(interpolateColorsTriangle(RGBf(col1), C1, RGBf(col2), C2, RGBf(col3), totC));
3113 }
3114
3115
3132 inline HSV interpolateColorsBilinear(const HSV & C00, const HSV & C10, const HSV & C01, const HSV & C11, const float ax, const float ay)
3133 {
3134 // just forward to RGBf ..
3135 return HSV(interpolateColorsBilinear(RGBf(C00), RGBf(C10), RGBf(C01), RGBf(C11), ax, ay));
3136 }
3137
3138
3142 inline HSV meanColor(const HSV & colA, const HSV & colB)
3143 {
3144 // hum.. what does that mean in HSV, let's do it in RGB color space instead
3145 return HSV(meanColor(RGBf(colA), RGBf(colB)));
3146 }
3147
3148
3152 inline HSV meanColor(const HSV& colA, const HSV& colB, const HSV& colC, const HSV& colD)
3153 {
3154 // hum.. what does that mean in HSV, let's do it in RGB color space instead
3155 return HSV(meanColor(RGBf(colA), RGBf(colB), RGBf(colC), RGBf(colD)));
3156 }
3157
3158
3159}
3160
3161
3162
3163#include "Color.inl"
3164
3165
3166#endif
3167
3168#endif
3169
3170
3171/* end of file */
3172
const RGB32 RGB32_Cyan
Color cyan in RGB32 format.
const RGB32 RGB32_Yellow
Color yellow in RGB32 format.
const RGB32 RGB32_Blue
Color blue in RGB32 format.
TGX_INLINE RGB565 interpolateColorsBilinear(const RGB565 &C00, const RGB565 &C10, const RGB565 &C01, const RGB565 &C11, const float ax, const float ay)
Bilinear interpolation between 4 colors.
Definition: Color.h:621
const RGB32 RGB32_Black
Color black in RGB32 format.
#define TGX_RGB32_ORDER_BGR
color ordering for RGB565 (default B,G,R for compatibility with the mtools library)
Definition: Color.h:100
const RGB565 RGB565_Salmon
Color salmon in RGB565 format.
const RGB565 RGB565_Navy
Color navy in RGB565 format.
const RGB32 RGB32_Gray
Color gray in RGB32 format.
const RGB32 RGB32_Red
Color red in RGB32 format.
const RGB32 RGB32_Magenta
Color majenta in RGB32 format.
#define TGX_RGB24_ORDER_BGR
color ordering for RGB24 (default R,G,B)
Definition: Color.h:96
const RGB32 RGB32_Orange
Color orange in RGB32 format.
const RGB565 RGB565_Yellow
Color yellow in RGB565 format.
const RGB565 RGB565_White
Color white in RGB565 format.
#define TGX_RGB565_ORDER_BGR
color ordering for RGB565 (default B,G,R for compatibility with adafruit and most SPI display)
Definition: Color.h:92
#define TGX_RGB64_ORDER_BGR
color ordering for RGB64 (default R,G,B)
Definition: Color.h:104
TGX_INLINE RGB565 interpolateColorsTriangle(const RGB565 &col1, int32_t C1, const RGB565 &col2, int32_t C2, const RGB565 &col3, const int32_t totC)
Interpolate between 3 colors.
Definition: Color.h:591
const RGB32 RGB32_Navy
Color navy in RGB32 format.
const RGB32 RGB32_Green
Color green in RGB32 format.
const RGB565 RGB565_Orange
Color orange in RGB565 format.
const RGB32 RGB32_Maroon
Color maroon in RGB32 format.
const RGB32 RGB32_Purple
Color purple in RGB32 format.
#define TGX_RGBf_ORDER_BGR
color ordering for RGBf (default R,G,B)
Definition: Color.h:108
const RGB565 RGB565_Teal
Color teal in RGB565 format.
const RGB32 RGB32_Transparent
premultiplied transparent black (0,0,0,0)
const RGB565 RGB565_Green
Color green in RGB565 format.
RGB565 meanColor(RGB565 colA, RGB565 colB)
Return the average color between colA and colB.
Definition: Color.h:647
const RGB32 RGB32_Salmon
Color salmon in RGB32 format.
const RGB565 RGB565_Gray
Color gray in RGB565 format.
const RGB565 RGB565_Purple
Color purple in RGB565 format.
const RGB565 RGB565_Cyan
Color cyan in RGB565 format.
const RGB565 RGB565_Lime
Color lime in RGB565 format.
TGX_INLINE RGBf interpolate(const RGBf &col1, const RGBf &col2, float alpha)
Interpolate between 2 colors.
Definition: Color.h:2746
const RGB565 RGB565_Black
Color black in RGB565 format.
const RGB565 RGB565_Magenta
Color majenta in RGB565 format.
const RGB565 RGB565_Maroon
Color maroon in RGB565 format.
const RGB32 RGB32_Silver
Color silver in RGB32 format.
const RGB565 RGB565_Silver
Color silver in RGB565 format.
const RGB32 RGB32_Teal
Color teal in RGB32 format.
const RGB32 RGB32_White
Color white in RGB32 format.
const RGB565 RGB565_Red
Color red in RGB565 format.
const RGB32 RGB32_Olive
Color olive in RGB32 format.
const RGB32 RGB32_Lime
Color lime in RGB32 format.
const RGB565 RGB565_Blue
Color blue in RGB565 format.
const RGB565 RGB565_Olive
Color olive in RGB565 format.
Utility/miscellaneous functions used throughout the library.
TGX_INLINE T clamp(T v, T vmin, T vmax)
Template clamp version.
Definition: Misc.h:157
3D vector.
4D vector.
Color in H/S/V format [experimental].
Definition: Color.h:2827
constexpr HSV(fVec3 v)
Constructor from a fVec3 with components (x=H, y=S, z=V) in [0.0f, 1.0f].
Definition: Color.h:2855
float S
Saturation in [0;0f, 1.0f].
Definition: Color.h:2836
HSV & operator=(const RGB64 &c)
Assignment operator from a RGB64 color.
HSV & operator=(const HSV &)=default
Default assignment operator.
HSV(const HSV &)=default
Default Copy constructor.
HSV & operator=(fVec3 v)
Assignment operator from a fVec3 with components (x=H, y=S, z=V) in [0.0f, 1.0f].
Definition: Color.h:2965
void blend(const HSV &fg_col, float alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:3014
float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:3084
HSV(uint64_t val)
Constructor from a uint64_t (seen as RGB64, component A is ignored).
HSV & operator=(fVec4 v)
Assignment operator from a fVec4 with components (x=H, y=S, z=V, w=ignored) in [0....
Definition: Color.h:2977
float V
Value in [0.F ,1.0f].
Definition: Color.h:2837
HSV(const RGBf &c)
Constructor from a RGBf color.
float H
Hue in [0.0f ,1.0f].
Definition: Color.h:2835
HSV(const RGB32 &c)
Constructor from a RGB32 color.
HSV & operator=(const RGB24 &c)
Assignment operator from a RGB24 color.
HSV(const RGB565 &c)
Constructor from a RGB565 color.
void setOpacity(float op)
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:3095
HSV(const RGB64 &c)
Constructor from a RGB64 color.
HSV & operator=(const RGB565 &c)
Assignment operator from a RGB565 color.
constexpr HSV(fVec4 v)
Constructor from a fVec4 with components (x=H, y=S, z=V, w=ignored) in [0.0f, 1.0f].
Definition: Color.h:2862
void blend256(const HSV &fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the integer range 0 (fully transparent) to 2...
Definition: Color.h:3034
constexpr bool operator!=(const HSV &c) const
Inequality comparator.
Definition: Color.h:2998
constexpr bool operator==(const HSV &c) const
Equality comparator.
Definition: Color.h:2989
HSV & operator=(const RGBf &c)
Assignment operator from a RGBf color.
HSV(uint16_t val)
Constructor from a uint16_t (seen as RGB565).
HSV()=default
Default constructor.
HSV(const RGB24 &c)
Constructor from a RGB24 color.
void mult256(int mr, int mg, int mb, int ma)
Multiply each color component by a given factor x/256 with x in [0,256].
Definition: Color.h:3061
HSV(uint32_t val)
Constructor from a uint32_t (seen as RGB32, component A is ignored).
constexpr HSV(float h, float s, float v)
Constructor from raw h, s, v values in [0.0f, 1.0f].
Definition: Color.h:2849
void premultiply()
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:3072
HSV & operator=(const RGB32 &c)
Assignment operator from a RGB32 color.
void mult256(int mr, int mg, int mb)
Multiply each color component by factor (m/256) with m in [0,256].
Definition: Color.h:3046
Color in R8/G8/B8 format.
Definition: Color.h:679
RGB24()=default
Default contructor.
float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:1071
void blend256(const RGB24 &fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the integer range 0 (fully transparent) to 2...
Definition: Color.h:1022
void operator+=(const RGB24 &c)
Add another color, component by component.
Definition: Color.h:901
constexpr RGB24(const RGB64 &c)
Constructor from a RGB64 color.
uint8_t R
Red channel (8bits)
Definition: Color.h:696
RGB24 & operator=(const RGB64 &c)
Assignment operator from a RGB64 color.
void mult256(int mr, int mg, int mb)
Multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:1035
constexpr RGB24(int r, int g, int b)
Constructor from raw r,g,b values in [0,255].
Definition: Color.h:712
constexpr RGB24(const RGB565 &c)
Constructor from a RGB24 color.
RGB24 & operator=(const HSV &c)
Assignment operator from a HSV color.
RGB24(float r, float g, float b)
Constructor from float r,g,b in [0.0f, 1.0f].
Definition: Color.h:739
constexpr RGB24(const RGB32 &c)
Constructor from a RGB32 color.
void operator*=(float v)
Multiply each component by the scalar (floating point) value v.
Definition: Color.h:956
constexpr RGB24(const RGBf &c)
Constructor from a RGBf color.
RGB24 & operator=(const RGB32 &c)
Assignment operator from a RGB32 color.
constexpr RGB24(uint64_t c)
Constructor from a uint64_t (seen as RGB64).
constexpr RGB24(uint16_t c)
Constructor from a uint16_t (seen as RGB565).
void premultiply()
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:1059
RGB24(const HSV &c)
Constructor from a HSV color.
void mult256(int mr, int mg, int mb, int ma)
Multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:1048
RGB24(const RGB24 &)=default
Default Copy constructor.
constexpr RGB24(iVec3 v)
Constructor from a iVec3 with component (x=R, y=G, z=B).
Definition: Color.h:725
RGB24 & operator=(iVec3 v)
Assignment operator from a iVec3 vector (x=R, y=G, z=B).
RGB24(fVec4 v)
Constructor from a fVec3 with component (x=R, y=G, z=B, w=ignored) in [0.0f, 1.0f].
Definition: Color.h:762
void operator-=(const RGB24 &c)
Substract another color, component by component.
Definition: Color.h:912
constexpr bool operator!=(const RGB24 &c) const
Inequality comparator.
Definition: Color.h:996
uint8_t B
Blue channel (8bits)
Definition: Color.h:698
void blend(const RGB24 &fg_col, float alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:1009
RGB24 & operator=(const RGBf &c)
Assignment operator from a RGBf color.
constexpr RGB24(iVec4 v)
Constructor from a iVec4 with component (x=R, y=G, z=B, w=ignored).
Definition: Color.h:732
RGB24 & operator=(iVec4 v)
Assignment operator from a iVec4 vector (x=R, y=G, z=B, w=ignored).
void operator-=(uint8_t v)
Substract the scalar value v to each component.
Definition: Color.h:934
void operator*=(uint8_t v)
Multiply each component by the scalar value v.
Definition: Color.h:945
uint8_t G
Green channel (8bits)
Definition: Color.h:697
constexpr RGB24(uint32_t c)
Constructor from a uint32_t (seen as RGB32).
void operator/=(uint8_t v)
Divide each component by the scalar value v.
Definition: Color.h:966
void operator+=(uint8_t v)
Add the scalar value v to each component.
Definition: Color.h:923
RGB24(fVec3 v)
Constructor from a fVec3 with component (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:755
void setOpacity(float op)
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:1082
constexpr RGB24(uint8_t *p)
Constructor from a uint8_t pointer to 3 bytes in the following order: R,G,B.
Definition: Color.h:769
RGB24 & operator=(const RGB24 &)=default
Default assignment operator.
RGB24 & operator=(fVec4 v)
Assignment operator from a fVec4 vector (x=R, y=G, z=B, w=ignored).
constexpr bool operator==(const RGB24 &c) const
Equality comparator.
Definition: Color.h:987
RGB24 & operator=(fVec3 v)
Assignment operator from a fVec3 vector (x=R, y=G, z=B).
RGB24 & operator=(const RGB565 &c)
Assignment operator from a RGB565 color.
void operator/=(float v)
Divide each component by the scalar (floating point value) v.
Definition: Color.h:976
Color in R8/G8/B8/A8 format.
Definition: Color.h:1174
uint32_t val
color as uint32_t
Definition: Color.h:1188
constexpr RGB32(uint64_t val)
Constructor from a uint64_t (seen as RGB64).
constexpr RGB32(const RGB64 &c)
Constructor from a RGB64 color.
static const uint8_t DEFAULT_A
fully opaque alpha value
Definition: Color.h:1183
constexpr RGB32(iVec4 v)
Constructor from a fVec4 with components (x=R, y=G, z=B, w=A) in [0,255].
Definition: Color.h:1236
RGB32 & operator=(const RGB24 &c)
Assignment operator from a RGB24 color.
constexpr RGB32(uint32_t c)
Constructor from a uint32_t.
Definition: Color.h:1276
void premultiply()
Convert the color from plain alpha to pre-multiplied alpha.
Definition: Color.h:1615
void setOpaque()
Set the alpha channel of the color to fully opaque.
Definition: Color.h:1682
void mult256(int mr, int mg, int mb)
Multiply each color component by a given factor m/256 with m in [0,256] except the A component.
Definition: Color.h:1588
RGB32 getMultOpacity(float op) const
Return a copy of this color with opacity multiplied by a given factor in [0.0f, 1....
Definition: Color.h:1665
RGB32 & operator=(fVec3 v)
Assignment operator from a vector (x=R, y=G, z=B) in [0.0f, 1.0f].
void setTransparent()
Set the alpha channel of the color to fully transparent.
Definition: Color.h:1691
constexpr RGB32(const RGB24 &c)
Constructor from a RGB24 color.
void operator-=(uint8_t v)
Substract the scalar value v to each component (including alpha channel).
Definition: Color.h:1462
void mult256(int mr, int mg, int mb, int ma)
Multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:1599
constexpr RGB32(int r, int g, int b, int a=DEFAULT_A)
Constructor from raw r,g,b,a values.
Definition: Color.h:1216
void operator*=(float v)
Multiply each component by the scalar (floating point) value v (including alpha channel).
Definition: Color.h:1486
RGB32 & operator=(iVec3 v)
Assignment operator from a vector (x=R, y=G, z=B).
RGB32 & operator=(const RGB32 &)=default
Default assignment operator.
constexpr RGB32(iVec3 v)
Constructor from a fVec3 with components (x=R, y=G, z=B) in [0,255].
Definition: Color.h:1229
void operator+=(uint8_t v)
Add the scalar value v to each component (including alpha channel).
Definition: Color.h:1450
uint8_t A
Alpha channel (8bits)
Definition: Color.h:1195
RGB32()=default
Default constructor.
RGB32(fVec3 v)
Constructor from a fVec3 with components (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:1262
constexpr RGB32(const RGB565 &c)
Constructor from a RGB565 color.
uint8_t R
Red channel (8bits)
Definition: Color.h:1194
void operator+=(const RGB32 &c)
Add another color, component by component (including alpha channel).
Definition: Color.h:1426
void blend(RGB32 fg_col)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:1579
void operator-=(const RGB32 &c)
Substract another color, component by component (including alpha channel).
Definition: Color.h:1438
void blend256(const RGB32 &fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:1559
void operator/=(uint8_t v)
Divide each component by the scalar value v (including alpha channel).
Definition: Color.h:1497
RGB32 & operator=(const RGB565 &c)
Assignment operator from a RGB565 color.
RGB32(fVec4 v)
Constructor from a fVec4 with components (x=R, y=G, z=B, w=A) in [0.0f, 1.0f].
Definition: Color.h:1269
constexpr bool operator==(const RGB32 &c) const
Equality comparator.
Definition: Color.h:1520
RGB32 & operator=(iVec4 v)
Assignment operator from a vector (x=R, y=G, z=B, w=A).
uint8_t G
Green channel (8bits)
Definition: Color.h:1193
RGB32 & operator=(const HSV &c)
Assignment operator from a HSV color.
RGB32 & operator=(fVec4 v)
Assignment operator from a vector (x=R, y=G, z=B, w=A) in [0.0f, 1.0f].
RGB32(const HSV &c)
Constructor from a HSV color.
void operator*=(uint8_t v)
Multiply each component by the scalar value v (including alpha channel).
Definition: Color.h:1474
RGB32 & operator=(const RGB64 &c)
Assignment operator from a RGB64 color.
RGB32(float r, float g, float b, float a=-1.0f)
Constructor from float r,g,b,a in [0.0f, 1.0f].
Definition: Color.h:1243
uint8_t B
Blue channel (8bits)
Definition: Color.h:1192
RGB32 & operator=(const RGBf &c)
Assignment operator from a RGBf color.
constexpr RGB32(const RGBf &c)
Constructor from a RGBf color.
RGB32(const RGB32 &)=default
Default Copy constructor.
constexpr bool operator!=(const RGB32 &c) const
Inequality comparator.
Definition: Color.h:1529
constexpr RGB32(uint16_t val)
Constructor from a uint16_t (seen as RGB565).
void blend(const RGB32 &fg_col, float alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:1544
void multOpacity(float op)
Multiply the opacity of the color by a given factor in [0.0f, 1.0f].
Definition: Color.h:1654
void setOpacity(float op)
Change the opacity of the color to a given value in [0.0f, 1.0f].
Definition: Color.h:1640
float opacity() const
Return the opacity (alpha channel value) of this color in the range [0,1] (0=fully transparent,...
Definition: Color.h:1627
void operator/=(float v)
Divide each component by the scalar (floating point value) v, including the alpha channel.
Definition: Color.h:1508
Color in R5/G6/B5 format.
Definition: Color.h:216
void mult256(int mr, int mg, int mb, int ma)
Multiply each color component by a given factor x/256 with x in [0,256].
Definition: Color.h:542
RGB565(float r, float g, float b)
Constructor from float r,g,b in [0.0f, 1.0f].
Definition: Color.h:291
constexpr RGB565(const RGB24 &c)
Constructor from a RGB24 color.
RGB565(fVec3 v)
Constructor from a fVec3 vector with components (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:307
void premultiply()
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:553
constexpr RGB565(const RGB565 &)=default
Default Copy constructor.
constexpr RGB565(iVec3 v)
Constructor from a iVec3 vector (x=R, y=G, z=B).
Definition: Color.h:272
void blend(RGB565 fg_col, float alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:503
RGB565()=default
Default constructor.
RGB565 & operator=(fVec4 v)
Assignment operator from a fVec4.
void mult256(int mr, int mg, int mb)
Multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:529
constexpr RGB565(const RGB64 &c)
Constructor from a RGB64 color.
constexpr RGB565(iVec4 v)
Constructor from an iVec4 vector (x=R, y=G, z=B, w=ignored).
Definition: Color.h:284
RGB565 & operator=(const RGB32 &c)
Assignment operator from a RGB32 color.
void setOpacity(float op)
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:576
constexpr bool operator==(const RGB565 &c) const
Equality comparator.
Definition: Color.h:481
constexpr RGB565(const RGBf &c)
Constructor from a RGBf color.
RGB565(fVec4 v)
Constructor from a fVec4 vector with components (x=R, y=G, z=B, w=ignored) in [0.0f,...
Definition: Color.h:314
constexpr RGB565(uint16_t c)
Constructor from a uint16_t.
Definition: Color.h:321
float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:565
RGB565 & operator=(const RGB565 &)=default
Default assignment operator.
constexpr RGB565(const RGB32 &c)
Constructor from a RGB32 color.
constexpr bool operator!=(const RGB565 &c) const
Inequality comparator.
Definition: Color.h:490
uint16_t G
Green channel (6 bits)
Definition: Color.h:232
void operator-=(const RGB565 &c)
Substract another color, component by component.
Definition: Color.h:470
RGB565 & operator=(iVec4 v)
Assignment operator from a iVec4.
constexpr RGB565(uint64_t val)
Constructor from a uint64_t (seen as RGB64).
uint16_t val
color as uint16_t
Definition: Color.h:227
void operator+=(const RGB565 &c)
Add another color, component by component.
Definition: Color.h:459
RGB565 & operator=(const RGB24 &c)
Assignment operator from a RGB24 color.
RGB565(const HSV &c)
Constructor from a HSV color.
RGB565 & operator=(const HSV &c)
Assignment operator from a HSV color.
uint16_t R
Red channel (5 bits)
Definition: Color.h:233
RGB565 & operator=(const RGBf &c)
Assignment operator from a RGBf color.
constexpr RGB565(int r, int g, int b)
Constructor from R,G,B values.
Definition: Color.h:256
RGB565 & operator=(fVec3 v)
Assignment operator from a fVec3.
void blend256(const RGB565 &fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the integer range 0 (fully transparent) to 2...
Definition: Color.h:516
RGB565 & operator=(iVec3 v)
Assignment operator from an iVec3 vector.
RGB565 & operator=(const RGB64 &c)
Assignment operator from a RGB64 color.
constexpr RGB565(uint32_t val)
Constructor from a uint32_t (seen as RGB32).
uint16_t B
Blue channel (5 bits)
Definition: Color.h:231
Color in R16/G16/B16/A16 format.
Definition: Color.h:1789
uint16_t B
Blue channel (16 bits)
Definition: Color.h:1817
RGB64(const RGB64 &)=default
Default Copy ctor.
void operator/=(uint16_t v)
Divide each component by the scalar value v, including the alpha channel.
Definition: Color.h:2112
RGB64(const RGB32 &c)
Constructor from a RGB32 color.
RGB64()=default
Default constructor.
void operator*=(float v)
Multiply each component by the scalar (floating point) value v, including the alpha channel.
Definition: Color.h:2101
void operator-=(const RGB64 &c)
Substract another color, component by component, including the alpha channel.
Definition: Color.h:2053
void blend256(const RGB64 &fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the integer range 0 (fully transparent) to 2...
Definition: Color.h:2174
void blend(const RGB64 &fg_col, float alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:2159
RGB64(const RGB565 &c)
Constructor from a RGB565 color.
void multOpacity(float op)
Multiply the opacity of the color by a given factor in [0.0f, 1.0f].
Definition: Color.h:2284
void setOpaque()
Set the alpha channel of the color to fully opaque.
Definition: Color.h:2304
RGB64(const RGB24 &c)
Constructor from a RGB24 color.
RGB64 getMultOpacity(float op) const
Return a copy of this color with opacity multiplied by a given factor in [0.0f, 1....
Definition: Color.h:2295
void operator+=(const RGB64 &c)
Add another color, component by component, including the alpha channel.
Definition: Color.h:2041
RGB64 & operator=(iVec4 v)
Assignment operator from a iVec4 with components (x=R, y=G, z=B, w=A) in [0,65535].
RGB64 & operator=(fVec3 v)
Assignment operator from a fVec3 with components (x=R, y=G, z=B) in [0.0f,1.0f].
uint16_t A
Alpha channel (16 bits)
Definition: Color.h:1818
RGB64(const RGBf &c)
Constructor from a RGBf color.
RGB64(const HSV &c)
Constructor from a HSV color.
RGB64 & operator=(const HSV &c)
Assignment operator from a HSV color.
RGB64(uint32_t val)
Constructor from a uint32_t (seen as RGB32).
float opacity() const
Return the opacity (alpha channel value) of this color in the range [0,1] (0=fully transparent,...
Definition: Color.h:2257
void blend65536(RGB64 fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the integer range 0 (fully transparent) to 6...
Definition: Color.h:2189
void setOpacity(float op)
Change the opacity of the color to a given value in [0.0f, 1.0f].
Definition: Color.h:2270
RGB64 & operator=(const RGB24 &c)
Assignment operator from a RGB24 color.
uint64_t val
color as uint64_t
Definition: Color.h:1804
RGB64 & operator=(const RGBf &c)
Assignment operator from a RGBf color.
constexpr bool operator==(const RGB64 &c) const
Equality comparator.
Definition: Color.h:2135
static const uint16_t DEFAULT_A
fully opaque alpha value
Definition: Color.h:1798
constexpr bool operator!=(const RGB64 &c) const
Inequality comparator.
Definition: Color.h:2144
RGB64(uint16_t val)
Constructor from a uint16_t (seen as RGB565).
void blend(const RGB64 &fg_col)
alpha-blend fg_col over this one.
Definition: Color.h:2209
RGB64 & operator=(const RGB32 &c)
Assignment operator from a RGB32 color.
uint16_t G
Green channel (16 bits)
Definition: Color.h:1816
void operator+=(uint16_t v)
Add the scalar value v to each component, including the alpha channel.
Definition: Color.h:2065
RGB64 & operator=(iVec3 v)
Assignment operator from a iVec3 with components (x=R, y=G, z=B) in [0,65535].
RGB64 & operator=(const RGB565 &c)
Assignment operator from a RGB24 color.
void operator-=(uint16_t v)
Substract the scalar value v to each component, including the alpha channel.
Definition: Color.h:2077
RGB64(fVec3 v)
Constructor from a fVec3 with components (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:1877
RGB64(fVec4 v)
Constructor from a fVec4 with components (x=R, y=G, z=B, w=A) in [0.0f, 1.0f].
Definition: Color.h:1884
void mult256(int mr, int mg, int mb, int ma)
Multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:2229
void mult256(int mr, int mg, int mb)
Multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:2218
constexpr RGB64(iVec3 v)
Constructor from a iVec3 with components (x=R, y=G, z=B) in [0,65535].
Definition: Color.h:1845
RGB64 & operator=(fVec4 v)
Assignment operator from a fVec3 with components (x=R, y=G, z=B, w=A) in [0.0f,1.0f].
RGB64 & operator=(const RGB64 &)=default
Default assignment operator.
void operator/=(float v)
Divide each component by the scalar (floating point value) v, including the alpha channel.
Definition: Color.h:2123
uint16_t R
Red channel (16 bits)
Definition: Color.h:1815
constexpr RGB64(int r, int g, int b, int a=DEFAULT_A)
Constructor from raw r,g,b,a value in [0,65535].
Definition: Color.h:1833
void operator*=(uint16_t v)
Multiply each component by the scalar value v, including the alpha channel.
Definition: Color.h:2089
RGB64(float r, float g, float b, float a=-1.0f)
Constructor from float r,g,b,a in [0.0f, 1.0f].
Definition: Color.h:1859
constexpr RGB64(iVec4 v)
Constructor from a iVec4 with components (x=R, y=G, z=B, w=A) in [0,65535].
Definition: Color.h:1852
void premultiply()
Convert the color from plain alpha to pre-multiplied alpha.
Definition: Color.h:2245
constexpr RGB64(uint64_t c)
Constructor from a uint64_t.
Definition: Color.h:1891
void setTransparent()
Set the alpha channel of the color to fully transparent.
Definition: Color.h:2313
Color in R,G,B float format.
Definition: Color.h:2405
constexpr RGBf(uint64_t val)
Constructor from a uint64_t (seen as RGB64, the A component is ignored).
constexpr RGBf(float r, float g, float b)
Constructor from raw r,g,b values in [0.0f, 1.0f].
Definition: Color.h:2433
float R
Red channel.
Definition: Color.h:2418
TGX_INLINE RGBf operator*(const RGBf &c) const
Return the color obtained by multipliying the channels of both colors together.
Definition: Color.h:2599
constexpr RGBf(const RGB32 &c)
Constructor from a RGB32 color.
TGX_INLINE void blend256(const RGBf &fg_col, uint32_t alpha)
alpha-blend fg_col over this one with a given opacity in the integer range 0 (fully transparent) to 2...
Definition: Color.h:2660
constexpr RGBf(const fVec4 &v)
Constructor from a fVec4 with components (x=R, y=G, z=B, w=ignored) in [0.0f, 1.0f].
Definition: Color.h:2452
constexpr RGBf(const RGBf &)=default
Default Copy constructor.
TGX_INLINE void clamp()
Clamp all color channel to [0.0f,1.0f].
Definition: Color.h:2645
TGX_INLINE void mult256(int mr, int mg, int mb)
multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:2684
RGBf & operator=(const RGB24 &c)
Assignment operator from a RGB24 color.
RGBf & operator=(const RGB64 &c)
Assignment operator from a RGB64 color.
TGX_INLINE void operator*=(float a)
Multiply each channel by a constant.
Definition: Color.h:2607
TGX_INLINE RGBf operator*(float a) const
Return the color where all components are multiplied by a.
Definition: Color.h:2618
TGX_INLINE void mult256(int mr, int mg, int mb, int ma)
multiply each color component by a given factor m/256 with m in [0,256].
Definition: Color.h:2696
RGBf & operator=(const HSV &c)
Assignment operator from a HSV color.
RGBf()=default
Default constructor.
constexpr RGBf(uint32_t val)
Constructor from a uint32_t (seen as RGB32, the A component is ignored).
RGBf & operator=(const RGB565 &c)
Assignment operator from a RGB565 color.
constexpr bool operator!=(const RGBf &c) const
Inequality comparator.
Definition: Color.h:2636
TGX_INLINE float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:2719
RGBf & operator=(const RGB32 &c)
Assignment operator from a RGB32 color.
TGX_INLINE void operator-=(const RGBf &c)
Substract another color, component by component.
Definition: Color.h:2578
float B
Blue channel.
Definition: Color.h:2420
constexpr RGBf(const RGB64 &c)
Constructor from a RGB64 color.
RGBf & operator=(const RGBf &)=default
Default assignment operator.
float G
Green channel.
Definition: Color.h:2419
RGBf(const HSV &c)
Constructor from a HSV color.
constexpr bool operator==(const RGBf &c) const
Equality comparator.
Definition: Color.h:2627
RGBf & operator=(const fVec4 &v)
Assignment operator from a fVec4 with components (x=R, y=G, z=B, w=ignored) in [0....
TGX_INLINE void operator*=(const RGBf &c)
Multiply each channel by the same channel on c.
Definition: Color.h:2589
constexpr RGBf(const RGB24 &c)
Constructor from a RGB24 color.
RGBf & operator=(const fVec3 &v)
Assignment operator from a fVec3 with components (x=R, y=G, z=B) in [0.0f, 1.0f].
constexpr RGBf(const fVec3 &v)
Constructor from a fVec3 with components (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:2445
constexpr RGBf(uint16_t val)
Constructor from a uint16_t (seen as RGB565).
constexpr RGBf(const RGB565 &c)
Constructor from a RGB565 color.
TGX_INLINE void setOpacity(float op)
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:2730
TGX_INLINE void operator+=(const RGBf &c)
Add another color, component by component.
Definition: Color.h:2567
TGX_INLINE void premultiply()
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:2707
TGX_INLINE void blend(const RGBf &fg_col, float alpha)
alpha-blend fg_col over this one with a given opacity in the range 0.0f (fully transparent) to 1....
Definition: Color.h:2673
T x
'x' coordinate (first dimension)
Definition: Vec2.h:72
T y
'y' coordinate (second dimension)
Definition: Vec2.h:73
Generic 3D vector [specializations iVec3, fVec3, dVec3].
Definition: Vec3.h:70
T z
'z' coordinate (third dimension)
Definition: Vec3.h:83
Geenric 4D vector [specializations iVec4, fVec4, dVec4].
Definition: Vec4.h:71