TGX 1.0.3
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 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 inline RGB565 interpolateColorsBilinear(const RGB565 & C00, const RGB565 & C10, const RGB565 & C01, const RGB565 & C11, const float ax, const float ay)
622 {
623 /* flotating 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( ((int)colA.R + (int)colB.R) >> 1,
650 ((int)colA.G + (int)colB.G) >> 1,
651 ((int)colA.B + (int)colB.B) >> 1);
652 }
653
654
660 inline RGB565 meanColor(RGB565 colA, RGB565 colB, RGB565 colC, RGB565 colD)
661 {
662 return RGB565(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
663 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
664 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2);
665 }
666
667
668
669
670
680struct RGB24
681 {
682
683
684 // mtools extension (if available).
685 #if (MTOOLS_TGX_EXTENSIONS)
686 #include <mtools/extensions/tgx/tgx_ext_Color_RGB24.inl>
687 #endif
688
689
690 // no dual memory represention
691 // no alignement, just 3 consecutive uint8_t
692
693#if TGX_RGB24_ORDER_BGR
694 uint8_t B;
695 uint8_t G;
696 uint8_t R;
697#else
698 uint8_t R;
699 uint8_t G;
700 uint8_t B;
701#endif
702
703
704
708 RGB24() = default;
709
710
714 constexpr RGB24(int r, int g, int b) :
716 B((uint8_t)b), G((uint8_t)g), R((uint8_t)r)
717 #else
718 R((uint8_t)r), G((uint8_t)g), B((uint8_t)b)
719 #endif
720
721 {}
722
723
727 constexpr RGB24(iVec3 v) : RGB24(v.x, v.y, v.z)
728 {}
729
730
734 constexpr RGB24(iVec4 v) : RGB24(v.x, v.y, v.z)
735 {}
736
737
741 RGB24(float r, float g, float b) :
743 B((uint8_t)(b * 255)),
744 G((uint8_t)(g * 255)),
745 R((uint8_t)(r * 255))
746 #else
747 R((uint8_t)(r * 255)),
748 G((uint8_t)(g * 255)),
749 B((uint8_t)(b * 255))
750 #endif
751 {}
752
753
757 RGB24(fVec3 v) : RGB24(v.x, v.y, v.z)
758 {}
759
760
764 RGB24(fVec4 v) : RGB24(v.x, v.y, v.z)
765 {}
766
767
771 constexpr RGB24(uint8_t * p) : R(p[0]), G(p[1]), B(p[2]) {}
772
773
777 RGB24(const RGB24&) = default;
778
779
783 constexpr inline RGB24(uint16_t c);
784
785
789 constexpr inline RGB24(uint32_t c);
790
791
795 constexpr inline RGB24(uint64_t c);
796
797
801 constexpr inline RGB24(const RGB565& c);
802
803
807 constexpr inline RGB24(const RGB32& c);
808
809
813 constexpr inline RGB24(const RGB64& c);
814
815
819 constexpr inline RGB24(const RGBf& c);
820
821
825 RGB24(const HSV& c);
826
827
831 explicit operator iVec3() const { return iVec3(R, G, B); }
832
833
837 explicit operator fVec3() const { return fVec3(R / 255.0f, G / 255.0f, B / 255.0f); }
838
839
843 RGB24& operator=(const RGB24&) = default;
844
845
849 inline RGB24& operator=(const RGB565& c);
850
851
855 inline RGB24& operator=(const RGB32& c);
856
857
861 inline RGB24& operator=(const RGB64& c);
862
863
867 inline RGB24& operator=(const RGBf& c);
868
869
873 RGB24& operator=(const HSV& c);
874
875
880
881
886
887
892
893
898
899
903 void operator+=(const RGB24 & c)
904 {
905 R += c.R;
906 G += c.G;
907 B += c.B;
908 }
909
910
914 void operator-=(const RGB24& c)
915 {
916 R -= c.R;
917 G -= c.G;
918 B -= c.B;
919 }
920
921
925 void operator+=(uint8_t v)
926 {
927 R += v;
928 G += v;
929 B += v;
930 }
931
932
936 void operator-=(uint8_t v)
937 {
938 R -= v;
939 G -= v;
940 B -= v;
941 }
942
943
947 void operator*=(uint8_t v)
948 {
949 R *= v;
950 G *= v;
951 B *= v;
952 }
953
954
958 void operator*=(float v)
959 {
960 R = (uint8_t)(R * v);
961 G = (uint8_t)(G * v);
962 B = (uint8_t)(B * v);
963 }
964
968 void operator/=(uint8_t v)
969 {
970 R /= v;
971 G /= v;
972 B /= v;
973 }
974
978 void operator/=(float v)
979 {
980 R = (uint8_t)(R / v);
981 G = (uint8_t)(G / v);
982 B = (uint8_t)(B / v);
983 }
984
985
989 constexpr bool operator==(const RGB24& c) const
990 {
991 return((R == c.R) && (G == c.G) && (B == c.B));
992 }
993
994
998 constexpr bool operator!=(const RGB24& c) const
999 {
1000 return !(*this == c);
1001 }
1002
1003
1011 inline void blend(const RGB24 & fg_col, float alpha)
1012 {
1013 blend256(fg_col, (uint32_t)(alpha * 256));
1014 }
1015
1016
1024 inline void blend256(const RGB24& fg_col, uint32_t alpha)
1025 {
1026 const uint16_t a = (uint16_t)alpha;
1027 const uint16_t ia = (uint16_t)(256 - alpha);
1028 R = ((fg_col.R * a) + (R * ia)) >> 8;
1029 G = ((fg_col.G * a) + (G * ia)) >> 8;
1030 B = ((fg_col.B * a) + (B * ia)) >> 8;
1031 }
1032
1033
1037 inline void mult256(int mr, int mg, int mb)
1038 {
1039 R = (R * mr) >> 8;
1040 G = (G * mg) >> 8;
1041 B = (B * mb) >> 8;
1042 }
1043
1044
1050 inline void mult256(int mr, int mg, int mb, int ma)
1051 {
1052 mult256(mr, mg, mb);
1053 }
1054
1055
1061 inline void premultiply()
1062 {
1063 // nothing here.
1064 return;
1065 }
1066
1067
1073 float opacity() const
1074 {
1075 return 1.0f;
1076 }
1077
1078
1084 void setOpacity(float op)
1085 {
1086 // nothing here.
1087 return;
1088 }
1089
1090
1091 };
1092
1093
1094
1100 inline RGB24 interpolateColorsTriangle(const RGB24& col1, int32_t C1, const RGB24& col2, int32_t C2, const RGB24& col3, const int32_t totC)
1101 {
1102 return RGB24((int)(col3.R + (C1 * (col1.R - col3.R) + C2 * (col2.R - col3.R)) / totC),
1103 (int)(col3.G + (C1 * (col1.G - col3.G) + C2 * (col2.G - col3.G)) / totC),
1104 (int)(col3.B + (C1 * (col1.B - col3.B) + C2 * (col2.B - col3.B)) / totC));
1105 }
1106
1107
1124 inline RGB24 interpolateColorsBilinear(const RGB24 & C00, const RGB24 & C10, const RGB24 & C01, const RGB24 & C11, const float ax, const float ay)
1125 {
1126 const int iax = (int)(ax * 256);
1127 const int iay = (int)(ay * 256);
1128 const int rax = 256 - iax;
1129 const int ray = 256 - iay;
1130 const int R = rax*(ray*C00.R + iay*C01.R) + iax*(ray*C10.R + iay*C11.R);
1131 const int G = rax*(ray*C00.G + iay*C01.G) + iax*(ray*C10.G + iay*C11.G);
1132 const int B = rax*(ray*C00.B + iay*C01.B) + iax*(ray*C10.B + iay*C11.B);
1133 return RGB24(R >> 16,G >> 16,B >> 16);
1134 }
1135
1136
1140 inline RGB24 meanColor(const RGB24 & colA, const RGB24 & colB)
1141 {
1142 return RGB24(((int)colA.R + (int)colB.R) >> 1,
1143 ((int)colA.G + (int)colB.G) >> 1,
1144 ((int)colA.B + (int)colB.B) >> 1);
1145 }
1146
1147
1151 inline RGB24 meanColor(const RGB24& colA, const RGB24& colB, const RGB24& colC, const RGB24& colD)
1152 {
1153 return RGB24(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
1154 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
1155 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2);
1156 }
1157
1158
1159
1160
1161
1175struct RGB32
1176 {
1177
1178
1179 // mtools extension (if available).
1180 #if (MTOOLS_TGX_EXTENSIONS)
1181 #include <mtools/extensions/tgx/tgx_ext_Color_RGB32.inl>
1182 #endif
1183
1184
1185 static const uint8_t DEFAULT_A = 255;
1186
1187 // dual memory representation
1188 union
1189 {
1190 uint32_t val;
1191 struct
1192 {
1193#if TGX_RGB32_ORDER_BGR
1194 uint8_t B;
1195 uint8_t G;
1196 uint8_t R;
1197 uint8_t A;
1198
1199#else
1200 uint8_t R;
1201 uint8_t G;
1202 uint8_t B;
1203 uint8_t A;
1204#endif
1205 };
1206 };
1207
1208
1212 RGB32() = default;
1213
1214
1218 constexpr RGB32(int r, int g, int b, int a = DEFAULT_A) :
1220 B((uint8_t)b), G((uint8_t)g), R((uint8_t)r), A((uint8_t)a)
1221 #else
1222 R((uint8_t)r), G((uint8_t)g), B((uint8_t)b), A((uint8_t)a)
1223 #endif
1224 {}
1225
1226
1231 constexpr RGB32(iVec3 v) : RGB32(v.x, v.y, v.z)
1232 {}
1233
1234
1238 constexpr RGB32(iVec4 v) : RGB32(v.x, v.y, v.z, v.w)
1239 {}
1240
1241
1245 RGB32(float r, float g, float b, float a = -1.0f) :
1247 B((uint8_t)(b * 255)),
1248 G((uint8_t)(g * 255)),
1249 R((uint8_t)(r * 255)),
1250 A((uint8_t)((a < 0.0f) ? DEFAULT_A : ((uint8_t)roundf(a * 255.0f))))
1251 #else
1252 R((uint8_t)(r * 255.0f)),
1253 G((uint8_t)(g * 255.0f)),
1254 B((uint8_t)(b * 255.0f)),
1255 A((uint8_t)((a < 0.0f) ? DEFAULT_A : ((uint8_t)roundf(a * 255.0f))))
1256 #endif
1257 {}
1258
1259
1264 RGB32(fVec3 v) : RGB32(v.x, v.y, v.z)
1265 {}
1266
1267
1271 RGB32(fVec4 v) : RGB32(v.x, v.y, v.z, v.w)
1272 {}
1273
1274
1278 constexpr RGB32(uint32_t c) : val(c) {}
1279
1280
1284 constexpr inline RGB32(uint16_t val);
1285
1286
1290 constexpr inline RGB32(uint64_t val);
1291
1292
1296 RGB32(const RGB32&) = default;
1297
1298
1302 constexpr inline RGB32(const RGB565& c);
1303
1304
1308 constexpr inline RGB32(const RGB24& c);
1309
1310
1314 constexpr inline RGB32(const RGB64& c);
1315
1316
1320 constexpr inline RGB32(const RGBf& c);
1321
1322
1326 RGB32(const HSV& c);
1327
1328
1332 explicit operator uint32_t&() { return val; }
1333
1334
1338 explicit operator const uint32_t& () const { return val; }
1339
1340
1344 explicit operator iVec3() const { return iVec3(R, G, B); }
1345
1346
1350 explicit operator fVec3() const { return fVec3(R / 255.0f, G / 255.0f, B / 255.0f); }
1351
1352
1356 explicit operator iVec4() const { return iVec4(R, G, B, A); }
1357
1358
1362 explicit operator fVec4() const { return fVec4(R / 255.0f, G / 255.0f, B / 255.0f, A / 255.0f); }
1363
1364
1368 RGB32& operator=(const RGB32&) = default;
1369
1370
1374 inline RGB32& operator=(const RGB565& c);
1375
1376
1380 inline RGB32& operator=(const RGB24& c);
1381
1382
1386 inline RGB32& operator=(const RGB64& c);
1387
1388
1392 inline RGB32& operator=(const RGBf& c);
1393
1394
1398 RGB32& operator=(const HSV& c);
1399
1400
1405
1406
1411
1412
1417
1418
1423
1424
1428 void operator+=(const RGB32& c)
1429 {
1430 R += c.R;
1431 G += c.G;
1432 B += c.B;
1433 A += c.A;
1434 }
1435
1436
1440 void operator-=(const RGB32& c)
1441 {
1442 R -= c.R;
1443 G -= c.G;
1444 B -= c.B;
1445 A -= c.A;
1446 }
1447
1448
1452 void operator+=(uint8_t v)
1453 {
1454 R += v;
1455 G += v;
1456 B += v;
1457 A += v;
1458 }
1459
1460
1464 void operator-=(uint8_t v)
1465 {
1466 R -= v;
1467 G -= v;
1468 B -= v;
1469 A -= v;
1470 }
1471
1472
1476 void operator*=(uint8_t v)
1477 {
1478 R *= v;
1479 G *= v;
1480 B *= v;
1481 A *= v;
1482 }
1483
1484
1488 void operator*=(float v)
1489 {
1490 R = (uint8_t)(R * v);
1491 G = (uint8_t)(G * v);
1492 B = (uint8_t)(B * v);
1493 A = (uint8_t)(A * v);
1494 }
1495
1499 void operator/=(uint8_t v)
1500 {
1501 R /= v;
1502 G /= v;
1503 B /= v;
1504 A /= v;
1505 }
1506
1510 void operator/=(float v)
1511 {
1512 R = (uint8_t)(R / v);
1513 G = (uint8_t)(G / v);
1514 B = (uint8_t)(B / v);
1515 A = (uint8_t)(A / v);
1516 }
1517
1518
1522 constexpr bool operator==(const RGB32& c) const
1523 {
1524 return(val == c.val);
1525 }
1526
1527
1531 constexpr bool operator!=(const RGB32& c) const
1532 {
1533 return !(*this == c);
1534 }
1535
1536
1546 inline void blend(const RGB32 & fg_col, float alpha)
1547 {
1548 blend256(fg_col, (uint32_t)(alpha * 256));
1549 }
1550
1551
1561 inline void blend256(const RGB32 & fg_col, uint32_t alpha)
1562 {
1563 // below is the correct alpha blending with pre-multiplied alpha
1564 // we do 'real' interpolate with eternal 'alpha' but not with the 'A' component of fg_col
1565 // since it is already pre-multiplied
1566 const uint32_t inv_alpha = (65536 - (alpha * (fg_col.A + (fg_col.A > 127)))) >> 8;
1567 const uint32_t ag = ((fg_col.val & 0xFF00FF00) >> 8)*alpha + ((val & 0xFF00FF00) >> 8)*inv_alpha;
1568 const uint32_t rb = (fg_col.val & 0x00FF00FF) * alpha + (val & 0x00FF00FF) * inv_alpha;
1569 val = (ag & 0xFF00FF00) | ((rb >> 8) & 0x00FF00FF);
1570 }
1571
1572
1581 inline void blend(RGB32 fg_col)
1582 {
1583 blend256(fg_col, 256);
1584 }
1585
1586
1590 inline void mult256(int mr, int mg, int mb)
1591 {
1592 R = (R * mr) >> 8;
1593 G = (G * mg) >> 8;
1594 B = (B * mb) >> 8;
1595 }
1596
1597
1601 inline void mult256(int mr, int mg, int mb, int ma)
1602 {
1603 R = (R * mr) >> 8;
1604 G = (G * mg) >> 8;
1605 B = (B * mb) >> 8;
1606 A = (B * ma) >> 8;
1607 }
1608
1609
1617 inline void premultiply()
1618 {
1619 R = (uint8_t)((((uint16_t)R) * A) / 255);
1620 G = (uint8_t)((((uint16_t)G) * A) / 255);
1621 B = (uint8_t)((((uint16_t)B) * A) / 255);
1622 }
1623
1624
1629 float opacity() const
1630 {
1631 return (((float)A)/ 255.0f);
1632 }
1633
1634
1642 void setOpacity(float op)
1643 {
1644 // slow version
1645 float mo = op * 255.0f;
1646 float mult = (A == 0) ? 0.0f : (mo / ((float)A));
1647 (*this) = RGB32((int)(R * mult), (int)(G * mult), (int)(B * mult), (int)mo);
1648 }
1649
1650
1656 void multOpacity(float op)
1657 {
1658 *this = getMultOpacity(op);
1659 }
1660
1661
1667 RGB32 getMultOpacity(float op) const
1668 {
1669 // faster
1670 uint32_t o = (uint32_t)(256 * op);
1671 uint32_t ag = (val & 0xFF00FF00) >> 8;
1672 uint32_t rb = val & 0x00FF00FF;
1673 uint32_t sag = o * ag;
1674 uint32_t srb = o * rb;
1675 sag = sag & 0xFF00FF00;
1676 srb = (srb >> 8) & 0x00FF00FF;
1677 return RGB32(sag | srb);
1678 }
1679
1680
1685 {
1686 setOpacity(1.0f);
1687 }
1688
1689
1694 {
1695 R = 0;
1696 G = 0;
1697 B = 0;
1698 A = 0;
1699 }
1700
1701
1702 };
1703
1704
1705
1711 inline RGB32 interpolateColorsTriangle(const RGB32& col1, int32_t C1, const RGB32& col2, int32_t C2, const RGB32& col3, const int32_t totC)
1712 {
1713 return RGB32((int)(col3.R + (C1 * (col1.R - col3.R) + C2 * (col2.R - col3.R)) / totC),
1714 (int)(col3.G + (C1 * (col1.G - col3.G) + C2 * (col2.G - col3.G)) / totC),
1715 (int)(col3.B + (C1 * (col1.B - col3.B) + C2 * (col2.B - col3.B)) / totC),
1716 (int)(col3.A + (C1 * (col1.A - col3.A) + C2 * (col2.A - col3.A)) / totC));
1717 }
1718
1719
1736 inline RGB32 interpolateColorsBilinear(const RGB32 & C00, const RGB32 & C10, const RGB32 & C01, const RGB32 & C11, const float ax, const float ay)
1737 {
1738 const int iax = (int)(ax * 256);
1739 const int iay = (int)(ay * 256);
1740 const int rax = 256 - iax;
1741 const int ray = 256 - iay;
1742 const int R = rax*(ray*C00.R + iay*C01.R) + iax*(ray*C10.R + iay*C11.R);
1743 const int G = rax*(ray*C00.G + iay*C01.G) + iax*(ray*C10.G + iay*C11.G);
1744 const int B = rax*(ray*C00.B + iay*C01.B) + iax*(ray*C10.B + iay*C11.B);
1745 const int A = rax*(ray*C00.A + iay*C01.A) + iax*(ray*C10.A + iay*C11.A);
1746 return RGB32(R >> 16,G >> 16,B >> 16,A >> 16);
1747 }
1748
1749
1753 inline RGB32 meanColor(RGB32 colA, RGB32 colB)
1754 {
1755 return RGB32(((int)colA.R + (int)colB.R) >> 1,
1756 ((int)colA.G + (int)colB.G) >> 1,
1757 ((int)colA.B + (int)colB.B) >> 1,
1758 ((int)colA.A + (int)colB.A) >> 1);
1759 }
1760
1761
1765 inline RGB32 meanColor(RGB32 colA, RGB32 colB, RGB32 colC, RGB32 colD)
1766 {
1767 return RGB32(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
1768 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
1769 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2,
1770 ((int)colA.A + (int)colB.A + (int)colC.A + (int)colD.A) >> 2);
1771 }
1772
1773
1774
1775
1776
1790struct RGB64
1791 {
1792
1793
1794 // mtools extension (if available).
1795 #if (MTOOLS_TGX_EXTENSIONS)
1796 #include <mtools/extensions/tgx/tgx_ext_Color_RGB64.inl>
1797 #endif
1798
1799
1800 static const uint16_t DEFAULT_A = 65535;
1801
1802
1803 // dual memory representation
1804 union
1805 {
1806 uint64_t val;
1807 struct
1808 {
1809
1810#if TGX_RGB64_ORDER_BGR
1811 uint16_t B;
1812 uint16_t G;
1813 uint16_t R;
1814 uint16_t A;
1815
1816#else
1817 uint16_t R;
1818 uint16_t G;
1819 uint16_t B;
1820 uint16_t A;
1821#endif
1822 };
1823 };
1824
1825
1829 RGB64() = default;
1830
1831
1835 constexpr RGB64(int r, int g, int b, int a = DEFAULT_A) :
1837 B((uint16_t)b), G((uint16_t)g), R((uint16_t)r), A((uint16_t)a)
1838 #else
1839 R((uint16_t)r), G((uint16_t)g), B((uint16_t)b), A((uint16_t)a)
1840 #endif
1841 {}
1842
1843
1847 constexpr RGB64(iVec3 v) : RGB64(v.x, v.y, v.z)
1848 {}
1849
1850
1854 constexpr RGB64(iVec4 v) : RGB64(v.x, v.y, v.z, v.w)
1855 {}
1856
1857
1861 RGB64(float r, float g, float b, float a = -1.0f) :
1863 B((uint16_t)(b * 65535)),
1864 G((uint16_t)(g * 65535)),
1865 R((uint16_t)(r * 65535)),
1866 A((uint16_t)((a < 0.0f) ? DEFAULT_A : a * 65535))
1867 #else
1868 R((uint16_t)(r * 65535)),
1869 G((uint16_t)(g * 65535)),
1870 B((uint16_t)(b * 65535)),
1871 A((uint16_t)((a < 0.0f) ? DEFAULT_A : a * 65535))
1872 #endif
1873 {}
1874
1875
1879 RGB64(fVec3 v) : RGB64(v.x, v.y, v.z)
1880 {}
1881
1882
1886 RGB64(fVec4 v) : RGB64(v.x, v.y, v.z, v.w)
1887 {}
1888
1889
1893 constexpr inline RGB64(uint64_t c) : val(c) {}
1894
1895
1899 inline RGB64(uint16_t val);
1900
1901
1905 inline RGB64(uint32_t val);
1906
1907
1911 RGB64(const RGB64&) = default;
1912
1913
1917 inline RGB64(const RGB565& c);
1918
1919
1923 inline RGB64(const RGB24& c);
1924
1925
1929 inline RGB64(const RGB32& c);
1930
1931
1935 inline RGB64(const RGBf& c);
1936
1937
1941 RGB64(const HSV& c);
1942
1943
1947 explicit operator uint64_t&() { return val; }
1948
1949
1953 explicit operator const uint64_t&() const { return val; }
1954
1955
1959 explicit operator iVec3() const { return iVec3(R, G, B); }
1960
1961
1965 explicit operator fVec3() const { return fVec3(R / 65535.0f, G / 65535.0f, B / 65535.0f); }
1966
1967
1971 explicit operator iVec4() const { return iVec4(R, G, B, A); }
1972
1973
1977 explicit operator fVec4() const { return fVec4(R / 65535.0f, G / 65535.0f, B / 65535.0f, A / 65535.0f); }
1978
1979
1983 RGB64& operator=(const RGB64&) = default;
1984
1985
1989 inline RGB64& operator=(const RGB565& c);
1990
1991
1995 inline RGB64& operator=(const RGB24& c);
1996
1997
2001 inline RGB64& operator=(const RGB32& c);
2002
2003
2007 inline RGB64& operator=(const RGBf& c);
2008
2009
2013 RGB64& operator=(const HSV& c);
2014
2015
2020
2021
2026
2027
2032
2033
2038
2039
2043 void operator+=(const RGB64& c)
2044 {
2045 R += c.R;
2046 G += c.G;
2047 B += c.B;
2048 A += c.A;
2049 }
2050
2051
2055 void operator-=(const RGB64& c)
2056 {
2057 R -= c.R;
2058 G -= c.G;
2059 B -= c.B;
2060 A -= c.A;
2061 }
2062
2063
2067 void operator+=(uint16_t v)
2068 {
2069 R += v;
2070 G += v;
2071 B += v;
2072 A += v;
2073 }
2074
2075
2079 void operator-=(uint16_t v)
2080 {
2081 R -= v;
2082 G -= v;
2083 B -= v;
2084 A -= v;
2085 }
2086
2087
2091 void operator*=(uint16_t v)
2092 {
2093 R *= v;
2094 G *= v;
2095 B *= v;
2096 A *= v;
2097 }
2098
2099
2103 void operator*=(float v)
2104 {
2105 R = (uint16_t)(R * v);
2106 G = (uint16_t)(G * v);
2107 B = (uint16_t)(B * v);
2108 A = (uint16_t)(A * v);
2109 }
2110
2114 void operator/=(uint16_t v)
2115 {
2116 R /= v;
2117 G /= v;
2118 B /= v;
2119 A /= v;
2120 }
2121
2125 void operator/=(float v)
2126 {
2127 R = (uint16_t)(R / v);
2128 G = (uint16_t)(G / v);
2129 B = (uint16_t)(B / v);
2130 A = (uint16_t)(A / v);
2131 }
2132
2133
2137 constexpr bool operator==(const RGB64& c) const
2138 {
2139 return(val == c.val);
2140 }
2141
2142
2146 constexpr bool operator!=(const RGB64& c) const
2147 {
2148 return !(*this == c);
2149 }
2150
2151
2161 inline void blend(const RGB64& fg_col, float alpha)
2162 {
2163 blend65536(fg_col, (uint32_t)(alpha * 65536));
2164 }
2165
2166
2176 inline void blend256(const RGB64 & fg_col, uint32_t alpha)
2177 {
2178 blend65536(fg_col, (alpha << 8));
2179 }
2180
2181
2191 inline void blend65536(RGB64 fg_col, uint32_t alpha)
2192 {
2193 // correct version where 'alpha' is interpolated normally but 'A' is only multiplied
2194 // with background because the color is assumed pre-multiplied
2195 alpha >>= 1; // alpha in [0,32768]
2196 const uint32_t inv_alpha = (2147483648 - (alpha * (((uint32_t)fg_col.A) + (fg_col.A > 32767)))) >> 16;
2197 R = (uint16_t)((((uint32_t)fg_col.R) * alpha + ((uint32_t)R) * inv_alpha) >> 15);
2198 G = (uint16_t)((((uint32_t)fg_col.G) * alpha + ((uint32_t)G) * inv_alpha) >> 15);
2199 B = (uint16_t)((((uint32_t)fg_col.B) * alpha + ((uint32_t)B) * inv_alpha) >> 15);
2200 A = (uint16_t)((((uint32_t)fg_col.A) * alpha + ((uint32_t)A) * inv_alpha) >> 15);
2201 }
2202
2203
2211 inline void blend(const RGB64 & fg_col)
2212 {
2213 blend65536(fg_col, 65536);
2214 }
2215
2216
2220 inline void mult256(int mr, int mg, int mb)
2221 {
2222 R = (R * mr) >> 8;
2223 G = (G * mg) >> 8;
2224 B = (B * mb) >> 8;
2225 }
2226
2227
2231 inline void mult256(int mr, int mg, int mb, int ma)
2232 {
2233 R = (R * mr) >> 8;
2234 G = (G * mg) >> 8;
2235 B = (B * mb) >> 8;
2236 A = (A * mb) >> 8;
2237 }
2238
2239
2247 inline void premultiply()
2248 {
2249 R = (uint16_t)((((uint32_t)R) * A) / 65535);
2250 G = (uint16_t)((((uint32_t)G) * A) / 65535);
2251 B = (uint16_t)((((uint32_t)B) * A) / 65535);
2252 }
2253
2254
2259 float opacity() const
2260 {
2261 return (((float)A) / 65535.0f);
2262 }
2263
2264
2272 void setOpacity(float op)
2273 {
2274 // slow version
2275 float mo = op * 65535.0f;
2276 float mult = (A == 0) ? 0.0f : (mo / ((float)A));
2277 (*this) = RGB64((int)(R * mult), (int)(G * mult), (int)(B * mult), (int)mo);
2278 }
2279
2280
2286 void multOpacity(float op)
2287 {
2288 *this = getMultOpacity(op);
2289 }
2290
2291
2297 RGB64 getMultOpacity(float op) const
2298 {
2299 return RGB64((int)(R * op), (int)(G * op), (int)(B * op), (int)(A * op));
2300 }
2301
2302
2307 {
2308 setOpacity(1.0f);
2309 }
2310
2311
2316 {
2317 R = 0;
2318 G = 0;
2319 B = 0;
2320 A = 0;
2321 }
2322
2323
2324 };
2325
2326
2327
2328
2334 inline RGB64 interpolateColorsTriangle(const RGB64& col1, int32_t C1, const RGB64& col2, int32_t C2, const RGB64& col3, const int32_t totC)
2335 {
2336 // forward to RGB32 (, maybe improve this but is it worth it, the method is never used ?)
2337 return RGB64(interpolateColorsTriangle(RGB32(col1), C1, RGB32(col2), C2, RGB32(col3), totC));
2338 }
2339
2340
2357 inline RGB64 interpolateColorsBilinear(const RGB64 & C00, const RGB64 & C10, const RGB64 & C01, const RGB64 & C11, const float ax, const float ay)
2358 {
2359 // let's use floating point version for max accuraccy, RGB64 is slow anyway...
2360 const float rax = 1.0f - ax;
2361 const float ray = 1.0f - ay;
2362 const int R = (int)roundf(rax*(ray*C00.R + ay*C01.R) + ax*(ray*C10.R + ay*C11.R));
2363 const int G = (int)roundf(rax*(ray*C00.G + ay*C01.G) + ax*(ray*C10.G + ay*C11.G));
2364 const int B = (int)roundf(rax*(ray*C00.B + ay*C01.B) + ax*(ray*C10.B + ay*C11.B));
2365 const int A = (int)roundf(rax*(ray*C00.A + ay*C01.A) + ax*(ray*C10.A + ay*C11.A));
2366 return RGB64(R,G,B,A);
2367 }
2368
2369
2373 inline RGB64 meanColor(const RGB64 & colA, const RGB64 & colB)
2374 {
2375 return RGB64(((int)colA.R + (int)colB.R) >> 1,
2376 ((int)colA.G + (int)colB.G) >> 1,
2377 ((int)colA.B + (int)colB.B) >> 1,
2378 ((int)colA.A + (int)colB.A) >> 1);
2379 }
2380
2381
2385 inline RGB64 meanColor(const RGB64& colA, const RGB64& colB, const RGB64& colC, const RGB64& colD)
2386 {
2387 return RGB64(((int)colA.R + (int)colB.R + (int)colC.R + (int)colD.R) >> 2,
2388 ((int)colA.G + (int)colB.G + (int)colC.G + (int)colD.G) >> 2,
2389 ((int)colA.B + (int)colB.B + (int)colC.B + (int)colD.B) >> 2,
2390 ((int)colA.A + (int)colB.A + (int)colC.A + (int)colD.A) >> 2);
2391 }
2392
2393
2394
2395
2396
2406 struct RGBf
2407 {
2408
2409 // mtools extension (if available).
2410 #if (MTOOLS_TGX_EXTENSIONS)
2411 #include <mtools/extensions/tgx/tgx_ext_Color_RGBf.inl>
2412 #endif
2413
2414
2415#if TGX_RGBf_ORDER_BGR
2416 float B;
2417 float G;
2418 float R;
2419#else
2420 float R;
2421 float G;
2422 float B;
2423#endif
2424
2425
2429 RGBf() = default;
2430
2431
2435 constexpr RGBf(float r, float g, float b) :
2437 B(b), G(g), R(r)
2438 #else
2439 R(r), G(g), B(b)
2440 #endif
2441 {}
2442
2443
2447 constexpr RGBf(fVec3 v) : RGBf(v.x, v.y, v.z)
2448 {}
2449
2450
2454 constexpr RGBf(fVec4 v) : RGBf(v.x, v.y, v.z)
2455 {}
2456
2457
2461 constexpr inline RGBf(uint16_t val);
2462
2463
2467 constexpr inline RGBf(uint32_t val);
2468
2469
2473 constexpr inline RGBf(uint64_t val);
2474
2475
2479 constexpr RGBf(const RGBf&) = default;
2480
2481
2485 constexpr inline RGBf(const RGB565& c);
2486
2487
2491 constexpr inline RGBf(const RGB24& c);
2492
2493
2497 constexpr inline RGBf(const RGB32& c);
2498
2499
2503 constexpr inline RGBf(const RGB64& c);
2504
2505
2509 RGBf(const HSV& c);
2510
2511
2515 explicit operator fVec3() const { return fVec3(R, G, B); }
2516
2517
2521 RGBf& operator=(const RGBf&) = default;
2522
2523
2527 inline RGBf& operator=(const RGB565& c);
2528
2529
2533 inline RGBf& operator=(const RGB24& c);
2534
2535
2539 inline RGBf& operator=(const RGB32& c);
2540
2541
2545 inline RGBf& operator=(const RGB64& c);
2546
2547
2551 RGBf& operator=(const HSV& c);
2552
2553
2558
2559
2564
2565
2569 void operator+=(const RGBf& c)
2570 {
2571 R += c.R;
2572 G += c.G;
2573 B += c.B;
2574 }
2575
2576
2580 void operator-=(const RGBf& c)
2581 {
2582 R -= c.R;
2583 G -= c.G;
2584 B -= c.B;
2585 }
2586
2587
2591 inline void operator*=(const RGBf & c)
2592 {
2593 R *= c.R;
2594 G *= c.G;
2595 B *= c.B;
2596 }
2597
2601 inline RGBf operator*(const RGBf & c) const
2602 {
2603 return RGBf(R * c.R, G * c.G, B * c.B);
2604 }
2605
2609 inline void operator*=(float a)
2610 {
2611 R *= a;
2612 G *= a;
2613 B *= a;
2614 }
2615
2616
2620 inline RGBf operator*(float a) const
2621 {
2622 return RGBf(R * a, G * a, B * a);
2623 }
2624
2625
2629 constexpr bool operator==(const RGBf & c) const
2630 {
2631 return((R == c.R)&&(G == c.G)&&(B == c.B));
2632 }
2633
2634
2638 constexpr bool operator!=(const RGBf & c) const
2639 {
2640 return !(*this == c);
2641 }
2642
2643
2647 inline void clamp()
2648 {
2649 R = tgx::clamp(R, 0.0f, 1.0f);
2650 G = tgx::clamp(G, 0.0f, 1.0f);
2651 B = tgx::clamp(B, 0.0f, 1.0f);
2652 }
2653
2654
2662 inline void blend256(const RGB64& fg_col, uint32_t alpha)
2663 {
2664 blend(fg_col, alpha / 256.0f);
2665 }
2666
2667
2675 inline void blend(const RGBf & fg_col, float alpha)
2676 {
2677 R += (fg_col.R - R) * alpha;
2678 G += (fg_col.G - G) * alpha;
2679 B += (fg_col.B - B) * alpha;
2680 }
2681
2682
2686 inline void mult256(int mr, int mg, int mb)
2687 {
2688 R = (R * mr)/256;
2689 G = (G * mg)/256;
2690 B = (B * mb)/256;
2691 }
2692
2693
2698 inline void mult256(int mr, int mg, int mb, int ma)
2699 {
2700 mult256(mr, mg, mb);
2701 }
2702
2703
2709 inline void premultiply()
2710 {
2711 // nothing here.
2712 return;
2713 }
2714
2715
2721 float opacity() const
2722 {
2723 return 1.0f;
2724 }
2725
2726
2732 void setOpacity(float op)
2733 {
2734 // nothing here.
2735 return;
2736 }
2737
2738
2739 };
2740
2741
2742
2748 inline RGBf interpolate(const RGBf& col1, const RGBf& col2, float alpha)
2749 {
2750 return RGBf(col1.R + alpha * (col2.R - col1.R),
2751 col1.G + alpha * (col2.G - col1.G),
2752 col1.B + alpha * (col2.B - col1.B));
2753 }
2754
2755
2761 inline RGBf interpolateColorsTriangle(const RGBf & col1, int32_t C1, const RGBf & col2, int32_t C2, const RGBf & col3, int32_t totC)
2762 {
2763 return RGBf(col3.R + (C1 * (col1.R - col3.R) + C2 * (col2.R - col3.R)) / totC,
2764 col3.G + (C1 * (col1.G - col3.G) + C2 * (col2.G - col3.G)) / totC,
2765 col3.B + (C1 * (col1.B - col3.B) + C2 * (col2.B - col3.B)) / totC);
2766 }
2767
2768
2785 inline RGBf interpolateColorsBilinear(const RGBf & C00, const RGBf & C10, const RGBf & C01, const RGBf & C11, const float ax, const float ay)
2786 {
2787 const float rax = 1.0f - ax;
2788 const float ray = 1.0f - ay;
2789 const float R = rax*(ray*C00.R + ay*C01.R) + ax*(ray*C10.R + ay*C11.R);
2790 const float G = rax*(ray*C00.G + ay*C01.G) + ax*(ray*C10.G + ay*C11.G);
2791 const float B = rax*(ray*C00.B + ay*C01.B) + ax*(ray*C10.B + ay*C11.B);
2792 return RGBf(R,G,B);
2793 }
2794
2795
2799 inline RGBf meanColor(const RGBf & colA, const RGBf & colB)
2800 {
2801 return RGBf((colA.R + colB.R)/2, (colA.G + colB.G)/2, (colA.B + colB.B)/2);
2802 }
2803
2804
2808 inline RGBf meanColor(const RGBf & colA, const RGBf & colB, const RGBf & colC, const RGBf & colD)
2809 {
2810 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);
2811 }
2812
2813
2814
2815
2816
2828struct HSV
2829 {
2830
2831 // mtools extension (if available).
2832 #if (MTOOLS_TGX_EXTENSIONS)
2833 #include <mtools/extensions/tgx/tgx_ext_Color_HSV.inl>
2834 #endif
2835
2836 // color components
2837 float H;
2838 float S;
2839 float V;
2840
2841
2845 HSV() = default;
2846
2847
2851 constexpr HSV(float h, float s, float v) : H(h), S(s), V(v) {}
2852
2853
2857 constexpr HSV(fVec3 v) : HSV(v.x, v.y, v.z)
2858 {}
2859
2860
2864 constexpr HSV(fVec4 v) : HSV(v.x, v.y, v.z)
2865 {}
2866
2867
2871 HSV(const HSV&) = default;
2872
2873
2877 HSV(uint16_t val);
2878
2879
2883 HSV(uint32_t val);
2884
2885
2889 HSV(uint64_t val);
2890
2891
2895 HSV(const RGB565& c);
2896
2897
2901 HSV(const RGB24 & c);
2902
2903
2907 HSV(const RGB32 & c);
2908
2909
2913 HSV(const RGB64 & c);
2914
2915
2919 HSV(const RGBf& c);
2920
2921
2925 explicit operator fVec3() const { return fVec3(H, S, V); }
2926
2927
2931 HSV& operator=(const HSV&) = default;
2932
2933
2938
2939
2943 HSV& operator=(const RGB24 & c);
2944
2945
2949 HSV& operator=(const RGB32 & c);
2950
2951
2955 HSV& operator=(const RGB64 & c);
2956
2957
2961 HSV& operator=(const RGBf& c);
2962
2963
2968 {
2969 H = v.x;
2970 S = v.y;
2971 V = v.z;
2972 return *this;
2973 }
2974
2975
2980 {
2981 H = v.x;
2982 S = v.y;
2983 V = v.z;
2984 return *this;
2985 }
2986
2987
2991 constexpr bool operator==(const HSV & c) const
2992 {
2993 return((H == c.H)&&(S == c.S)&&(V == c.V));
2994 }
2995
2996
3000 constexpr bool operator!=(const HSV & c) const
3001 {
3002 return !(*this == c);
3003 }
3004
3005
3016 inline void blend(const HSV & fg_col, float alpha)
3017 {
3018 // is there a natural blending formula in HSV space ?
3019 // let us do it in RGB space for the time being
3020 RGBf c(*this);
3021 c.blend(RGBf(fg_col), alpha);
3022 *this = HSV(c);
3023 }
3024
3025
3036 inline void blend256(const HSV & fg_col, uint32_t alpha)
3037 {
3038 blend(fg_col, (alpha / 256.0f));
3039 }
3040
3041
3048 inline void mult256(int mr, int mg, int mb)
3049 {
3050 RGBf c = RGBf(*this);
3051 c.mult256(mr, mg, mb);
3052 *this = HSV(c);
3053 }
3054
3055
3063 inline void mult256(int mr, int mg, int mb, int ma)
3064 {
3065 mult256(mr, mg, mb);
3066 }
3067
3068
3074 inline void premultiply()
3075 {
3076 // nothing here.
3077 return;
3078 }
3079
3080
3086 float opacity() const
3087 {
3088 return 1.0f;
3089 }
3090
3091
3097 void setOpacity(float op)
3098 {
3099 // nothing here.
3100 return;
3101 }
3102
3103 };
3104
3105
3106
3112 inline HSV interpolateColorsTriangle(HSV col1, int32_t C1, HSV col2, int32_t C2, HSV col3, int32_t totC)
3113 {
3114 return HSV(interpolateColorsTriangle(RGBf(col1), C1, RGBf(col2), C2, RGBf(col3), totC));
3115 }
3116
3117
3134 inline HSV interpolateColorsBilinear(const HSV & C00, const HSV & C10, const HSV & C01, const HSV & C11, const float ax, const float ay)
3135 {
3136 // just forward to RGBf ..
3137 return HSV(interpolateColorsBilinear(RGBf(C00), RGBf(C10), RGBf(C01), RGBf(C11), ax, ay));
3138 }
3139
3140
3144 inline HSV meanColor(const HSV & colA, const HSV & colB)
3145 {
3146 // hum.. what does that mean in HSV, let's do it in RGB color space instead
3147 return HSV(meanColor(RGBf(colA), RGBf(colB)));
3148 }
3149
3150
3154 inline HSV meanColor(const HSV& colA, const HSV& colB, const HSV& colC, const HSV& colD)
3155 {
3156 // hum.. what does that mean in HSV, let's do it in RGB color space instead
3157 return HSV(meanColor(RGBf(colA), RGBf(colB), RGBf(colC), RGBf(colD)));
3158 }
3159
3160
3161}
3162
3163
3164
3165#include "Color.inl"
3166
3167
3168#endif
3169
3170#endif
3171
3172
3173/* end of file */
3174
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.
RGBf interpolate(const RGBf &col1, const RGBf &col2, float alpha)
Interpolate between 2 colors.
Definition: Color.h:2748
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
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.
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.
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_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.
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 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(const T &v, const T &vmin, const T &vmax)
Template clamp version.
Definition: Misc.h:188
3D vector.
4D vector.
Color in H/S/V format [experimental].
Definition: Color.h:2829
constexpr HSV(fVec3 v)
Constructor from a fVec3 with components (x=H, y=S, z=V) in [0.0f, 1.0f].
Definition: Color.h:2857
float S
Saturation in [0;0f, 1.0f].
Definition: Color.h:2838
HSV & operator=(const RGB64 &c)
Assignement operator from a RGB64 color.
HSV & operator=(const HSV &)=default
Default assignement operator.
HSV(const HSV &)=default
Default Copy constructor.
HSV & operator=(fVec3 v)
Assignement operator from a fVec3 with components (x=H, y=S, z=V) in [0.0f, 1.0f].
Definition: Color.h:2967
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:3016
float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:3086
HSV(uint64_t val)
Constructor from a uint64_t (seen as RGB64, component A is ignored).
HSV & operator=(fVec4 v)
Assignement operator from a fVec4 with components (x=H, y=S, z=V, w=ignored) in [0....
Definition: Color.h:2979
float V
Value in [0.F ,1.0f].
Definition: Color.h:2839
HSV(const RGBf &c)
Constructor from a RGBf color.
float H
Hue in [0.0f ,1.0f].
Definition: Color.h:2837
HSV(const RGB32 &c)
Constructor from a RGB32 color.
HSV & operator=(const RGB24 &c)
Assignement 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:3097
HSV(const RGB64 &c)
Constructor from a RGB64 color.
HSV & operator=(const RGB565 &c)
Assignement 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:2864
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:3036
constexpr bool operator!=(const HSV &c) const
Inequality comparator.
Definition: Color.h:3000
constexpr bool operator==(const HSV &c) const
Equality comparator.
Definition: Color.h:2991
HSV & operator=(const RGBf &c)
Assignement 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:3063
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:2851
void premultiply()
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:3074
HSV & operator=(const RGB32 &c)
Assignement 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:3048
Color in R8/G8/B8 format.
Definition: Color.h:681
RGB24()=default
Default contructor.
float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:1073
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:1024
void operator+=(const RGB24 &c)
Add another color, component by component.
Definition: Color.h:903
constexpr RGB24(const RGB64 &c)
Constructor from a RGB64 color.
uint8_t R
Red channel (8bits)
Definition: Color.h:698
RGB24 & operator=(const RGB64 &c)
Assignement 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:1037
constexpr RGB24(int r, int g, int b)
Constructor from raw r,g,b values in [0,255].
Definition: Color.h:714
constexpr RGB24(const RGB565 &c)
Constructor from a RGB24 color.
RGB24 & operator=(const HSV &c)
Assignement 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:741
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:958
constexpr RGB24(const RGBf &c)
Constructor from a RGBf color.
RGB24 & operator=(const RGB32 &c)
Assignement 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:1061
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:1050
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:727
RGB24 & operator=(iVec3 v)
Assignement 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:764
void operator-=(const RGB24 &c)
Substract another color, component by component.
Definition: Color.h:914
constexpr bool operator!=(const RGB24 &c) const
Inequality comparator.
Definition: Color.h:998
uint8_t B
Blue channel (8bits)
Definition: Color.h:700
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:1011
RGB24 & operator=(const RGBf &c)
Assignement 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:734
RGB24 & operator=(iVec4 v)
Assignement 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:936
void operator*=(uint8_t v)
Multiply each component by the scalar value v.
Definition: Color.h:947
uint8_t G
Green channel (8bits)
Definition: Color.h:699
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:968
void operator+=(uint8_t v)
Add the scalar value v to each component.
Definition: Color.h:925
RGB24(fVec3 v)
Constructor from a fVec3 with component (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:757
void setOpacity(float op)
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:1084
constexpr RGB24(uint8_t *p)
Constructor from a uint8_t pointer to 3 bytes in the following order: R,G,B.
Definition: Color.h:771
RGB24 & operator=(const RGB24 &)=default
Default assignement operator.
RGB24 & operator=(fVec4 v)
Assignement 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:989
RGB24 & operator=(fVec3 v)
Assignement operator from a fVec3 vector (x=R, y=G, z=B).
RGB24 & operator=(const RGB565 &c)
Assignement operator from a RGB565 color.
void operator/=(float v)
Divide each component by the scalar (floating point value) v.
Definition: Color.h:978
Color in R8/G8/B8/A8 format.
Definition: Color.h:1176
uint32_t val
color as uint32_t
Definition: Color.h:1190
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:1185
constexpr RGB32(iVec4 v)
Constructor from a fVec4 with components (x=R, y=G, z=B, w=A) in [0,255].
Definition: Color.h:1238
RGB32 & operator=(const RGB24 &c)
Assignement operator from a RGB24 color.
constexpr RGB32(uint32_t c)
Constructor from a uint32_t.
Definition: Color.h:1278
void premultiply()
Convert the color from plain alpha to pre-multiplied alpha.
Definition: Color.h:1617
void setOpaque()
Set the alpha channel of the color to fully opaque.
Definition: Color.h:1684
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:1590
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:1667
RGB32 & operator=(fVec3 v)
Assignement 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:1693
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:1464
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:1601
constexpr RGB32(int r, int g, int b, int a=DEFAULT_A)
Constructor from raw r,g,b,a values.
Definition: Color.h:1218
void operator*=(float v)
Multiply each component by the scalar (floating point) value v (including alpha channel).
Definition: Color.h:1488
RGB32 & operator=(iVec3 v)
Assignement operator from a vector (x=R, y=G, z=B).
RGB32 & operator=(const RGB32 &)=default
Default assignement operator.
constexpr RGB32(iVec3 v)
Constructor from a fVec3 with components (x=R, y=G, z=B) in [0,255].
Definition: Color.h:1231
void operator+=(uint8_t v)
Add the scalar value v to each component (including alpha channel).
Definition: Color.h:1452
uint8_t A
Alpha channel (8bits)
Definition: Color.h:1197
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:1264
constexpr RGB32(const RGB565 &c)
Constructor from a RGB565 color.
uint8_t R
Red channel (8bits)
Definition: Color.h:1196
void operator+=(const RGB32 &c)
Add another color, component by component (including alpha channel).
Definition: Color.h:1428
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:1581
void operator-=(const RGB32 &c)
Substract another color, component by component (including alpha channel).
Definition: Color.h:1440
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:1561
void operator/=(uint8_t v)
Divide each component by the scalar value v (including alpha channel).
Definition: Color.h:1499
RGB32 & operator=(const RGB565 &c)
Assignement 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:1271
constexpr bool operator==(const RGB32 &c) const
Equality comparator.
Definition: Color.h:1522
RGB32 & operator=(iVec4 v)
Assignement operator from a vector (x=R, y=G, z=B, w=A).
uint8_t G
Green channel (8bits)
Definition: Color.h:1195
RGB32 & operator=(const HSV &c)
Assignement operator from a HSV color.
RGB32 & operator=(fVec4 v)
Assignement 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:1476
RGB32 & operator=(const RGB64 &c)
Assignement 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:1245
uint8_t B
Blue channel (8bits)
Definition: Color.h:1194
RGB32 & operator=(const RGBf &c)
Assignement 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:1531
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:1546
void multOpacity(float op)
Multiply the opacity of the color by a given factor in [0.0f, 1.0f].
Definition: Color.h:1656
void setOpacity(float op)
Change the opacity of the color to a given value in [0.0f, 1.0f].
Definition: Color.h:1642
float opacity() const
Return the opacity (alpha channel value) of this color in the range [0,1] (0=fully transparent,...
Definition: Color.h:1629
void operator/=(float v)
Divide each component by the scalar (floating point value) v, including the alpha channel.
Definition: Color.h:1510
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)
Assignement 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)
Assignement 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 assignement 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)
Assignement 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)
Assignement operator from a RGB24 color.
RGB565(const HSV &c)
Constructor from a HSV color.
RGB565 & operator=(const HSV &c)
Assignement operator from a HSV color.
uint16_t R
Red channel (5 bits)
Definition: Color.h:233
RGB565 & operator=(const RGBf &c)
Assignement 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)
Assignement 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)
Assignement operator from an iVec3 vector.
RGB565 & operator=(const RGB64 &c)
Assignement 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:1791
uint16_t B
Blue channel (16 bits)
Definition: Color.h:1819
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:2114
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:2103
void operator-=(const RGB64 &c)
Substract another color, component by component, including the alpha channel.
Definition: Color.h:2055
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:2176
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:2161
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:2286
void setOpaque()
Set the alpha channel of the color to fully opaque.
Definition: Color.h:2306
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:2297
void operator+=(const RGB64 &c)
Add another color, component by component, including the alpha channel.
Definition: Color.h:2043
RGB64 & operator=(iVec4 v)
Assignement operator from a iVec4 with components (x=R, y=G, z=B, w=A) in [0,65535].
RGB64 & operator=(fVec3 v)
Assignement 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:1820
RGB64(const RGBf &c)
Constructor from a RGBf color.
RGB64(const HSV &c)
Constructor from a HSV color.
RGB64 & operator=(const HSV &c)
Assignement 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:2259
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:2191
void setOpacity(float op)
Change the opacity of the color to a given value in [0.0f, 1.0f].
Definition: Color.h:2272
RGB64 & operator=(const RGB24 &c)
Assignement operator from a RGB24 color.
uint64_t val
color as uint64_t
Definition: Color.h:1806
RGB64 & operator=(const RGBf &c)
Assignement operator from a RGBf color.
constexpr bool operator==(const RGB64 &c) const
Equality comparator.
Definition: Color.h:2137
static const uint16_t DEFAULT_A
fully opaque alpha value
Definition: Color.h:1800
constexpr bool operator!=(const RGB64 &c) const
Inequality comparator.
Definition: Color.h:2146
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:2211
RGB64 & operator=(const RGB32 &c)
Assignement operator from a RGB32 color.
uint16_t G
Green channel (16 bits)
Definition: Color.h:1818
void operator+=(uint16_t v)
Add the scalar value v to each component, including the alpha channel.
Definition: Color.h:2067
RGB64 & operator=(iVec3 v)
Assignement operator from a iVec3 with components (x=R, y=G, z=B) in [0,65535].
RGB64 & operator=(const RGB565 &c)
Assignement 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:2079
RGB64(fVec3 v)
Constructor from a fVec3 with components (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:1879
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:1886
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:2231
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:2220
constexpr RGB64(iVec3 v)
Constructor from a iVec3 with components (x=R, y=G, z=B) in [0,65535].
Definition: Color.h:1847
RGB64 & operator=(fVec4 v)
Assignement operator from a fVec3 with components (x=R, y=G, z=B, w=A) in [0.0f,1....
RGB64 & operator=(const RGB64 &)=default
Default assignement operator.
void operator/=(float v)
Divide each component by the scalar (floating point value) v, including the alpha channel.
Definition: Color.h:2125
uint16_t R
Red channel (16 bits)
Definition: Color.h:1817
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:1835
void operator*=(uint16_t v)
Multiply each component by the scalar value v, including the alpha channel.
Definition: Color.h:2091
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:1861
constexpr RGB64(iVec4 v)
Constructor from a iVec4 with components (x=R, y=G, z=B, w=A) in [0,65535].
Definition: Color.h:1854
void premultiply()
Convert the color from plain alpha to pre-multiplied alpha.
Definition: Color.h:2247
constexpr RGB64(uint64_t c)
Constructor from a uint64_t.
Definition: Color.h:1893
void setTransparent()
Set the alpha channel of the color to fully transparent.
Definition: Color.h:2315
Color in R,G,B float format.
Definition: Color.h:2407
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:2686
void operator+=(const RGBf &c)
Add another color, component by component.
Definition: Color.h:2569
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:2435
void operator-=(const RGBf &c)
Substract another color, component by component.
Definition: Color.h:2580
float R
Red channel.
Definition: Color.h:2420
constexpr RGBf(fVec4 v)
Constructor from a fVec4 with components (x=R, y=G, z=B, w=ignored) in [0.0f, 1.0f].
Definition: Color.h:2454
constexpr RGBf(const RGB32 &c)
Constructor from a RGB32 color.
void operator*=(const RGBf &c)
Multiply each channel by the same channel on c.
Definition: Color.h:2591
constexpr RGBf(const RGBf &)=default
Default Copy constructor.
void clamp()
Clamp all color channel to [0.0f,1.0f].
Definition: Color.h:2647
RGBf & operator=(fVec3 v)
Assignement operator from a fVec3 with components (x=R, y=G, z=B) in [0.0f, 1.0f].
RGBf & operator=(const RGB24 &c)
Assignement operator from a RGB24 color.
RGBf & operator=(const RGB64 &c)
Assignement operator from a RGB64 color.
RGBf & operator=(const HSV &c)
Assignement operator from a HSV color.
RGBf()=default
Default constructor.
RGBf operator*(const RGBf &c) const
Return the color obtained by multipliying the channels of both colors together.
Definition: Color.h:2601
RGBf & operator=(fVec4 v)
Assignement operator from a fVec4 with components (x=R, y=G, z=B, w=ignored) in [0....
constexpr RGBf(uint32_t val)
Constructor from a uint32_t (seen as RGB32, the A component is ignored).
RGBf & operator=(const RGB565 &c)
Assignement operator from a RGB565 color.
constexpr bool operator!=(const RGBf &c) const
Inequality comparator.
Definition: Color.h:2638
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:2698
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:2675
RGBf & operator=(const RGB32 &c)
Assignement operator from a RGB32 color.
float B
Blue channel.
Definition: Color.h:2422
constexpr RGBf(const RGB64 &c)
Constructor from a RGB64 color.
float opacity() const
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:2721
RGBf & operator=(const RGBf &)=default
Default assignement operator.
float G
Green channel.
Definition: Color.h:2421
void setOpacity(float op)
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:2732
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:2662
RGBf(const HSV &c)
Constructor from a HSV color.
constexpr RGBf(fVec3 v)
Constructor from a fVec3 with components (x=R, y=G, z=B) in [0.0f, 1.0f].
Definition: Color.h:2447
constexpr bool operator==(const RGBf &c) const
Equality comparator.
Definition: Color.h:2629
void premultiply()
Dummy function for compatibility with color types having an alpha channel.
Definition: Color.h:2709
constexpr RGBf(const RGB24 &c)
Constructor from a RGB24 color.
void operator*=(float a)
Multiply each channel by a constant.
Definition: Color.h:2609
RGBf operator*(float a) const
Return the color where all components are multiplied by a.
Definition: Color.h:2620
constexpr RGBf(uint16_t val)
Constructor from a uint16_t (seen as RGB565).
constexpr RGBf(const RGB565 &c)
Constructor from a RGB565 color.
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