mikroSDK Reference Manual
lv_color.h
Go to the documentation of this file.
1
6#ifndef LV_COLOR_H
7#define LV_COLOR_H
8
9#ifdef __cplusplus
10extern "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 *********************/
35LV_EXPORT_CONST_INT(LV_COLOR_DEPTH);
36LV_EXPORT_CONST_INT(LV_COLOR_16_SWAP);
37
41enum {
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)
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
190typedef 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;
198
199typedef union {
200 struct {
201 uint8_t blue : 2;
202 uint8_t green : 3;
203 uint8_t red : 3;
204 } ch;
205 uint8_t full;
207
208typedef 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;
223
224typedef 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;
233
234typedef LV_CONCAT3(uint, LV_COLOR_SIZE, _t) lv_color_int_t;
235typedef LV_CONCAT3(lv_color, LV_COLOR_DEPTH, _t) lv_color_t;
236
237typedef struct {
238 uint16_t h;
239 uint8_t s;
240 uint8_t v;
242
244/*No idea where the guard is required but else throws warnings in the docs*/
245typedef uint8_t lv_opa_t;
247
249
250typedef lv_color_t (*lv_color_filter_cb_t)(const struct _lv_color_filter_dsc_t *, lv_color_t, lv_opa_t);
251
253 lv_color_filter_cb_t filter_cb;
254 void * user_data;
256
257
258typedef enum {
259 LV_PALETTE_RED,
260 LV_PALETTE_PINK,
261 LV_PALETTE_PURPLE,
262 LV_PALETTE_DEEP_PURPLE,
263 LV_PALETTE_INDIGO,
264 LV_PALETTE_BLUE,
265 LV_PALETTE_LIGHT_BLUE,
266 LV_PALETTE_CYAN,
267 LV_PALETTE_TEAL,
268 LV_PALETTE_GREEN,
269 LV_PALETTE_LIGHT_GREEN,
270 LV_PALETTE_LIME,
271 LV_PALETTE_YELLOW,
272 LV_PALETTE_AMBER,
273 LV_PALETTE_ORANGE,
274 LV_PALETTE_DEEP_ORANGE,
275 LV_PALETTE_BROWN,
276 LV_PALETTE_BLUE_GREY,
277 LV_PALETTE_GREY,
278 _LV_PALETTE_LAST,
279 LV_PALETTE_NONE = 0xff,
280} lv_palette_t;
281
282/**********************
283 * GLOBAL PROTOTYPES
284 **********************/
285
286/*In color conversations:
287 * - When converting to bigger color type the LSB weight of 1 LSB is calculated
288 * E.g. 16 bit Red has 5 bits
289 * 8 bit Red has 3 bits
290 * ----------------------
291 * 8 bit red LSB = (2^5 - 1) / (2^3 - 1) = 31 / 7 = 4
292 *
293 * - When calculating to smaller color type simply shift out the LSBs
294 * E.g. 8 bit Red has 3 bits
295 * 16 bit Red has 5 bits
296 * ----------------------
297 * Shift right with 5 - 3 = 2
298 */
299static inline uint8_t lv_color_to1(lv_color_t color)
300{
301#if LV_COLOR_DEPTH == 1
302 return color.full;
303#elif LV_COLOR_DEPTH == 8
304 if((LV_COLOR_GET_R(color) & 0x4) || (LV_COLOR_GET_G(color) & 0x4) || (LV_COLOR_GET_B(color) & 0x2)) {
305 return 1;
306 }
307 else {
308 return 0;
309 }
310#elif LV_COLOR_DEPTH == 16
311 if((LV_COLOR_GET_R(color) & 0x10) || (LV_COLOR_GET_G(color) & 0x20) || (LV_COLOR_GET_B(color) & 0x10)) {
312 return 1;
313 }
314 else {
315 return 0;
316 }
317#elif LV_COLOR_DEPTH == 32
318 if((LV_COLOR_GET_R(color) & 0x80) || (LV_COLOR_GET_G(color) & 0x80) || (LV_COLOR_GET_B(color) & 0x80)) {
319 return 1;
320 }
321 else {
322 return 0;
323 }
324#endif
325}
326
327static inline uint8_t lv_color_to8(lv_color_t color)
328{
329#if LV_COLOR_DEPTH == 1
330 if(color.full == 0)
331 return 0;
332 else
333 return 0xFF;
334#elif LV_COLOR_DEPTH == 8
335 return color.full;
336#elif LV_COLOR_DEPTH == 16
337 lv_color8_t ret;
338 LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 2); /*5 - 3 = 2*/
339 LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 3); /*6 - 3 = 3*/
340 LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 3); /*5 - 2 = 3*/
341 return ret.full;
342#elif LV_COLOR_DEPTH == 32
343 lv_color8_t ret;
344 LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 5); /*8 - 3 = 5*/
345 LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 5); /*8 - 3 = 5*/
346 LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 6); /*8 - 2 = 6*/
347 return ret.full;
348#endif
349}
350
351static inline uint16_t lv_color_to16(lv_color_t color)
352{
353#if LV_COLOR_DEPTH == 1
354 if(color.full == 0)
355 return 0;
356 else
357 return 0xFFFF;
358#elif LV_COLOR_DEPTH == 8
359 lv_color16_t ret;
360 LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) * 4); /*(2^5 - 1)/(2^3 - 1) = 31/7 = 4*/
361 LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) * 9); /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
362 LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) * 10); /*(2^5 - 1)/(2^2 - 1) = 31/3 = 10*/
363 return ret.full;
364#elif LV_COLOR_DEPTH == 16
365 return color.full;
366#elif LV_COLOR_DEPTH == 32
367 lv_color16_t ret;
368 LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) >> 3); /*8 - 5 = 3*/
369 LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) >> 2); /*8 - 6 = 2*/
370 LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) >> 3); /*8 - 5 = 3*/
371 return ret.full;
372#endif
373}
374
375static inline uint32_t lv_color_to32(lv_color_t color)
376{
377#if LV_COLOR_DEPTH == 1
378 if(color.full == 0)
379 return 0xFF000000;
380 else
381 return 0xFFFFFFFF;
382#elif LV_COLOR_DEPTH == 8
383 lv_color32_t ret;
384 LV_COLOR_SET_R32(ret, LV_COLOR_GET_R(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
385 LV_COLOR_SET_G32(ret, LV_COLOR_GET_G(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
386 LV_COLOR_SET_B32(ret, LV_COLOR_GET_B(color) * 85); /*(2^8 - 1)/(2^2 - 1) = 255/3 = 85*/
387 LV_COLOR_SET_A32(ret, 0xFF);
388 return ret.full;
389#elif LV_COLOR_DEPTH == 16
419 lv_color32_t ret;
420 LV_COLOR_SET_R32(ret, (LV_COLOR_GET_R(color) * 263 + 7) >> 5);
421 LV_COLOR_SET_G32(ret, (LV_COLOR_GET_G(color) * 259 + 3) >> 6);
422 LV_COLOR_SET_B32(ret, (LV_COLOR_GET_B(color) * 263 + 7) >> 5);
423 LV_COLOR_SET_A32(ret, 0xFF);
424 return ret.full;
425#elif LV_COLOR_DEPTH == 32
426 return color.full;
427#endif
428}
429
431
439LV_ATTRIBUTE_FAST_MEM static inline lv_color_t lv_color_mix(lv_color_t c1, lv_color_t c2, uint8_t mix)
440{
441 lv_color_t ret;
442
443#if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP == 0 && LV_COLOR_MIX_ROUND_OFS == 0
444 /*Source: https://stackoverflow.com/a/50012418/1999969*/
445 mix = (uint32_t)((uint32_t)mix + 4) >> 3;
446 uint32_t bg = (uint32_t)((uint32_t)c2.full | ((uint32_t)c2.full << 16)) &
447 0x7E0F81F; /*0b00000111111000001111100000011111*/
448 uint32_t fg = (uint32_t)((uint32_t)c1.full | ((uint32_t)c1.full << 16)) & 0x7E0F81F;
449 uint32_t result = ((((fg - bg) * mix) >> 5) + bg) & 0x7E0F81F;
450 ret.full = (uint16_t)((result >> 16) | result);
451#elif LV_COLOR_DEPTH != 1
452 /*LV_COLOR_DEPTH == 8, 16 or 32*/
453 LV_COLOR_SET_R(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_R(c1) * mix + LV_COLOR_GET_R(c2) *
454 (255 - mix) + LV_COLOR_MIX_ROUND_OFS));
455 LV_COLOR_SET_G(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_G(c1) * mix + LV_COLOR_GET_G(c2) *
456 (255 - mix) + LV_COLOR_MIX_ROUND_OFS));
457 LV_COLOR_SET_B(ret, LV_UDIV255((uint16_t)LV_COLOR_GET_B(c1) * mix + LV_COLOR_GET_B(c2) *
458 (255 - mix) + LV_COLOR_MIX_ROUND_OFS));
459 LV_COLOR_SET_A(ret, 0xFF);
460#else
461 /*LV_COLOR_DEPTH == 1*/
462 ret.full = mix > LV_OPA_50 ? c1.full : c2.full;
463#endif
464
465 return ret;
466}
467
468LV_ATTRIBUTE_FAST_MEM static inline void lv_color_premult(lv_color_t c, uint8_t mix, uint16_t * out)
469{
470#if LV_COLOR_DEPTH != 1
471 out[0] = (uint16_t)LV_COLOR_GET_R(c) * mix;
472 out[1] = (uint16_t)LV_COLOR_GET_G(c) * mix;
473 out[2] = (uint16_t)LV_COLOR_GET_B(c) * mix;
474#else
475 (void) mix;
476 /*Pre-multiplication can't be used with 1 bpp*/
477 out[0] = LV_COLOR_GET_R(c);
478 out[1] = LV_COLOR_GET_G(c);
479 out[2] = LV_COLOR_GET_B(c);
480#endif
481
482}
483
493LV_ATTRIBUTE_FAST_MEM static inline lv_color_t lv_color_mix_premult(uint16_t * premult_c1, lv_color_t c2, uint8_t mix)
494{
495 lv_color_t ret;
496#if LV_COLOR_DEPTH != 1
497 /*LV_COLOR_DEPTH == 8 or 32*/
498 LV_COLOR_SET_R(ret, LV_UDIV255(premult_c1[0] + LV_COLOR_GET_R(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
499 LV_COLOR_SET_G(ret, LV_UDIV255(premult_c1[1] + LV_COLOR_GET_G(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
500 LV_COLOR_SET_B(ret, LV_UDIV255(premult_c1[2] + LV_COLOR_GET_B(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
501 LV_COLOR_SET_A(ret, 0xFF);
502#else
503 /*LV_COLOR_DEPTH == 1*/
504 /*Restore color1*/
505 lv_color_t c1;
506 LV_COLOR_SET_R(c1, premult_c1[0]);
507 LV_COLOR_SET_G(c1, premult_c1[1]);
508 LV_COLOR_SET_B(c1, premult_c1[2]);
509 ret.full = mix > LV_OPA_50 ? c2.full : c1.full;
510#endif
511
512 return ret;
513}
514
524LV_ATTRIBUTE_FAST_MEM static inline void lv_color_mix_with_alpha(lv_color_t bg_color, lv_opa_t bg_opa,
525 lv_color_t fg_color, lv_opa_t fg_opa,
526 lv_color_t * res_color, lv_opa_t * res_opa)
527{
528 /*Pick the foreground if it's fully opaque or the Background is fully transparent*/
529 if(fg_opa >= LV_OPA_MAX || bg_opa <= LV_OPA_MIN) {
530 res_color->full = fg_color.full;
531 *res_opa = fg_opa;
532 }
533 /*Transparent foreground: use the Background*/
534 else if(fg_opa <= LV_OPA_MIN) {
535 res_color->full = bg_color.full;
536 *res_opa = bg_opa;
537 }
538 /*Opaque background: use simple mix*/
539 else if(bg_opa >= LV_OPA_MAX) {
540 *res_color = lv_color_mix(fg_color, bg_color, fg_opa);
541 *res_opa = LV_OPA_COVER;
542 }
543 /*Both colors have alpha. Expensive calculation need to be applied*/
544 else {
545 /*Save the parameters and the result. If they will be asked again don't compute again*/
546 static lv_opa_t fg_opa_save = 0;
547 static lv_opa_t bg_opa_save = 0;
548 static lv_color_t fg_color_save = _LV_COLOR_ZERO_INITIALIZER;
549 static lv_color_t bg_color_save = _LV_COLOR_ZERO_INITIALIZER;
550 static lv_color_t res_color_saved = _LV_COLOR_ZERO_INITIALIZER;
551 static lv_opa_t res_opa_saved = 0;
552
553 if(fg_opa != fg_opa_save || bg_opa != bg_opa_save || fg_color.full != fg_color_save.full ||
554 bg_color.full != bg_color_save.full) {
555 fg_opa_save = fg_opa;
556 bg_opa_save = bg_opa;
557 fg_color_save.full = fg_color.full;
558 bg_color_save.full = bg_color.full;
559 /*Info:
560 * https://en.wikipedia.org/wiki/Alpha_compositing#Analytical_derivation_of_the_over_operator*/
561 res_opa_saved = 255 - ((uint16_t)((uint16_t)(255 - fg_opa) * (255 - bg_opa)) >> 8);
562 LV_ASSERT(res_opa_saved != 0);
563 lv_opa_t ratio = (uint16_t)((uint16_t)fg_opa * 255) / res_opa_saved;
564 res_color_saved = lv_color_mix(fg_color, bg_color, ratio);
565
566 }
567
568 res_color->full = res_color_saved.full;
569 *res_opa = res_opa_saved;
570 }
571}
572
574
580static inline uint8_t lv_color_brightness(lv_color_t color)
581{
582 lv_color32_t c32;
583 c32.full = lv_color_to32(color);
584 uint16_t bright = (uint16_t)(3u * LV_COLOR_GET_R32(c32) + LV_COLOR_GET_B32(c32) + 4u * LV_COLOR_GET_G32(c32));
585 return (uint8_t)(bright >> 3);
586}
587
588static inline lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b)
589{
590 return _LV_COLOR_MAKE_TYPE_HELPER LV_COLOR_MAKE(r, g, b);
591}
592
593static inline lv_color_t lv_color_hex(uint32_t c)
594{
595#if LV_COLOR_DEPTH == 16
596 lv_color_t r;
597#if LV_COLOR_16_SWAP == 0
598 /* Convert a 4 bytes per pixel in format ARGB32 to R5G6B5 format
599 naive way (by calling lv_color_make with components):
600 r = ((c & 0xFF0000) >> 19)
601 g = ((c & 0xFF00) >> 10)
602 b = ((c & 0xFF) >> 3)
603 rgb565 = (r << 11) | (g << 5) | b
604 That's 3 mask, 5 bitshift and 2 or operations
605
606 A bit better:
607 r = ((c & 0xF80000) >> 8)
608 g = ((c & 0xFC00) >> 5)
609 b = ((c & 0xFF) >> 3)
610 rgb565 = r | g | b
611 That's 3 mask, 3 bitshifts and 2 or operations */
612 r.full = (uint16_t)(((c & 0xF80000) >> 8) | ((c & 0xFC00) >> 5) | ((c & 0xFF) >> 3));
613#else
614 /* We want: rrrr rrrr GGGg gggg bbbb bbbb => gggb bbbb rrrr rGGG */
615 r.full = (uint16_t)(((c & 0xF80000) >> 16) | ((c & 0xFC00) >> 13) | ((c & 0x1C00) << 3) | ((c & 0xF8) << 5));
616#endif
617 return r;
618#elif LV_COLOR_DEPTH == 32
619 lv_color_t r;
620 r.full = c | 0xFF000000;
621 return r;
622#else /*LV_COLOR_DEPTH == 8*/
623 return lv_color_make((uint8_t)((c >> 16) & 0xFF), (uint8_t)((c >> 8) & 0xFF), (uint8_t)(c & 0xFF));
624#endif
625}
626
627static inline lv_color_t lv_color_hex3(uint32_t c)
628{
629 return lv_color_make((uint8_t)(((c >> 4) & 0xF0) | ((c >> 8) & 0xF)), (uint8_t)((c & 0xF0) | ((c & 0xF0) >> 4)),
630 (uint8_t)((c & 0xF) | ((c & 0xF) << 4)));
631}
632
633static inline void lv_color_filter_dsc_init(lv_color_filter_dsc_t * dsc, lv_color_filter_cb_t cb)
634{
635 dsc->filter_cb = cb;
636}
637
640LV_ATTRIBUTE_FAST_MEM void lv_color_fill(lv_color_t * buf, lv_color_t color, uint32_t px_num);
641
643lv_color_t lv_color_lighten(lv_color_t c, lv_opa_t lvl);
644
645lv_color_t lv_color_darken(lv_color_t c, lv_opa_t lvl);
646
647lv_color_t lv_color_change_lightness(lv_color_t c, lv_opa_t lvl);
648
656lv_color_t lv_color_hsv_to_rgb(uint16_t h, uint8_t s, uint8_t v);
657
665lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r8, uint8_t g8, uint8_t b8);
666
673
678static inline lv_color_t lv_color_chroma_key(void)
679{
680 return LV_COLOR_CHROMA_KEY;
681}
682
683/**********************
684 * PREDEFINED COLORS
685 **********************/
686/*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/
687
688lv_color_t lv_palette_main(lv_palette_t p);
689static inline lv_color_t lv_color_white(void)
690{
691 return lv_color_make(0xff, 0xff, 0xff);
692}
693static inline lv_color_t lv_color_black(void)
694{
695 return lv_color_make(0x00, 0x0, 0x00);
696}
697lv_color_t lv_palette_lighten(lv_palette_t p, uint8_t lvl);
698lv_color_t lv_palette_darken(lv_palette_t p, uint8_t lvl);
699
700/**********************
701 * MACROS
702 **********************/
703
704#ifdef __cplusplus
705} /*extern "C"*/
706#endif
707
708#endif /*LV_COLOR_H*/
lv_color_t lv_color_hsv_to_rgb(uint16_t h, uint8_t s, uint8_t v)
lv_color_hsv_t lv_color_to_hsv(lv_color_t color)
lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r8, uint8_t g8, uint8_t b8)
Definition lv_color.h:252
Definition lv_color.h:237
Definition lv_color.h:208
Definition lv_color.h:190
Definition lv_color.h:224
Definition lv_color.h:199