1 /**
2 * @file lv_color.h
3 *
4 */
5
6 #ifndef LV_COLOR_H
7 #define LV_COLOR_H
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 /*********************
14 * INCLUDES
15 *********************/
16 #include "../lv_conf_internal.h"
17 #include "lv_assert.h"
18 #include "lv_math.h"
19 #include "lv_types.h"
20
21 /*Error checking*/
22 #if LV_COLOR_DEPTH == 24
23 #error "LV_COLOR_DEPTH 24 is deprecated. Use LV_COLOR_DEPTH 32 instead (lv_conf.h)"
24 #endif
25
26 #if LV_COLOR_DEPTH != 16 && LV_COLOR_16_SWAP != 0
27 #error "LV_COLOR_16_SWAP requires LV_COLOR_DEPTH == 16. Set it in lv_conf.h"
28 #endif
29
30 #include <stdint.h>
31
32 /*********************
33 * DEFINES
34 *********************/
35 LV_EXPORT_CONST_INT(LV_COLOR_DEPTH);
36 LV_EXPORT_CONST_INT(LV_COLOR_16_SWAP);
37
38 /**
39 * Opacity percentages.
40 */
41 enum {
42 LV_OPA_TRANSP = 0,
43 LV_OPA_0 = 0,
44 LV_OPA_10 = 25,
45 LV_OPA_20 = 51,
46 LV_OPA_30 = 76,
47 LV_OPA_40 = 102,
48 LV_OPA_50 = 127,
49 LV_OPA_60 = 153,
50 LV_OPA_70 = 178,
51 LV_OPA_80 = 204,
52 LV_OPA_90 = 229,
53 LV_OPA_100 = 255,
54 LV_OPA_COVER = 255,
55 };
56
57 #define LV_OPA_MIN 2 /*Opacities below this will be transparent*/
58 #define LV_OPA_MAX 253 /*Opacities above this will fully cover*/
59
60 #if LV_COLOR_DEPTH == 1
61 #define LV_COLOR_SIZE 8
62 #elif LV_COLOR_DEPTH == 8
63 #define LV_COLOR_SIZE 8
64 #elif LV_COLOR_DEPTH == 16
65 #define LV_COLOR_SIZE 16
66 #elif LV_COLOR_DEPTH == 32
67 #define LV_COLOR_SIZE 32
68 #else
69 #error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!"
70 #endif
71
72 #if defined(__cplusplus) && !defined(_LV_COLOR_HAS_MODERN_CPP)
73 /**
74 * MSVC compiler's definition of the __cplusplus indicating 199711L regardless to C++ standard version
75 * see https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-cplusplus
76 * so we use _MSC_VER macro instead of __cplusplus
77 */
78 #ifdef _MSC_VER
79 #if _MSC_VER >= 1900 /*Visual Studio 2015*/
80 #define _LV_COLOR_HAS_MODERN_CPP 1
81 #endif
82 #else
83 #if __cplusplus >= 201103L
84 #define _LV_COLOR_HAS_MODERN_CPP 1
85 #endif
86 #endif
87 #endif /*__cplusplus*/
88
89 #ifndef _LV_COLOR_HAS_MODERN_CPP
90 #define _LV_COLOR_HAS_MODERN_CPP 0
91 #endif
92
93 #if _LV_COLOR_HAS_MODERN_CPP
94 /*Fix msvc compiler error C4576 inside C++ code*/
95 #define _LV_COLOR_MAKE_TYPE_HELPER lv_color_t
96 #else
97 #define _LV_COLOR_MAKE_TYPE_HELPER (lv_color_t)
98 #endif
99
100 /*---------------------------------------
101 * Macros for all existing color depths
102 * to set/get values of the color channels
103 *------------------------------------------*/
104 # define LV_COLOR_SET_R1(c, v) (c).ch.red = (uint8_t)((v) & 0x1)
105 # define LV_COLOR_SET_G1(c, v) (c).ch.green = (uint8_t)((v) & 0x1)
106 # define LV_COLOR_SET_B1(c, v) (c).ch.blue = (uint8_t)((v) & 0x1)
107 # define LV_COLOR_SET_A1(c, v) do {} while(0)
108
109 # define LV_COLOR_GET_R1(c) (c).ch.red
110 # define LV_COLOR_GET_G1(c) (c).ch.green
111 # define LV_COLOR_GET_B1(c) (c).ch.blue
112 # define LV_COLOR_GET_A1(c) 0xFF
113
114 # define _LV_COLOR_ZERO_INITIALIZER1 {0x00}
115 # define LV_COLOR_MAKE1(r8, g8, b8) {(uint8_t)((b8 >> 7) | (g8 >> 7) | (r8 >> 7))}
116
117 # define LV_COLOR_SET_R8(c, v) (c).ch.red = (uint8_t)((v) & 0x7U)
118 # define LV_COLOR_SET_G8(c, v) (c).ch.green = (uint8_t)((v) & 0x7U)
119 # define LV_COLOR_SET_B8(c, v) (c).ch.blue = (uint8_t)((v) & 0x3U)
120 # define LV_COLOR_SET_A8(c, v) do {} while(0)
121
122 # define LV_COLOR_GET_R8(c) (c).ch.red
123 # define LV_COLOR_GET_G8(c) (c).ch.green
124 # define LV_COLOR_GET_B8(c) (c).ch.blue
125 # define LV_COLOR_GET_A8(c) 0xFF
126
127 # define _LV_COLOR_ZERO_INITIALIZER8 {{0x00, 0x00, 0x00}}
128 # define LV_COLOR_MAKE8(r8, g8, b8) {{(uint8_t)((b8 >> 6) & 0x3U), (uint8_t)((g8 >> 5) & 0x7U), (uint8_t)((r8 >> 5) & 0x7U)}}
129
130 # define LV_COLOR_SET_R16(c, v) (c).ch.red = (uint8_t)((v) & 0x1FU)
131 #if LV_COLOR_16_SWAP == 0
132 # define LV_COLOR_SET_G16(c, v) (c).ch.green = (uint8_t)((v) & 0x3FU)
133 #else
134 # define LV_COLOR_SET_G16(c, v) {(c).ch.green_h = (uint8_t)(((v) >> 3) & 0x7); (c).ch.green_l = (uint8_t)((v) & 0x7);}
135 #endif
136 # define LV_COLOR_SET_B16(c, v) (c).ch.blue = (uint8_t)((v) & 0x1FU)
137 # define LV_COLOR_SET_A16(c, v) do {} while(0)
138
139 # define LV_COLOR_GET_R16(c) (c).ch.red
140 #if LV_COLOR_16_SWAP == 0
141 # define LV_COLOR_GET_G16(c) (c).ch.green
142 #else
143 # define LV_COLOR_GET_G16(c) (((c).ch.green_h << 3) + (c).ch.green_l)
144 #endif
145 # define LV_COLOR_GET_B16(c) (c).ch.blue
146 # define LV_COLOR_GET_A16(c) 0xFF
147
148 #if LV_COLOR_16_SWAP == 0
149 # define _LV_COLOR_ZERO_INITIALIZER16 {{0x00, 0x00, 0x00}}
150 # define LV_COLOR_MAKE16(r8, g8, b8) {{(uint8_t)((b8 >> 3) & 0x1FU), (uint8_t)((g8 >> 2) & 0x3FU), (uint8_t)((r8 >> 3) & 0x1FU)}}
151 #else
152 # define _LV_COLOR_ZERO_INITIALIZER16 {{0x00, 0x00, 0x00, 0x00}}
153 # define LV_COLOR_MAKE16(r8, g8, b8) {{(uint8_t)((g8 >> 5) & 0x7U), (uint8_t)((r8 >> 3) & 0x1FU), (uint8_t)((b8 >> 3) & 0x1FU), (uint8_t)((g8 >> 2) & 0x7U)}}
154 #endif
155
156 # define LV_COLOR_SET_R32(c, v) (c).ch.red = (uint8_t)((v) & 0xFF)
157 # define LV_COLOR_SET_G32(c, v) (c).ch.green = (uint8_t)((v) & 0xFF)
158 # define LV_COLOR_SET_B32(c, v) (c).ch.blue = (uint8_t)((v) & 0xFF)
159 # define LV_COLOR_SET_A32(c, v) (c).ch.alpha = (uint8_t)((v) & 0xFF)
160
161 # define LV_COLOR_GET_R32(c) (c).ch.red
162 # define LV_COLOR_GET_G32(c) (c).ch.green
163 # define LV_COLOR_GET_B32(c) (c).ch.blue
164 # define LV_COLOR_GET_A32(c) (c).ch.alpha
165
166 # define _LV_COLOR_ZERO_INITIALIZER32 {{0x00, 0x00, 0x00, 0x00}}
167 # define LV_COLOR_MAKE32(r8, g8, b8) {{b8, g8, r8, 0xff}} /*Fix 0xff alpha*/
168
169 /*---------------------------------------
170 * Macros for the current color depth
171 * to set/get values of the color channels
172 *------------------------------------------*/
173 #define LV_COLOR_SET_R(c, v) LV_CONCAT(LV_COLOR_SET_R, LV_COLOR_DEPTH)(c, v)
174 #define LV_COLOR_SET_G(c, v) LV_CONCAT(LV_COLOR_SET_G, LV_COLOR_DEPTH)(c, v)
175 #define LV_COLOR_SET_B(c, v) LV_CONCAT(LV_COLOR_SET_B, LV_COLOR_DEPTH)(c, v)
176 #define LV_COLOR_SET_A(c, v) LV_CONCAT(LV_COLOR_SET_A, LV_COLOR_DEPTH)(c, v)
177
178 #define LV_COLOR_GET_R(c) LV_CONCAT(LV_COLOR_GET_R, LV_COLOR_DEPTH)(c)
179 #define LV_COLOR_GET_G(c) LV_CONCAT(LV_COLOR_GET_G, LV_COLOR_DEPTH)(c)
180 #define LV_COLOR_GET_B(c) LV_CONCAT(LV_COLOR_GET_B, LV_COLOR_DEPTH)(c)
181 #define LV_COLOR_GET_A(c) LV_CONCAT(LV_COLOR_GET_A, LV_COLOR_DEPTH)(c)
182
183 #define _LV_COLOR_ZERO_INITIALIZER LV_CONCAT(_LV_COLOR_ZERO_INITIALIZER, LV_COLOR_DEPTH)
184 #define LV_COLOR_MAKE(r8, g8, b8) LV_CONCAT(LV_COLOR_MAKE, LV_COLOR_DEPTH)(r8, g8, b8)
185
186 /**********************
187 * TYPEDEFS
188 **********************/
189
190 typedef union {
191 uint8_t full; /*must be declared first to set all bits of byte via initializer list*/
192 union {
193 uint8_t blue : 1;
194 uint8_t green : 1;
195 uint8_t red : 1;
196 } ch;
197 } lv_color1_t;
198
199 typedef union {
200 struct {
201 uint8_t blue : 2;
202 uint8_t green : 3;
203 uint8_t red : 3;
204 } ch;
205 uint8_t full;
206 } lv_color8_t;
207
208 typedef union {
209 struct {
210 #if LV_COLOR_16_SWAP == 0
211 uint16_t blue : 5;
212 uint16_t green : 6;
213 uint16_t red : 5;
214 #else
215 uint16_t green_h : 3;
216 uint16_t red : 5;
217 uint16_t blue : 5;
218 uint16_t green_l : 3;
219 #endif
220 } ch;
221 uint16_t full;
222 } lv_color16_t;
223
224 typedef union {
225 struct {
226 uint8_t blue;
227 uint8_t green;
228 uint8_t red;
229 uint8_t alpha;
230 } ch;
231 uint32_t full;
232 } lv_color32_t;
233
234 typedef LV_CONCAT3(uint, LV_COLOR_SIZE, _t) lv_color_int_t;
235 typedef LV_CONCAT3(lv_color, LV_COLOR_DEPTH, _t) lv_color_t;
236
237 typedef struct {
238 uint16_t h;
239 uint8_t s;
240 uint8_t v;
241 } lv_color_hsv_t;
242
243 //! @cond Doxygen_Suppress
244 /*No idea where the guard is required but else throws warnings in the docs*/
245 typedef uint8_t lv_opa_t;
246 //! @endcond
247
248 struct _lv_color_filter_dsc_t;
249
250 typedef lv_color_t (*lv_color_filter_cb_t)(const struct _lv_color_filter_dsc_t *, lv_color_t, lv_opa_t);
251
252 typedef struct _lv_color_filter_dsc_t {
253 lv_color_filter_cb_t filter_cb;
254 void * user_data;
255 } lv_color_filter_dsc_t;
256
257 typedef enum {
258 LV_PALETTE_RED,
259 LV_PALETTE_PINK,
260 LV_PALETTE_PURPLE,
261 LV_PALETTE_DEEP_PURPLE,
262 LV_PALETTE_INDIGO,
263 LV_PALETTE_BLUE,
264 LV_PALETTE_LIGHT_BLUE,
265 LV_PALETTE_CYAN,
266 LV_PALETTE_TEAL,
267 LV_PALETTE_GREEN,
268 LV_PALETTE_LIGHT_GREEN,
269 LV_PALETTE_LIME,
270 LV_PALETTE_YELLOW,
271 LV_PALETTE_AMBER,
272 LV_PALETTE_ORANGE,
273 LV_PALETTE_DEEP_ORANGE,
274 LV_PALETTE_BROWN,
275 LV_PALETTE_BLUE_GREY,
276 LV_PALETTE_GREY,
277 _LV_PALETTE_LAST,
278 LV_PALETTE_NONE = 0xff,
279 } lv_palette_t;
280
281 /**********************
282 * GLOBAL PROTOTYPES
283 **********************/
284
285 /*In color conversations:
286 * - When converting to bigger color type the LSB weight of 1 LSB is calculated
287 * E.g. 16 bit Red has 5 bits
288 * 8 bit Red has 3 bits
289 * ----------------------
290 * 8 bit red LSB = (2^5 - 1) / (2^3 - 1) = 31 / 7 = 4
291 *
292 * - When calculating to smaller color type simply shift out the LSBs
293 * E.g. 8 bit Red has 3 bits
294 * 16 bit Red has 5 bits
295 * ----------------------
296 * Shift right with 5 - 3 = 2
297 */
lv_color_to1(lv_color_t color)298 static inline uint8_t lv_color_to1(lv_color_t color)
299 {
300 #if LV_COLOR_DEPTH == 1
301 return color.full;
302 #elif LV_COLOR_DEPTH == 8
303 if((LV_COLOR_GET_R(color) & 0x4) || (LV_COLOR_GET_G(color) & 0x4) || (LV_COLOR_GET_B(color) & 0x2)) {
304 return 1;
305 }
306 else {
307 return 0;
308 }
309 #elif LV_COLOR_DEPTH == 16
310 if((LV_COLOR_GET_R(color) & 0x10) || (LV_COLOR_GET_G(color) & 0x20) || (LV_COLOR_GET_B(color) & 0x10)) {
311 return 1;
312 }
313 else {
314 return 0;
315 }
316 #elif LV_COLOR_DEPTH == 32
317 if((LV_COLOR_GET_R(color) & 0x80) || (LV_COLOR_GET_G(color) & 0x80) || (LV_COLOR_GET_B(color) & 0x80)) {
318 return 1;
319 }
320 else {
321 return 0;
322 }
323 #endif
324 }
325
lv_color_to8(lv_color_t color)326 static inline uint8_t lv_color_to8(lv_color_t color)
327 {
328 #if LV_COLOR_DEPTH == 1
329 if(color.full == 0)
330 return 0;
331 else
332 return 0xFF;
333 #elif LV_COLOR_DEPTH == 8
334 return color.full;
335 #elif LV_COLOR_DEPTH == 16
336 lv_color8_t ret;
337 LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 2); /*5 - 3 = 2*/
338 LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 3); /*6 - 3 = 3*/
339 LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 3); /*5 - 2 = 3*/
340 return ret.full;
341 #elif LV_COLOR_DEPTH == 32
342 lv_color8_t ret;
343 LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 5); /*8 - 3 = 5*/
344 LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 5); /*8 - 3 = 5*/
345 LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 6); /*8 - 2 = 6*/
346 return ret.full;
347 #endif
348 }
349
lv_color_to16(lv_color_t color)350 static inline uint16_t lv_color_to16(lv_color_t color)
351 {
352 #if LV_COLOR_DEPTH == 1
353 if(color.full == 0)
354 return 0;
355 else
356 return 0xFFFF;
357 #elif LV_COLOR_DEPTH == 8
358 lv_color16_t ret;
359 LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) * 4); /*(2^5 - 1)/(2^3 - 1) = 31/7 = 4*/
360 LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) * 9); /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
361 LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) * 10); /*(2^5 - 1)/(2^2 - 1) = 31/3 = 10*/
362 return ret.full;
363 #elif LV_COLOR_DEPTH == 16
364 return color.full;
365 #elif LV_COLOR_DEPTH == 32
366 lv_color16_t ret;
367 LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) >> 3); /*8 - 5 = 3*/
368 LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) >> 2); /*8 - 6 = 2*/
369 LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) >> 3); /*8 - 5 = 3*/
370 return ret.full;
371 #endif
372 }
373
lv_color_to32(lv_color_t color)374 static inline uint32_t lv_color_to32(lv_color_t color)
375 {
376 #if LV_COLOR_DEPTH == 1
377 if(color.full == 0)
378 return 0xFF000000;
379 else
380 return 0xFFFFFFFF;
381 #elif LV_COLOR_DEPTH == 8
382 lv_color32_t ret;
383 LV_COLOR_SET_R32(ret, LV_COLOR_GET_R(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
384 LV_COLOR_SET_G32(ret, LV_COLOR_GET_G(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
385 LV_COLOR_SET_B32(ret, LV_COLOR_GET_B(color) * 85); /*(2^8 - 1)/(2^2 - 1) = 255/3 = 85*/
386 LV_COLOR_SET_A32(ret, 0xFF);
387 return ret.full;
388 #elif LV_COLOR_DEPTH == 16
389 /**
390 * The floating point math for conversion is:
391 * valueto = valuefrom * ( (2^bitsto - 1) / (float)(2^bitsfrom - 1) )
392 * The faster integer math for conversion is:
393 * valueto = ( valuefrom * multiplier + adder ) >> divisor
394 * multiplier = FLOOR( ( (2^bitsto - 1) << divisor ) / (float)(2^bitsfrom - 1) )
395 *
396 * Find the first divisor where ( adder >> divisor ) <= 0
397 *
398 * 5-bit to 8-bit: ( 31 * multiplier + adder ) >> divisor = 255
399 * divisor multiplier adder min (0) max (31)
400 * 0 8 7 7 255
401 * 1 16 14 7 255
402 * 2 32 28 7 255
403 * 3 65 25 3 255
404 * 4 131 19 1 255
405 * 5 263 7 0 255
406 *
407 * 6-bit to 8-bit: 255 = ( 63 * multiplier + adder ) >> divisor
408 * divisor multiplier adder min (0) max (63)
409 * 0 4 3 3 255
410 * 1 8 6 3 255
411 * 2 16 12 3 255
412 * 3 32 24 3 255
413 * 4 64 48 3 255
414 * 5 129 33 1 255
415 * 6 259 3 0 255
416 */
417
418 lv_color32_t ret;
419 LV_COLOR_SET_R32(ret, (LV_COLOR_GET_R(color) * 263 + 7) >> 5);
420 LV_COLOR_SET_G32(ret, (LV_COLOR_GET_G(color) * 259 + 3) >> 6);
421 LV_COLOR_SET_B32(ret, (LV_COLOR_GET_B(color) * 263 + 7) >> 5);
422 LV_COLOR_SET_A32(ret, 0xFF);
423 return ret.full;
424 #elif LV_COLOR_DEPTH == 32
425 return color.full;
426 #endif
427 }
428
429 //! @cond Doxygen_Suppress
430
431 /**
432 * Mix two colors with a given ratio.
433 * @param c1 the first color to mix (usually the foreground)
434 * @param c2 the second color to mix (usually the background)
435 * @param mix The ratio of the colors. 0: full `c2`, 255: full `c1`, 127: half `c1` and half`c2`
436 * @return the mixed color
437 */
lv_color_mix(lv_color_t c1,lv_color_t c2,uint8_t mix)438 static inline lv_color_t LV_ATTRIBUTE_FAST_MEM lv_color_mix(lv_color_t c1, lv_color_t c2, uint8_t mix)
439 {
440 lv_color_t ret;
441
442 #if LV_COLOR_DEPTH == 16 && LV_COLOR_MIX_ROUND_OFS == 0
443 #if LV_COLOR_16_SWAP == 1
444 c1.full = c1.full << 8 | c1.full >> 8;
445 c2.full = c2.full << 8 | c2.full >> 8;
446 #endif
447 /*Source: https://stackoverflow.com/a/50012418/1999969*/
448 mix = (uint32_t)((uint32_t)mix + 4) >> 3;
449 uint32_t bg = (uint32_t)((uint32_t)c2.full | ((uint32_t)c2.full << 16)) &
450 0x7E0F81F; /*0b00000111111000001111100000011111*/
451 uint32_t fg = (uint32_t)((uint32_t)c1.full | ((uint32_t)c1.full << 16)) & 0x7E0F81F;
452 uint32_t result = ((((fg - bg) * mix) >> 5) + bg) & 0x7E0F81F;
453 ret.full = (uint16_t)((result >> 16) | result);
454 #if LV_COLOR_16_SWAP == 1
455 ret.full = ret.full << 8 | ret.full >> 8;
456 #endif
457 #elif LV_COLOR_DEPTH != 1
458 /*LV_COLOR_DEPTH == 8, 16 or 32*/
459 LV_COLOR_SET_R(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_R(c1) * mix + LV_COLOR_GET_R(c2) *
460 (255 - mix) + LV_COLOR_MIX_ROUND_OFS));
461 LV_COLOR_SET_G(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_G(c1) * mix + LV_COLOR_GET_G(c2) *
462 (255 - mix) + LV_COLOR_MIX_ROUND_OFS));
463 LV_COLOR_SET_B(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_B(c1) * mix + LV_COLOR_GET_B(c2) *
464 (255 - mix) + LV_COLOR_MIX_ROUND_OFS));
465 LV_COLOR_SET_A(ret, 0xFF);
466 #else
467 /*LV_COLOR_DEPTH == 1*/
468 ret.full = mix > LV_OPA_50 ? c1.full : c2.full;
469 #endif
470
471 return ret;
472 }
473
lv_color_premult(lv_color_t c,uint8_t mix,uint16_t * out)474 static inline void LV_ATTRIBUTE_FAST_MEM lv_color_premult(lv_color_t c, uint8_t mix, uint16_t * out)
475 {
476 #if LV_COLOR_DEPTH != 1
477 out[0] = (uint16_t)LV_COLOR_GET_R(c) * mix;
478 out[1] = (uint16_t)LV_COLOR_GET_G(c) * mix;
479 out[2] = (uint16_t)LV_COLOR_GET_B(c) * mix;
480 #else
481 (void) mix;
482 /*Pre-multiplication can't be used with 1 bpp*/
483 out[0] = LV_COLOR_GET_R(c);
484 out[1] = LV_COLOR_GET_G(c);
485 out[2] = LV_COLOR_GET_B(c);
486 #endif
487
488 }
489
490 /**
491 * Mix two colors with a given ratio. It runs faster then `lv_color_mix` but requires some pre computation.
492 * @param premult_c1 The first color. Should be preprocessed with `lv_color_premult(c1)`
493 * @param c2 The second color. As it is no pre computation required on it
494 * @param mix The ratio of the colors. 0: full `c1`, 255: full `c2`, 127: half `c1` and half `c2`.
495 * Should be modified like mix = `255 - mix`
496 * @return the mixed color
497 * @note 255 won't give clearly `c1`.
498 */
lv_color_mix_premult(uint16_t * premult_c1,lv_color_t c2,uint8_t mix)499 static inline lv_color_t LV_ATTRIBUTE_FAST_MEM lv_color_mix_premult(uint16_t * premult_c1, lv_color_t c2, uint8_t mix)
500 {
501 lv_color_t ret;
502 #if LV_COLOR_DEPTH != 1
503 /*LV_COLOR_DEPTH == 8 or 32*/
504 LV_COLOR_SET_R(ret, LV_UDIV255(premult_c1[0] + LV_COLOR_GET_R(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
505 LV_COLOR_SET_G(ret, LV_UDIV255(premult_c1[1] + LV_COLOR_GET_G(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
506 LV_COLOR_SET_B(ret, LV_UDIV255(premult_c1[2] + LV_COLOR_GET_B(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
507 LV_COLOR_SET_A(ret, 0xFF);
508 #else
509 /*LV_COLOR_DEPTH == 1*/
510 /*Restore color1*/
511 lv_color_t c1;
512 LV_COLOR_SET_R(c1, premult_c1[0]);
513 LV_COLOR_SET_G(c1, premult_c1[1]);
514 LV_COLOR_SET_B(c1, premult_c1[2]);
515 ret.full = mix > LV_OPA_50 ? c2.full : c1.full;
516 #endif
517
518 return ret;
519 }
520
521 /**
522 * Mix two colors. Both color can have alpha value.
523 * @param bg_color background color
524 * @param bg_opa alpha of the background color
525 * @param fg_color foreground color
526 * @param fg_opa alpha of the foreground color
527 * @param res_color the result color
528 * @param res_opa the result opacity
529 */
lv_color_mix_with_alpha(lv_color_t bg_color,lv_opa_t bg_opa,lv_color_t fg_color,lv_opa_t fg_opa,lv_color_t * res_color,lv_opa_t * res_opa)530 static inline void LV_ATTRIBUTE_FAST_MEM lv_color_mix_with_alpha(lv_color_t bg_color, lv_opa_t bg_opa,
531 lv_color_t fg_color, lv_opa_t fg_opa,
532 lv_color_t * res_color, lv_opa_t * res_opa)
533 {
534 /*Pick the foreground if it's fully opaque or the Background is fully transparent*/
535 if(fg_opa >= LV_OPA_MAX || bg_opa <= LV_OPA_MIN) {
536 res_color->full = fg_color.full;
537 *res_opa = fg_opa;
538 }
539 /*Transparent foreground: use the Background*/
540 else if(fg_opa <= LV_OPA_MIN) {
541 res_color->full = bg_color.full;
542 *res_opa = bg_opa;
543 }
544 /*Opaque background: use simple mix*/
545 else if(bg_opa >= LV_OPA_MAX) {
546 *res_color = lv_color_mix(fg_color, bg_color, fg_opa);
547 *res_opa = LV_OPA_COVER;
548 }
549 /*Both colors have alpha. Expensive calculation need to be applied*/
550 else {
551 /*Save the parameters and the result. If they will be asked again don't compute again*/
552 static lv_opa_t fg_opa_save = 0;
553 static lv_opa_t bg_opa_save = 0;
554 static lv_color_t fg_color_save = _LV_COLOR_ZERO_INITIALIZER;
555 static lv_color_t bg_color_save = _LV_COLOR_ZERO_INITIALIZER;
556 static lv_color_t res_color_saved = _LV_COLOR_ZERO_INITIALIZER;
557 static lv_opa_t res_opa_saved = 0;
558
559 if(fg_opa != fg_opa_save || bg_opa != bg_opa_save || fg_color.full != fg_color_save.full ||
560 bg_color.full != bg_color_save.full) {
561 fg_opa_save = fg_opa;
562 bg_opa_save = bg_opa;
563 fg_color_save.full = fg_color.full;
564 bg_color_save.full = bg_color.full;
565 /*Info:
566 * https://en.wikipedia.org/wiki/Alpha_compositing#Analytical_derivation_of_the_over_operator*/
567 res_opa_saved = 255 - ((uint16_t)((uint16_t)(255 - fg_opa) * (255 - bg_opa)) >> 8);
568 LV_ASSERT(res_opa_saved != 0);
569 lv_opa_t ratio = (uint16_t)((uint16_t)fg_opa * 255) / res_opa_saved;
570 res_color_saved = lv_color_mix(fg_color, bg_color, ratio);
571
572 }
573
574 res_color->full = res_color_saved.full;
575 *res_opa = res_opa_saved;
576 }
577 }
578
579 //! @endcond
580
581 /**
582 * Get the brightness of a color
583 * @param color a color
584 * @return the brightness [0..255]
585 */
lv_color_brightness(lv_color_t color)586 static inline uint8_t lv_color_brightness(lv_color_t color)
587 {
588 lv_color32_t c32;
589 c32.full = lv_color_to32(color);
590 uint16_t bright = (uint16_t)(3u * LV_COLOR_GET_R32(c32) + LV_COLOR_GET_B32(c32) + 4u * LV_COLOR_GET_G32(c32));
591 return (uint8_t)(bright >> 3);
592 }
593
lv_color_make(uint8_t r,uint8_t g,uint8_t b)594 static inline lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b)
595 {
596 return _LV_COLOR_MAKE_TYPE_HELPER LV_COLOR_MAKE(r, g, b);
597 }
598
lv_color_hex(uint32_t c)599 static inline lv_color_t lv_color_hex(uint32_t c)
600 {
601 #if LV_COLOR_DEPTH == 16
602 lv_color_t r;
603 #if LV_COLOR_16_SWAP == 0
604 /* Convert a 4 bytes per pixel in format ARGB32 to R5G6B5 format
605 naive way (by calling lv_color_make with components):
606 r = ((c & 0xFF0000) >> 19)
607 g = ((c & 0xFF00) >> 10)
608 b = ((c & 0xFF) >> 3)
609 rgb565 = (r << 11) | (g << 5) | b
610 That's 3 mask, 5 bitshift and 2 or operations
611
612 A bit better:
613 r = ((c & 0xF80000) >> 8)
614 g = ((c & 0xFC00) >> 5)
615 b = ((c & 0xFF) >> 3)
616 rgb565 = r | g | b
617 That's 3 mask, 3 bitshifts and 2 or operations */
618 r.full = (uint16_t)(((c & 0xF80000) >> 8) | ((c & 0xFC00) >> 5) | ((c & 0xFF) >> 3));
619 #else
620 /* We want: rrrr rrrr GGGg gggg bbbb bbbb => gggb bbbb rrrr rGGG */
621 r.full = (uint16_t)(((c & 0xF80000) >> 16) | ((c & 0xFC00) >> 13) | ((c & 0x1C00) << 3) | ((c & 0xF8) << 5));
622 #endif
623 return r;
624 #elif LV_COLOR_DEPTH == 32
625 lv_color_t r;
626 r.full = c | 0xFF000000;
627 return r;
628 #else /*LV_COLOR_DEPTH == 8*/
629 return lv_color_make((uint8_t)((c >> 16) & 0xFF), (uint8_t)((c >> 8) & 0xFF), (uint8_t)(c & 0xFF));
630 #endif
631 }
632
lv_color_hex3(uint32_t c)633 static inline lv_color_t lv_color_hex3(uint32_t c)
634 {
635 return lv_color_make((uint8_t)(((c >> 4) & 0xF0) | ((c >> 8) & 0xF)), (uint8_t)((c & 0xF0) | ((c & 0xF0) >> 4)),
636 (uint8_t)((c & 0xF) | ((c & 0xF) << 4)));
637 }
638
lv_color_filter_dsc_init(lv_color_filter_dsc_t * dsc,lv_color_filter_cb_t cb)639 static inline void lv_color_filter_dsc_init(lv_color_filter_dsc_t * dsc, lv_color_filter_cb_t cb)
640 {
641 dsc->filter_cb = cb;
642 }
643
644 //! @cond Doxygen_Suppress
645 //!
646 void /* LV_ATTRIBUTE_FAST_MEM */ lv_color_fill(lv_color_t * buf, lv_color_t color, uint32_t px_num);
647
648 //! @endcond
649 lv_color_t lv_color_lighten(lv_color_t c, lv_opa_t lvl);
650
651 lv_color_t lv_color_darken(lv_color_t c, lv_opa_t lvl);
652
653 lv_color_t lv_color_change_lightness(lv_color_t c, lv_opa_t lvl);
654
655 /**
656 * Convert a HSV color to RGB
657 * @param h hue [0..359]
658 * @param s saturation [0..100]
659 * @param v value [0..100]
660 * @return the given RGB color in RGB (with LV_COLOR_DEPTH depth)
661 */
662 lv_color_t lv_color_hsv_to_rgb(uint16_t h, uint8_t s, uint8_t v);
663
664 /**
665 * Convert a 32-bit RGB color to HSV
666 * @param r8 8-bit red
667 * @param g8 8-bit green
668 * @param b8 8-bit blue
669 * @return the given RGB color in HSV
670 */
671 lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r8, uint8_t g8, uint8_t b8);
672
673 /**
674 * Convert a color to HSV
675 * @param color color
676 * @return the given color in HSV
677 */
678 lv_color_hsv_t lv_color_to_hsv(lv_color_t color);
679
680 /**
681 * Just a wrapper around LV_COLOR_CHROMA_KEY because it might be more convenient to use a function in some cases
682 * @return LV_COLOR_CHROMA_KEY
683 */
lv_color_chroma_key(void)684 static inline lv_color_t lv_color_chroma_key(void)
685 {
686 return LV_COLOR_CHROMA_KEY;
687 }
688
689 /**********************
690 * PREDEFINED COLORS
691 **********************/
692 /*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/
693
694 lv_color_t lv_palette_main(lv_palette_t p);
lv_color_white(void)695 static inline lv_color_t lv_color_white(void)
696 {
697 return lv_color_make(0xff, 0xff, 0xff);
698 }
lv_color_black(void)699 static inline lv_color_t lv_color_black(void)
700 {
701 return lv_color_make(0x00, 0x0, 0x00);
702 }
703 lv_color_t lv_palette_lighten(lv_palette_t p, uint8_t lvl);
704 lv_color_t lv_palette_darken(lv_palette_t p, uint8_t lvl);
705
706 /**********************
707 * MACROS
708 **********************/
709
710 #ifdef __cplusplus
711 } /*extern "C"*/
712 #endif
713
714 #endif /*LV_COLOR_H*/
715