Colors.h
Go to the documentation of this file.
1 /****
2  * Colors.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include <WString.h>
25 
26 /*
27  * Basic colour definitions (from Adafruit ILI9341)
28  */
29 #define COLOR_NAME_MAP(XX) \
30  XX(BLACK, 0, 0, 0) \
31  XX(NAVY, 0, 0, 128) \
32  XX(DARKGREEN, 0, 128, 0) \
33  XX(DARKCYAN, 0, 128, 128) \
34  XX(MAROON, 128, 0, 0) \
35  XX(PURPLE, 128, 0, 128) \
36  XX(OLIVE, 128, 128, 0) \
37  XX(LIGHTGREY, 192, 192, 192) \
38  XX(DARKGREY, 128, 128, 128) \
39  XX(BLUE, 0, 0, 255) \
40  XX(GREEN, 0, 255, 0) \
41  XX(CYAN, 0, 255, 255) \
42  XX(RED, 255, 0, 0) \
43  XX(MAGENTA, 255, 0, 255) \
44  XX(YELLOW, 255, 255, 0) \
45  XX(WHITE, 255, 255, 255) \
46  XX(ORANGE, 255, 165, 0) \
47  XX(GREENYELLOW, 173, 255, 47) \
48  XX(PINK, 255, 192, 203)
49 
50 /*
51  * gdipluscolor.h
52  *
53  * GDI+ color
54  *
55  * This file is part of the w32api package.
56  *
57  * Contributors:
58  * Created by Markus Koenig <markus@stber-koenig.de>
59  *
60  * THIS SOFTWARE IS NOT COPYRIGHTED
61  *
62  * This source code is offered for use in the public domain. You may
63  * use, modify or distribute it freely.
64  *
65  * This code is distributed in the hope that it will be useful but
66  * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
67  * DISCLAIMED. This includes but is not limited to warranties of
68  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
69  *
70  */
71 #define GDIPLUS_COLOR_TABLE(XX) \
72  XX(AliceBlue, 0xFFF0F8FFUL) \
73  XX(AntiqueWhite, 0xFFFAEBD7UL) \
74  XX(Aqua, 0xFF00FFFFUL) \
75  XX(Aquamarine, 0xFF7FFFD4UL) \
76  XX(Azure, 0xFFF0FFFFUL) \
77  XX(Beige, 0xFFF5F5DCUL) \
78  XX(Bisque, 0xFFFFE4C4UL) \
79  XX(Black, 0xFF000000UL) \
80  XX(BlanchedAlmond, 0xFFFFEBCDUL) \
81  XX(Blue, 0xFF0000FFUL) \
82  XX(BlueViolet, 0xFF8A2BE2UL) \
83  XX(Brown, 0xFFA52A2AUL) \
84  XX(BurlyWood, 0xFFDEB887UL) \
85  XX(CadetBlue, 0xFF5F9EA0UL) \
86  XX(Chartreuse, 0xFF7FFF00UL) \
87  XX(Chocolate, 0xFFD2691EUL) \
88  XX(Coral, 0xFFFF7F50UL) \
89  XX(CornflowerBlue, 0xFF6495EDUL) \
90  XX(Cornsilk, 0xFFFFF8DCUL) \
91  XX(Crimson, 0xFFDC143CUL) \
92  XX(Cyan, 0xFF00FFFFUL) \
93  XX(DarkBlue, 0xFF00008BUL) \
94  XX(DarkCyan, 0xFF008B8BUL) \
95  XX(DarkGoldenrod, 0xFFB8860BUL) \
96  XX(DarkGray, 0xFFA9A9A9UL) \
97  XX(DarkGreen, 0xFF006400UL) \
98  XX(DarkKhaki, 0xFFBDB76BUL) \
99  XX(DarkMagenta, 0xFF8B008BUL) \
100  XX(DarkOliveGreen, 0xFF556B2FUL) \
101  XX(DarkOrange, 0xFFFF8C00UL) \
102  XX(DarkOrchid, 0xFF9932CCUL) \
103  XX(DarkRed, 0xFF8B0000UL) \
104  XX(DarkSalmon, 0xFFE9967AUL) \
105  XX(DarkSeaGreen, 0xFF8FBC8FUL) \
106  XX(DarkSlateBlue, 0xFF483D8BUL) \
107  XX(DarkSlateGray, 0xFF2F4F4FUL) \
108  XX(DarkTurquoise, 0xFF00CED1UL) \
109  XX(DarkViolet, 0xFF9400D3UL) \
110  XX(DeepPink, 0xFFFF1493UL) \
111  XX(DeepSkyBlue, 0xFF00BFFFUL) \
112  XX(DimGray, 0xFF696969UL) \
113  XX(DodgerBlue, 0xFF1E90FFUL) \
114  XX(Firebrick, 0xFFB22222UL) \
115  XX(FloralWhite, 0xFFFFFAF0UL) \
116  XX(ForestGreen, 0xFF228B22UL) \
117  XX(Fuchsia, 0xFFFF00FFUL) \
118  XX(Gainsboro, 0xFFDCDCDCUL) \
119  XX(GhostWhite, 0xFFF8F8FFUL) \
120  XX(Gold, 0xFFFFD700UL) \
121  XX(Goldenrod, 0xFFDAA520UL) \
122  XX(Gray, 0xFF808080UL) \
123  XX(Green, 0xFF008000UL) \
124  XX(GreenYellow, 0xFFADFF2FUL) \
125  XX(Honeydew, 0xFFF0FFF0UL) \
126  XX(HotPink, 0xFFFF69B4UL) \
127  XX(IndianRed, 0xFFCD5C5CUL) \
128  XX(Indigo, 0xFF4B0082UL) \
129  XX(Ivory, 0xFFFFFFF0UL) \
130  XX(Khaki, 0xFFF0E68CUL) \
131  XX(Lavender, 0xFFE6E6FAUL) \
132  XX(LavenderBlush, 0xFFFFF0F5UL) \
133  XX(LawnGreen, 0xFF7CFC00UL) \
134  XX(LemonChiffon, 0xFFFFFACDUL) \
135  XX(LightBlue, 0xFFADD8E6UL) \
136  XX(LightCoral, 0xFFF08080UL) \
137  XX(LightCyan, 0xFFE0FFFFUL) \
138  XX(LightGoldenrodYellow, 0xFFFAFAD2UL) \
139  XX(LightGray, 0xFFD3D3D3UL) \
140  XX(LightGreen, 0xFF90EE90UL) \
141  XX(LightPink, 0xFFFFB6C1UL) \
142  XX(LightSalmon, 0xFFFFA07AUL) \
143  XX(LightSeaGreen, 0xFF20B2AAUL) \
144  XX(LightSkyBlue, 0xFF87CEFAUL) \
145  XX(LightSlateGray, 0xFF778899UL) \
146  XX(LightSteelBlue, 0xFFB0C4DEUL) \
147  XX(LightYellow, 0xFFFFFFE0UL) \
148  XX(Lime, 0xFF00FF00UL) \
149  XX(LimeGreen, 0xFF32CD32UL) \
150  XX(Linen, 0xFFFAF0E6UL) \
151  XX(Magenta, 0xFFFF00FFUL) \
152  XX(Maroon, 0xFF800000UL) \
153  XX(MediumAquamarine, 0xFF66CDAAUL) \
154  XX(MediumBlue, 0xFF0000CDUL) \
155  XX(MediumOrchid, 0xFFBA55D3UL) \
156  XX(MediumPurple, 0xFF9370DBUL) \
157  XX(MediumSeaGreen, 0xFF3CB371UL) \
158  XX(MediumSlateBlue, 0xFF7B68EEUL) \
159  XX(MediumSpringGreen, 0xFF00FA9AUL) \
160  XX(MediumTurquoise, 0xFF48D1CCUL) \
161  XX(MediumVioletRed, 0xFFC71585UL) \
162  XX(MidnightBlue, 0xFF191970UL) \
163  XX(MintCream, 0xFFF5FFFAUL) \
164  XX(MistyRose, 0xFFFFE4E1UL) \
165  XX(Moccasin, 0xFFFFE4B5UL) \
166  XX(NavajoWhite, 0xFFFFDEADUL) \
167  XX(Navy, 0xFF000080UL) \
168  XX(OldLace, 0xFFFDF5E6UL) \
169  XX(Olive, 0xFF808000UL) \
170  XX(OliveDrab, 0xFF6B8E23UL) \
171  XX(Orange, 0xFFFFA500UL) \
172  XX(OrangeRed, 0xFFFF4500UL) \
173  XX(Orchid, 0xFFDA70D6UL) \
174  XX(PaleGoldenrod, 0xFFEEE8AAUL) \
175  XX(PaleGreen, 0xFF98FB98UL) \
176  XX(PaleTurquoise, 0xFFAFEEEEUL) \
177  XX(PaleVioletRed, 0xFFDB7093UL) \
178  XX(PapayaWhip, 0xFFFFEFD5UL) \
179  XX(PeachPuff, 0xFFFFDAB9UL) \
180  XX(Peru, 0xFFCD853FUL) \
181  XX(Pink, 0xFFFFC0CBUL) \
182  XX(Plum, 0xFFDDA0DDUL) \
183  XX(PowderBlue, 0xFFB0E0E6UL) \
184  XX(Purple, 0xFF800080UL) \
185  XX(Red, 0xFFFF0000UL) \
186  XX(RosyBrown, 0xFFBC8F8FUL) \
187  XX(RoyalBlue, 0xFF4169E1UL) \
188  XX(SaddleBrown, 0xFF8B4513UL) \
189  XX(Salmon, 0xFFFA8072UL) \
190  XX(SandyBrown, 0xFFF4A460UL) \
191  XX(SeaGreen, 0xFF2E8B57UL) \
192  XX(SeaShell, 0xFFFFF5EEUL) \
193  XX(Sienna, 0xFFA0522DUL) \
194  XX(Silver, 0xFFC0C0C0UL) \
195  XX(SkyBlue, 0xFF87CEEBUL) \
196  XX(SlateBlue, 0xFF6A5ACDUL) \
197  XX(SlateGray, 0xFF708090UL) \
198  XX(Snow, 0xFFFFFAFAUL) \
199  XX(SpringGreen, 0xFF00FF7FUL) \
200  XX(SteelBlue, 0xFF4682B4UL) \
201  XX(Tan, 0xFFD2B48CUL) \
202  XX(Teal, 0xFF008080UL) \
203  XX(Thistle, 0xFFD8BFD8UL) \
204  XX(Tomato, 0xFFFF6347UL) \
205  XX(Transparent, 0x00FFFFFFUL) \
206  XX(Turquoise, 0xFF40E0D0UL) \
207  XX(Violet, 0xFFEE82EEUL) \
208  XX(Wheat, 0xFFF5DEB3UL) \
209  XX(White, 0xFFFFFFFFUL) \
210  XX(WhiteSmoke, 0xFFF5F5F5UL) \
211  XX(Yellow, 0xFFFFFF00UL) \
212  XX(YellowGreen, 0xFF9ACD32UL)
213 
214 namespace Graphics
215 {
219 inline constexpr uint32_t getColorValue(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
220 {
221  return (a << 24) | (r << 16) | (g << 8) | b;
222 }
223 
227 enum class Color : uint32_t {
228  None = 0,
229 #define XX(name, r, g, b) name = getColorValue(r, g, b),
231 #undef XX
232 
233 #define XX(name, argb) name = argb,
235 #undef XX
236 };
237 
241 inline constexpr Color makeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
242 {
243  return Color(getColorValue(r, g, b, a));
244 }
245 
246 inline constexpr Color makeColor(uint32_t color, uint8_t alpha = 255)
247 {
248  color = (color & 0xffffff) | (alpha << 24);
249  return Color(color);
250 }
251 
252 inline constexpr Color makeColor(Color color, uint8_t alpha)
253 {
254  return makeColor(uint32_t(color), alpha);
255 }
256 
257 inline constexpr uint8_t getAlpha(Color color)
258 {
259  return uint32_t(color) >> 24;
260 }
261 
262 inline constexpr uint8_t getRed(Color color)
263 {
264  return uint32_t(color) >> 16;
265 }
266 
267 inline constexpr uint8_t getGreen(Color color)
268 {
269  return uint32_t(color) >> 8;
270 }
271 
272 inline constexpr uint8_t getBlue(Color color)
273 {
274  return uint32_t(color);
275 }
276 
277 bool fromString(const char* s, Color& color);
278 
279 inline bool fromString(const String& s, Color& color)
280 {
281  return fromString(s.c_str(), color);
282 }
283 
288 // name, bytes per pixel, bits per pixel, color order, byte order
289 #define PIXEL_FORMAT_MAP(XX) \
290  XX(RGB24, 3, 24, orderRGB, "24-bit RGB") \
291  XX(BGRA32, 4, 32, orderRGB, "32-bit ARGB") \
292  XX(BGR24, 3, 24, orderBGR, "24-bit BGR") \
293  XX(RGB565, 2, 16, orderRGB, "16-bit RGB 5/6/5")
294 
295 enum class PixelFormat : uint8_t {
296  None,
297 #define XX(name, bytes, bpp, colorOrder, desc) name = (bytes - 1) | ((bpp / 2) << 2) | (colorOrder << 7),
299 #undef XX
300 };
301 
304  struct {
305  uint8_t mByteCount : 2;
306  uint8_t mBitsPerPixel : 5;
307  uint8_t mColorOrder : 1;
308  };
309 
310  uint8_t byteCount() const
311  {
312  return mByteCount + 1;
313  }
314 
315  uint8_t bitsPerPixel() const
316  {
317  return mBitsPerPixel * 2;
318  }
319 
321  {
322  return ColorOrder(mColorOrder);
323  }
324 };
325 
326 static_assert(sizeof(PixelFormatStruct) == 1);
327 
331 inline uint8_t getBytesPerPixel(PixelFormat format)
332 {
333  return PixelFormatStruct{format}.byteCount();
334 }
335 
339 struct PackedColor {
340  uint32_t value : 24;
341  uint32_t alpha : 8;
342 };
343 
347 union __attribute__((packed)) PixelBuffer {
350  uint8_t u8[4];
351 
352  // Layout as for Color
353  struct BGRA32 {
354  uint8_t b;
355  uint8_t g;
356  uint8_t r;
357  uint8_t a;
358  };
360 
361  struct RGB24 {
362  uint8_t r;
363  uint8_t g;
364  uint8_t b;
365  };
367 
368  struct BGR24 {
369  uint8_t b;
370  uint8_t g;
371  uint8_t r;
372  };
374 
375  struct RGB565 {
376  uint16_t b : 5;
377  uint16_t g : 6;
378  uint16_t r : 5;
379  };
381 };
382 
390 
391 inline PackedColor pack(Color color, PixelFormat format)
392 {
393  PixelBuffer src{color};
394  return pack(src, format).packed;
395 }
396 
404 
405 inline Color unpack(PackedColor packed, PixelFormat format)
406 {
407  PixelBuffer src{.packed = packed};
408  return unpack(src, format).color;
409 }
410 
411 inline Color unpack(uint32_t packed, PixelFormat format)
412 {
413  return unpack(PixelBuffer{.packed = PackedColor{packed}}, format).color;
414 }
415 
422 size_t writeColor(void* buffer, PackedColor color, PixelFormat format);
423 
424 inline size_t writeColor(void* buffer, Color color, PixelFormat format)
425 {
426  return writeColor(buffer, pack(color, format), format);
427 }
428 
436 size_t writeColor(void* buffer, PackedColor color, PixelFormat format, size_t count);
437 
438 inline size_t writeColor(void* buffer, Color color, PixelFormat format, size_t count)
439 {
440  return writeColor(buffer, pack(color, format), format, count);
441 }
442 
446 size_t convert(const void* srcData, PixelFormat srcFormat, void* dstBuffer, PixelFormat dstFormat, size_t numPixels);
447 
448 } // namespace Graphics
449 
#define GDIPLUS_COLOR_TABLE(XX)
Definition: Colors.h:71
#define PIXEL_FORMAT_MAP(XX)
Definition: Colors.h:289
String toString(Graphics::Color color)
#define COLOR_NAME_MAP(XX)
Definition: Colors.h:29
uint8_t u8
Definition: c_types.h:9
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
Definition: Virtual.h:31
PixelBuffer unpack(PixelBuffer src, PixelFormat format)
Convert packed colour into RGB.
constexpr uint32_t getColorValue(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Obtain 24-bit colour value from red, green and blue components.
Definition: Colors.h:219
constexpr uint8_t getGreen(Color color)
Definition: Colors.h:267
size_t writeColor(void *buffer, PackedColor color, PixelFormat format)
Store a packed colour value into memory.
constexpr uint8_t getBlue(Color color)
Definition: Colors.h:272
PixelBuffer pack(PixelBuffer src, PixelFormat format)
Convert RGB colour into packed format.
constexpr uint8_t getAlpha(Color color)
Definition: Colors.h:257
constexpr Color makeColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Function to create a custom colour.
Definition: Colors.h:241
constexpr uint8_t getRed(Color color)
Definition: Colors.h:262
size_t convert(const void *srcData, PixelFormat srcFormat, void *dstBuffer, PixelFormat dstFormat, size_t numPixels)
Convert block of data from one pixel format to another.
bool fromString(const char *s, Color &color)
ColorOrder
Order refers to colour order within bitstream.
Definition: Colors.h:287
@ orderBGR
Definition: Colors.h:287
@ orderRGB
Definition: Colors.h:287
PixelFormat
Definition: Colors.h:295
XX(name, bytes, bpp, colorOrder, desc)
uint8_t getBytesPerPixel(PixelFormat format)
Get number of bytes required to store a pixel in the given format.
Definition: Colors.h:331
Color
Standard colour definitions.
Definition: Colors.h:227
XX(name, r, g, b)
Colour in device pixel format.
Definition: Colors.h:339
uint32_t value
Definition: Colors.h:340
uint32_t alpha
Definition: Colors.h:341
Definition: Colors.h:368
uint8_t r
Definition: Colors.h:371
uint8_t b
Definition: Colors.h:369
uint8_t g
Definition: Colors.h:370
Definition: Colors.h:353
uint8_t b
Definition: Colors.h:354
uint8_t a
Definition: Colors.h:357
uint8_t r
Definition: Colors.h:356
uint8_t g
Definition: Colors.h:355
Definition: Colors.h:361
uint8_t g
Definition: Colors.h:363
uint8_t b
Definition: Colors.h:364
uint8_t r
Definition: Colors.h:362
Definition: Colors.h:375
uint16_t b
Definition: Colors.h:376
uint16_t r
Definition: Colors.h:378
uint16_t g
Definition: Colors.h:377
Structure used to perform pixel format conversions.
Definition: Colors.h:347
RGB24 rgb24
Definition: Colors.h:366
PackedColor packed
Definition: Colors.h:349
BGRA32 bgra32
Definition: Colors.h:359
RGB565 rgb565
Definition: Colors.h:380
BGR24 bgr24
Definition: Colors.h:373
Color color
Definition: Colors.h:348
Definition: Colors.h:302
uint8_t bitsPerPixel() const
Definition: Colors.h:315
ColorOrder colorOrder() const
Definition: Colors.h:320
uint8_t mBitsPerPixel
Definition: Colors.h:306
PixelFormat format
Definition: Colors.h:303
uint8_t mColorOrder
Definition: Colors.h:307
uint8_t byteCount() const
Definition: Colors.h:310
uint8_t mByteCount
Definition: Colors.h:305