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 /*********************
22  *      DEFINES
23  *********************/
24 LV_EXPORT_CONST_INT(LV_COLOR_DEPTH);
25 
26 #if LV_COLOR_DEPTH == 8
27 #define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 2
28 #elif LV_COLOR_DEPTH == 16
29 #define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 3
30 #elif LV_COLOR_DEPTH == 24
31 #define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 4
32 #elif LV_COLOR_DEPTH == 32
33 #define LV_COLOR_NATIVE_WITH_ALPHA_SIZE 4
34 #endif
35 
36 /**
37  * Opacity percentages.
38  */
39 
40 enum {
41     LV_OPA_TRANSP = 0,
42     LV_OPA_0      = 0,
43     LV_OPA_10     = 25,
44     LV_OPA_20     = 51,
45     LV_OPA_30     = 76,
46     LV_OPA_40     = 102,
47     LV_OPA_50     = 127,
48     LV_OPA_60     = 153,
49     LV_OPA_70     = 178,
50     LV_OPA_80     = 204,
51     LV_OPA_90     = 229,
52     LV_OPA_100    = 255,
53     LV_OPA_COVER  = 255,
54 };
55 
56 #define LV_OPA_MIN 2    /**< Opacities below this will be transparent */
57 #define LV_OPA_MAX 253  /**< Opacities above this will fully cover */
58 
59 /**
60  * Get the pixel size of a color format in bits, bpp
61  * @param cf        a color format (`LV_COLOR_FORMAT_...`)
62  * @return          the pixel size in bits
63  * @sa              lv_color_format_get_bpp
64  */
65 #define LV_COLOR_FORMAT_GET_BPP(cf) (       \
66                                             (cf) == LV_COLOR_FORMAT_I1 ? 1 :        \
67                                             (cf) == LV_COLOR_FORMAT_A1 ? 1 :        \
68                                             (cf) == LV_COLOR_FORMAT_I2 ? 2 :        \
69                                             (cf) == LV_COLOR_FORMAT_A2 ? 2 :        \
70                                             (cf) == LV_COLOR_FORMAT_I4 ? 4 :        \
71                                             (cf) == LV_COLOR_FORMAT_A4 ? 4 :        \
72                                             (cf) == LV_COLOR_FORMAT_L8 ? 8 :        \
73                                             (cf) == LV_COLOR_FORMAT_A8 ? 8 :        \
74                                             (cf) == LV_COLOR_FORMAT_I8 ? 8 :        \
75                                             (cf) == LV_COLOR_FORMAT_ARGB2222 ? 8 :  \
76                                             (cf) == LV_COLOR_FORMAT_AL88 ? 16 :     \
77                                             (cf) == LV_COLOR_FORMAT_RGB565 ? 16 :   \
78                                             (cf) == LV_COLOR_FORMAT_RGB565A8 ? 16 : \
79                                             (cf) == LV_COLOR_FORMAT_YUY2 ? 16 :     \
80                                             (cf) == LV_COLOR_FORMAT_ARGB1555 ? 16 : \
81                                             (cf) == LV_COLOR_FORMAT_ARGB4444 ? 16 : \
82                                             (cf) == LV_COLOR_FORMAT_ARGB8565 ? 24 : \
83                                             (cf) == LV_COLOR_FORMAT_RGB888 ? 24 :   \
84                                             (cf) == LV_COLOR_FORMAT_ARGB8888 ? 32 : \
85                                             (cf) == LV_COLOR_FORMAT_XRGB8888 ? 32 : \
86                                             0                                       \
87                                     )
88 
89 /**
90  * Get the pixel size of a color format in bytes
91  * @param cf        a color format (`LV_COLOR_FORMAT_...`)
92  * @return          the pixel size in bytes
93  * @sa              lv_color_format_get_size
94  */
95 #define LV_COLOR_FORMAT_GET_SIZE(cf) ((LV_COLOR_FORMAT_GET_BPP(cf) + 7) >> 3)
96 
97 /**********************
98  *      TYPEDEFS
99  **********************/
100 
101 typedef struct {
102     uint8_t blue;
103     uint8_t green;
104     uint8_t red;
105 } lv_color_t;
106 
107 typedef struct {
108     uint16_t blue : 5;
109     uint16_t green : 6;
110     uint16_t red : 5;
111 } lv_color16_t;
112 
113 typedef struct {
114     uint8_t blue;
115     uint8_t green;
116     uint8_t red;
117     uint8_t alpha;
118 } lv_color32_t;
119 
120 typedef struct {
121     uint16_t h;
122     uint8_t s;
123     uint8_t v;
124 } lv_color_hsv_t;
125 
126 typedef struct {
127     uint8_t lumi;
128     uint8_t alpha;
129 } lv_color16a_t;
130 
131 typedef enum {
132     LV_COLOR_FORMAT_UNKNOWN           = 0,
133 
134     LV_COLOR_FORMAT_RAW               = 0x01,
135     LV_COLOR_FORMAT_RAW_ALPHA         = 0x02,
136 
137     /*<=1 byte (+alpha) formats*/
138     LV_COLOR_FORMAT_L8                = 0x06,
139     LV_COLOR_FORMAT_I1                = 0x07,
140     LV_COLOR_FORMAT_I2                = 0x08,
141     LV_COLOR_FORMAT_I4                = 0x09,
142     LV_COLOR_FORMAT_I8                = 0x0A,
143     LV_COLOR_FORMAT_A8                = 0x0E,
144 
145     /*2 byte (+alpha) formats*/
146     LV_COLOR_FORMAT_RGB565            = 0x12,
147     LV_COLOR_FORMAT_ARGB8565          = 0x13,   /**< Not supported by sw renderer yet. */
148     LV_COLOR_FORMAT_RGB565A8          = 0x14,   /**< Color array followed by Alpha array*/
149     LV_COLOR_FORMAT_AL88              = 0x15,   /**< L8 with alpha >*/
150 
151     /*3 byte (+alpha) formats*/
152     LV_COLOR_FORMAT_RGB888            = 0x0F,
153     LV_COLOR_FORMAT_ARGB8888          = 0x10,
154     LV_COLOR_FORMAT_XRGB8888          = 0x11,
155 
156     /*Formats not supported by software renderer but kept here so GPU can use it*/
157     LV_COLOR_FORMAT_A1                = 0x0B,
158     LV_COLOR_FORMAT_A2                = 0x0C,
159     LV_COLOR_FORMAT_A4                = 0x0D,
160     LV_COLOR_FORMAT_ARGB1555          = 0x16,
161     LV_COLOR_FORMAT_ARGB4444          = 0x17,
162     LV_COLOR_FORMAT_ARGB2222          = 0X18,
163 
164     /* reference to https://wiki.videolan.org/YUV/ */
165     /*YUV planar formats*/
166     LV_COLOR_FORMAT_YUV_START         = 0x20,
167     LV_COLOR_FORMAT_I420              = LV_COLOR_FORMAT_YUV_START,  /*YUV420 planar(3 plane)*/
168     LV_COLOR_FORMAT_I422              = 0x21,  /*YUV422 planar(3 plane)*/
169     LV_COLOR_FORMAT_I444              = 0x22,  /*YUV444 planar(3 plane)*/
170     LV_COLOR_FORMAT_I400              = 0x23,  /*YUV400 no chroma channel*/
171     LV_COLOR_FORMAT_NV21              = 0x24,  /*YUV420 planar(2 plane), UV plane in 'V, U, V, U'*/
172     LV_COLOR_FORMAT_NV12              = 0x25,  /*YUV420 planar(2 plane), UV plane in 'U, V, U, V'*/
173 
174     /*YUV packed formats*/
175     LV_COLOR_FORMAT_YUY2              = 0x26,  /*YUV422 packed like 'Y U Y V'*/
176     LV_COLOR_FORMAT_UYVY              = 0x27,  /*YUV422 packed like 'U Y V Y'*/
177 
178     LV_COLOR_FORMAT_YUV_END           = LV_COLOR_FORMAT_UYVY,
179 
180     LV_COLOR_FORMAT_PROPRIETARY_START = 0x30,
181 
182     LV_COLOR_FORMAT_NEMA_TSC_START    = LV_COLOR_FORMAT_PROPRIETARY_START,
183     LV_COLOR_FORMAT_NEMA_TSC4         = LV_COLOR_FORMAT_NEMA_TSC_START,
184     LV_COLOR_FORMAT_NEMA_TSC6         = 0x31,
185     LV_COLOR_FORMAT_NEMA_TSC6A        = 0x32,
186     LV_COLOR_FORMAT_NEMA_TSC6AP       = 0x33,
187     LV_COLOR_FORMAT_NEMA_TSC12        = 0x34,
188     LV_COLOR_FORMAT_NEMA_TSC12A       = 0x35,
189     LV_COLOR_FORMAT_NEMA_TSC_END      = LV_COLOR_FORMAT_NEMA_TSC12A,
190 
191     /*Color formats in which LVGL can render*/
192 #if LV_COLOR_DEPTH == 1
193     LV_COLOR_FORMAT_NATIVE            = LV_COLOR_FORMAT_I1,
194     LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_I1,
195 #elif LV_COLOR_DEPTH == 8
196     LV_COLOR_FORMAT_NATIVE            = LV_COLOR_FORMAT_L8,
197     LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_AL88,
198 #elif LV_COLOR_DEPTH == 16
199     LV_COLOR_FORMAT_NATIVE            = LV_COLOR_FORMAT_RGB565,
200     LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_RGB565A8,
201 #elif LV_COLOR_DEPTH == 24
202     LV_COLOR_FORMAT_NATIVE            = LV_COLOR_FORMAT_RGB888,
203     LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_ARGB8888,
204 #elif LV_COLOR_DEPTH == 32
205     LV_COLOR_FORMAT_NATIVE            = LV_COLOR_FORMAT_XRGB8888,
206     LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_ARGB8888,
207 #else
208 #error "LV_COLOR_DEPTH should be 1, 8, 16, 24 or 32"
209 #endif
210 
211 } lv_color_format_t;
212 
213 #define LV_COLOR_FORMAT_IS_ALPHA_ONLY(cf) ((cf) >= LV_COLOR_FORMAT_A1 && (cf) <= LV_COLOR_FORMAT_A8)
214 #define LV_COLOR_FORMAT_IS_INDEXED(cf) ((cf) >= LV_COLOR_FORMAT_I1 && (cf) <= LV_COLOR_FORMAT_I8)
215 #define LV_COLOR_FORMAT_IS_YUV(cf)  ((cf) >= LV_COLOR_FORMAT_YUV_START && (cf) <= LV_COLOR_FORMAT_YUV_END)
216 #define LV_COLOR_INDEXED_PALETTE_SIZE(cf) ((cf) == LV_COLOR_FORMAT_I1 ? 2 :\
217                                            (cf) == LV_COLOR_FORMAT_I2 ? 4 :\
218                                            (cf) == LV_COLOR_FORMAT_I4 ? 16 :\
219                                            (cf) == LV_COLOR_FORMAT_I8 ? 256 : 0)
220 
221 /**********************
222  * MACROS
223  **********************/
224 
225 #define LV_COLOR_MAKE(r8, g8, b8) {b8, g8, r8}
226 
227 #define LV_OPA_MIX2(a1, a2) (((int32_t)(a1) * (a2)) >> 8)
228 #define LV_OPA_MIX3(a1, a2, a3) (((int32_t)(a1) * (a2) * (a3)) >> 16)
229 
230 /**********************
231  * GLOBAL PROTOTYPES
232  **********************/
233 
234 /**
235  * Get the pixel size of a color format in bits, bpp
236  * @param cf        a color format (`LV_COLOR_FORMAT_...`)
237  * @return          the pixel size in bits
238  * @sa              LV_COLOR_FORMAT_GET_BPP
239  */
240 uint8_t lv_color_format_get_bpp(lv_color_format_t cf);
241 
242 /**
243  * Get the pixel size of a color format in bytes
244  * @param cf        a color format (`LV_COLOR_FORMAT_...`)
245  * @return          the pixel size in bytes
246  * @sa              LV_COLOR_FORMAT_GET_SIZE
247  */
248 uint8_t lv_color_format_get_size(lv_color_format_t cf);
249 
250 /**
251  * Check if a color format has alpha channel or not
252  * @param src_cf    a color format (`LV_COLOR_FORMAT_...`)
253  * @return          true: has alpha channel; false: doesn't have alpha channel
254  */
255 bool lv_color_format_has_alpha(lv_color_format_t src_cf);
256 
257 /**
258  * Create an ARGB8888 color from RGB888 + alpha
259  * @param color     an RGB888 color
260  * @param opa       the alpha value
261  * @return          the ARGB8888 color
262  */
263 lv_color32_t lv_color_to_32(lv_color_t color, lv_opa_t opa);
264 
265 /**
266  * Convert an RGB888 color to an integer
267  * @param c     an RGB888 color
268  * @return      `c` as an integer
269  */
270 uint32_t lv_color_to_int(lv_color_t c);
271 
272 /**
273  * Check if two RGB888 color are equal
274  * @param c1    the first color
275  * @param c2    the second color
276  * @return      true: equal
277  */
278 bool lv_color_eq(lv_color_t c1, lv_color_t c2);
279 
280 /**
281  * Check if two ARGB8888 color are equal
282  * @param c1    the first color
283  * @param c2    the second color
284  * @return      true: equal
285  */
286 bool lv_color32_eq(lv_color32_t c1, lv_color32_t c2);
287 
288 /**
289  * Create a color from 0x000000..0xffffff input
290  * @param c     the hex input
291  * @return      the color
292  */
293 lv_color_t lv_color_hex(uint32_t c);
294 
295 /**
296  * Create an RGB888 color
297  * @param r     the red channel (0..255)
298  * @param g     the green channel (0..255)
299  * @param b     the blue channel (0..255)
300  * @return      the color
301  */
302 lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b);
303 
304 /**
305  * Create an ARGB8888 color
306  * @param r     the red channel (0..255)
307  * @param g     the green channel (0..255)
308  * @param b     the blue channel (0..255)
309  * @param a     the alpha channel (0..255)
310  * @return      the color
311  */
312 lv_color32_t lv_color32_make(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
313 
314 /**
315  * Create a color from 0x000..0xfff input
316  * @param c     the hex input (e.g. 0x123 will be 0x112233)
317  * @return      the color
318  */
319 lv_color_t lv_color_hex3(uint32_t c);
320 
321 /**
322  * Convert am RGB888 color to RGB565 stored in `uint16_t`
323  * @param color     and RGB888 color
324  * @return          `color` as RGB565 on `uin16_t`
325  */
326 uint16_t lv_color_to_u16(lv_color_t color);
327 
328 /**
329  * Convert am RGB888 color to XRGB8888 stored in `uint32_t`
330  * @param color     and RGB888 color
331  * @return          `color` as XRGB8888 on `uin32_t` (the alpha channel is always set to 0xFF)
332  */
333 uint32_t lv_color_to_u32(lv_color_t color);
334 
335 /**
336  * Mix two RGB565 colors
337  * @param c1        the first color (typically the foreground color)
338  * @param c2        the second color  (typically the background color)
339  * @param mix       0..255, or LV_OPA_0/10/20...
340  * @return          mix == 0: c2
341  *                  mix == 255: c1
342  *                  mix == 128: 0.5 x c1 + 0.5 x c2
343  */
344 uint16_t LV_ATTRIBUTE_FAST_MEM lv_color_16_16_mix(uint16_t c1, uint16_t c2, uint8_t mix);
345 
346 /**
347  * Mix white to a color
348  * @param c     the base color
349  * @param lvl   the intensity of white (0: no change, 255: fully white)
350  * @return      the mixed color
351  */
352 lv_color_t lv_color_lighten(lv_color_t c, lv_opa_t lvl);
353 
354 /**
355  * Mix black to a color
356  * @param c     the base color
357  * @param lvl   the intensity of black (0: no change, 255: fully black)
358  * @return      the mixed color
359  */
360 lv_color_t lv_color_darken(lv_color_t c, lv_opa_t lvl);
361 
362 /**
363  * Convert a HSV color to RGB
364  * @param h hue [0..359]
365  * @param s saturation [0..100]
366  * @param v value [0..100]
367  * @return the given RGB color in RGB (with LV_COLOR_DEPTH depth)
368  */
369 lv_color_t lv_color_hsv_to_rgb(uint16_t h, uint8_t s, uint8_t v);
370 
371 /**
372  * Convert a 32-bit RGB color to HSV
373  * @param r8 8-bit red
374  * @param g8 8-bit green
375  * @param b8 8-bit blue
376  * @return the given RGB color in HSV
377  */
378 lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r8, uint8_t g8, uint8_t b8);
379 
380 /**
381  * Convert a color to HSV
382  * @param color color
383  * @return the given color in HSV
384  */
385 lv_color_hsv_t lv_color_to_hsv(lv_color_t color);
386 
387 /*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/
388 
389 /**
390  * A helper for white color
391  * @return      a white color
392  */
393 lv_color_t lv_color_white(void);
394 
395 /**
396  * A helper for black color
397  * @return      a black color
398  */
399 lv_color_t lv_color_black(void);
400 
401 void lv_color_premultiply(lv_color32_t * c);
402 
403 void lv_color16_premultiply(lv_color16_t * c, lv_opa_t a);
404 
405 /**
406  * Get the luminance of a color: luminance = 0.3 R + 0.59 G + 0.11 B
407  * @param c a color
408  * @return the brightness [0..255]
409  */
410 uint8_t lv_color_luminance(lv_color_t c);
411 
412 /**
413  * Get the luminance of a color16: luminance = 0.3 R + 0.59 G + 0.11 B
414  * @param c a color
415  * @return the brightness [0..255]
416  */
417 uint8_t lv_color16_luminance(const lv_color16_t c);
418 
419 /**
420  * Get the luminance of a color24: luminance = 0.3 R + 0.59 G + 0.11 B
421  * @param c a color
422  * @return the brightness [0..255]
423  */
424 uint8_t lv_color24_luminance(const uint8_t * c);
425 
426 /**
427  * Get the luminance of a color32: luminance = 0.3 R + 0.59 G + 0.11 B
428  * @param c a color
429  * @return the brightness [0..255]
430  */
431 uint8_t lv_color32_luminance(lv_color32_t c);
432 
433 /**********************
434  *      MACROS
435  **********************/
436 
437 #include "lv_palette.h"
438 #include "lv_color_op.h"
439 
440 LV_ATTRIBUTE_EXTERN_DATA extern const lv_color_filter_dsc_t lv_color_filter_shade;
441 
442 #ifdef __cplusplus
443 } /*extern "C"*/
444 #endif
445 
446 #endif /*LV_COLOR_H*/
447