2017-11-23 20:42:14 +01:00
|
|
|
/**
|
2017-11-23 21:28:36 +01:00
|
|
|
* @file lv_color.h
|
2018-06-19 09:49:58 +02:00
|
|
|
*
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
|
|
|
|
2017-11-26 11:38:28 +01:00
|
|
|
#ifndef LV_COLOR_H
|
|
|
|
#define LV_COLOR_H
|
2017-11-23 20:42:14 +01:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*********************
|
|
|
|
* INCLUDES
|
|
|
|
*********************/
|
2019-12-26 02:49:30 +01:00
|
|
|
#include "../lv_conf_internal.h"
|
2021-11-08 03:54:13 -06:00
|
|
|
#include "lv_assert.h"
|
2020-03-26 08:58:16 +01:00
|
|
|
#include "lv_math.h"
|
2021-02-15 06:39:49 -08:00
|
|
|
#include "lv_types.h"
|
2017-11-23 20:42:14 +01:00
|
|
|
#include <stdint.h>
|
2023-02-20 20:50:58 +01:00
|
|
|
#include <stdbool.h>
|
2017-11-23 20:42:14 +01:00
|
|
|
|
|
|
|
/*********************
|
|
|
|
* DEFINES
|
|
|
|
*********************/
|
2021-10-18 13:39:04 +02:00
|
|
|
LV_EXPORT_CONST_INT(LV_COLOR_DEPTH);
|
2017-11-23 21:28:36 +01:00
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
#if LV_COLOR_DEPTH == 8
|
|
|
|
#define _LV_COLOR_NATIVE_WITH_ALPHA_SIZE 2
|
|
|
|
#elif LV_COLOR_DEPTH == 16
|
|
|
|
#define _LV_COLOR_NATIVE_WITH_ALPHA_SIZE 3
|
|
|
|
#elif LV_COLOR_DEPTH == 24
|
|
|
|
#define _LV_COLOR_NATIVE_WITH_ALPHA_SIZE 4
|
|
|
|
#elif LV_COLOR_DEPTH == 32
|
|
|
|
#define _LV_COLOR_NATIVE_WITH_ALPHA_SIZE 4
|
|
|
|
#endif
|
|
|
|
|
2019-06-27 18:07:26 -04:00
|
|
|
/**
|
|
|
|
* Opacity percentages.
|
|
|
|
*/
|
2023-04-27 06:42:02 -06:00
|
|
|
|
|
|
|
enum _lv_opa_t {
|
2019-04-04 07:15:40 +02:00
|
|
|
LV_OPA_TRANSP = 0,
|
|
|
|
LV_OPA_0 = 0,
|
|
|
|
LV_OPA_10 = 25,
|
|
|
|
LV_OPA_20 = 51,
|
|
|
|
LV_OPA_30 = 76,
|
|
|
|
LV_OPA_40 = 102,
|
|
|
|
LV_OPA_50 = 127,
|
|
|
|
LV_OPA_60 = 153,
|
|
|
|
LV_OPA_70 = 178,
|
|
|
|
LV_OPA_80 = 204,
|
|
|
|
LV_OPA_90 = 229,
|
|
|
|
LV_OPA_100 = 255,
|
|
|
|
LV_OPA_COVER = 255,
|
2019-01-14 15:08:54 +01:00
|
|
|
};
|
2017-11-23 20:42:14 +01:00
|
|
|
|
2023-04-27 06:42:02 -06:00
|
|
|
#ifdef DOXYGEN
|
|
|
|
typedef _lv_opa_t lv_opa_t;
|
|
|
|
#else
|
|
|
|
typedef uint8_t lv_opa_t;
|
|
|
|
#endif /*DOXYGEN*/
|
|
|
|
|
|
|
|
|
2020-06-14 12:36:57 +02:00
|
|
|
#define LV_OPA_MIN 2 /*Opacities below this will be transparent*/
|
|
|
|
#define LV_OPA_MAX 253 /*Opacities above this will fully cover*/
|
2018-08-08 09:50:01 +02:00
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
/**********************
|
|
|
|
* TYPEDEFS
|
|
|
|
**********************/
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
typedef struct {
|
2023-02-20 20:50:58 +01:00
|
|
|
uint8_t blue;
|
|
|
|
uint8_t green;
|
|
|
|
uint8_t red;
|
2023-07-05 13:05:19 +02:00
|
|
|
} lv_color_t;
|
2017-11-23 20:42:14 +01:00
|
|
|
|
2023-02-20 20:50:58 +01:00
|
|
|
typedef struct {
|
|
|
|
uint16_t blue : 5;
|
|
|
|
uint16_t green : 6;
|
|
|
|
uint16_t red : 5;
|
2018-06-19 09:49:58 +02:00
|
|
|
} lv_color16_t;
|
2017-11-23 20:42:14 +01:00
|
|
|
|
2023-02-20 20:50:58 +01:00
|
|
|
typedef struct {
|
|
|
|
uint8_t blue;
|
|
|
|
uint8_t green;
|
|
|
|
uint8_t red;
|
|
|
|
uint8_t alpha;
|
2018-09-21 07:23:44 +02:00
|
|
|
} lv_color32_t;
|
2017-11-23 20:42:14 +01:00
|
|
|
|
2020-02-26 19:48:27 +01:00
|
|
|
typedef struct {
|
2017-11-23 20:42:14 +01:00
|
|
|
uint16_t h;
|
|
|
|
uint8_t s;
|
|
|
|
uint8_t v;
|
2017-11-23 21:28:36 +01:00
|
|
|
} lv_color_hsv_t;
|
2017-11-23 20:42:14 +01:00
|
|
|
|
2023-07-14 02:43:14 +08:00
|
|
|
enum _lv_color_format_t {
|
|
|
|
LV_COLOR_FORMAT_UNKNOWN = 0,
|
2020-12-19 06:51:14 +01:00
|
|
|
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_RAW = 0x01,
|
|
|
|
LV_COLOR_FORMAT_RAW_ALPHA = 0x02,
|
2022-07-21 05:17:01 +02:00
|
|
|
|
2023-02-20 20:50:58 +01:00
|
|
|
/*<=1 byte (+alpha) formats*/
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_L8 = 0x06,
|
|
|
|
LV_COLOR_FORMAT_I1 = 0x07,
|
|
|
|
LV_COLOR_FORMAT_I2 = 0x08,
|
|
|
|
LV_COLOR_FORMAT_I4 = 0x09,
|
|
|
|
LV_COLOR_FORMAT_I8 = 0x0A,
|
|
|
|
LV_COLOR_FORMAT_A8 = 0x0E,
|
2022-07-21 05:17:01 +02:00
|
|
|
|
2023-02-20 20:50:58 +01:00
|
|
|
/*2 byte (+alpha) formats*/
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_RGB565 = 0x12,
|
|
|
|
LV_COLOR_FORMAT_RGB565A8 = 0x14 /**< Color array followed by Alpha array*/,
|
2022-07-21 05:17:01 +02:00
|
|
|
|
2023-02-20 20:50:58 +01:00
|
|
|
/*3 byte (+alpha) formats*/
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_RGB888 = 0x0F,
|
|
|
|
LV_COLOR_FORMAT_ARGB8888 = 0x10,
|
|
|
|
LV_COLOR_FORMAT_XRGB8888 = 0x11,
|
|
|
|
|
|
|
|
/*Miscellaneous formats*/
|
|
|
|
LV_COLOR_FORMAT_NATIVE_REVERSED = 0x1A,
|
|
|
|
|
|
|
|
/*Formats not supported by software renderer but kept here so GPU can use it*/
|
|
|
|
LV_COLOR_FORMAT_A1 = 0x0B,
|
|
|
|
LV_COLOR_FORMAT_A2 = 0x0C,
|
|
|
|
LV_COLOR_FORMAT_A4 = 0x0D,
|
2023-02-20 20:50:58 +01:00
|
|
|
|
|
|
|
/*Color formats in which LVGL can render*/
|
|
|
|
#if LV_COLOR_DEPTH == 8
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_L8,
|
2023-02-20 20:50:58 +01:00
|
|
|
#elif LV_COLOR_DEPTH == 16
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_RGB565,
|
2023-07-05 13:05:19 +02:00
|
|
|
LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_RGB565A8,
|
2023-02-20 20:50:58 +01:00
|
|
|
#elif LV_COLOR_DEPTH == 24
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_RGB888,
|
2023-07-05 13:05:19 +02:00
|
|
|
LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_ARGB8888,
|
2023-02-20 20:50:58 +01:00
|
|
|
#elif LV_COLOR_DEPTH == 32
|
2023-07-14 02:43:14 +08:00
|
|
|
LV_COLOR_FORMAT_NATIVE = LV_COLOR_FORMAT_XRGB8888,
|
2023-07-05 13:05:19 +02:00
|
|
|
LV_COLOR_FORMAT_NATIVE_WITH_ALPHA = LV_COLOR_FORMAT_ARGB8888,
|
2023-02-20 20:50:58 +01:00
|
|
|
#endif
|
2023-07-14 02:43:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef DOXYGEN
|
|
|
|
typedef _lv_color_format_t lv_color_format_t;
|
|
|
|
#else
|
|
|
|
typedef uint8_t lv_color_format_t;
|
|
|
|
#endif /*DOXYGEN*/
|
2022-07-21 05:17:01 +02:00
|
|
|
|
2023-07-14 02:43:14 +08:00
|
|
|
#define LV_COLOR_FORMAT_IS_INDEXED(cf) ((cf) >= LV_COLOR_FORMAT_I1 && (cf) <= LV_COLOR_FORMAT_I8)
|
2022-07-21 05:17:01 +02:00
|
|
|
|
2023-02-20 20:50:58 +01:00
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
2023-07-11 02:05:46 +08:00
|
|
|
#define LV_COLOR_MAKE(r8, g8, b8) {b8, g8, r8}
|
|
|
|
|
|
|
|
#define LV_OPA_MIX2(a1, a2) (((int32_t)(a1) * (a2)) >> 8)
|
|
|
|
#define LV_OPA_MIX3(a1, a2, a3) (((int32_t)(a1) * (a2) * (a3)) >> 16)
|
2023-07-05 13:05:19 +02:00
|
|
|
|
|
|
|
/**********************
|
|
|
|
* GLOBAL PROTOTYPES
|
|
|
|
**********************/
|
|
|
|
|
2023-02-20 20:50:58 +01:00
|
|
|
/**
|
|
|
|
* Get the pixel size of a color format in bits
|
2023-04-27 06:42:02 -06:00
|
|
|
* @param src_cf a color format (`LV_IMG_CF_...`)
|
2023-02-20 20:50:58 +01:00
|
|
|
* @return the pixel size in bits
|
|
|
|
*/
|
|
|
|
uint8_t lv_color_format_get_size(lv_color_format_t src_cf);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a color format has alpha channel or not
|
2023-04-27 06:42:02 -06:00
|
|
|
* @param src_cf a color format (`LV_IMG_CF_...`)
|
2023-02-20 20:50:58 +01:00
|
|
|
* @return true: has alpha channel; false: doesn't have alpha channel
|
|
|
|
*/
|
|
|
|
bool lv_color_format_has_alpha(lv_color_format_t src_cf);
|
2022-07-21 05:17:01 +02:00
|
|
|
|
2021-02-26 13:02:06 +01:00
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
lv_color32_t lv_color_to_32(lv_color_t color, lv_opa_t opa);
|
2023-02-20 20:50:58 +01:00
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
static inline uint32_t lv_color_to_int(lv_color_t c)
|
2023-02-20 20:50:58 +01:00
|
|
|
{
|
|
|
|
uint8_t * tmp = (uint8_t *) &c;
|
|
|
|
return tmp[0] + (tmp[1] << 8) + (tmp[2] << 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
static inline lv_color_t lv_color_from_int(uint32_t v)
|
2023-02-20 20:50:58 +01:00
|
|
|
{
|
2023-07-05 13:05:19 +02:00
|
|
|
void * p = (void *)&v;
|
|
|
|
return *((lv_color_t *)p);
|
2023-02-20 20:50:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool lv_color_eq(lv_color_t c1, lv_color_t c2)
|
|
|
|
{
|
|
|
|
return lv_color_to_int(c1) == lv_color_to_int(c2);
|
|
|
|
}
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
static inline bool lv_color32_eq(lv_color32_t c1, lv_color32_t c2)
|
2023-02-20 20:50:58 +01:00
|
|
|
{
|
2023-07-05 13:05:19 +02:00
|
|
|
return *((uint32_t *)&c1) == *((uint32_t *)&c2);
|
2017-11-23 20:42:14 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
static lv_color_t lv_color_hex(uint32_t c)
|
2017-11-23 20:42:14 +01:00
|
|
|
{
|
2023-07-05 13:05:19 +02:00
|
|
|
lv_color_t ret;
|
|
|
|
ret.red = (c >> 16) & 0xff;
|
|
|
|
ret.green = (c >> 8) & 0xff;
|
|
|
|
ret.blue = (c >> 0) & 0xff;
|
2023-02-20 20:50:58 +01:00
|
|
|
return ret;
|
2017-11-23 20:42:14 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
static inline lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b)
|
2017-11-23 20:42:14 +01:00
|
|
|
{
|
2023-07-05 13:05:19 +02:00
|
|
|
return lv_color_hex((r << 16) + (g << 8) + b);
|
2023-02-20 20:50:58 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
|
|
|
|
static inline lv_color_t lv_color_hex3(uint32_t c)
|
2023-02-20 20:50:58 +01:00
|
|
|
{
|
2023-07-05 13:05:19 +02:00
|
|
|
return lv_color_make((uint8_t)(((c >> 4) & 0xF0) | ((c >> 8) & 0xF)), (uint8_t)((c & 0xF0) | ((c & 0xF0) >> 4)),
|
|
|
|
(uint8_t)((c & 0xF) | ((c & 0xF) << 4)));
|
2017-11-23 20:42:14 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
uint16_t lv_color_to_u16(lv_color_t color);
|
|
|
|
uint32_t lv_color_to_u32(lv_color_t color);
|
2020-05-18 11:03:10 +02:00
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
LV_ATTRIBUTE_FAST_MEM static inline uint16_t lv_color_16_16_mix(uint16_t c1, uint16_t c2, uint8_t mix)
|
2017-11-23 20:42:14 +01:00
|
|
|
{
|
2023-07-05 13:05:19 +02:00
|
|
|
if(mix == 255) return c1;
|
|
|
|
if(mix == 0) return c2;
|
|
|
|
|
|
|
|
uint16_t ret;
|
2021-07-27 19:16:00 +02:00
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
/* Source: https://stackoverflow.com/a/50012418/1999969*/
|
2022-05-18 23:06:21 +02:00
|
|
|
mix = (uint32_t)((uint32_t)mix + 4) >> 3;
|
2023-03-06 05:57:04 +01:00
|
|
|
|
|
|
|
/*0x7E0F81F = 0b00000111111000001111100000011111*/
|
2023-07-05 13:05:19 +02:00
|
|
|
uint32_t bg = (uint32_t)(c2 | ((uint32_t)c2 << 16)) & 0x7E0F81F;
|
|
|
|
uint32_t fg = (uint32_t)(c1 | ((uint32_t)c1 << 16)) & 0x7E0F81F;
|
2021-07-27 19:16:00 +02:00
|
|
|
uint32_t result = ((((fg - bg) * mix) >> 5) + bg) & 0x7E0F81F;
|
2023-07-05 13:05:19 +02:00
|
|
|
ret = (uint16_t)(result >> 16) | result;
|
2018-02-26 15:57:40 +01:00
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-01-20 16:11:38 +01:00
|
|
|
lv_color_t lv_color_lighten(lv_color_t c, lv_opa_t lvl);
|
|
|
|
|
|
|
|
lv_color_t lv_color_darken(lv_color_t c, lv_opa_t lvl);
|
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
/**
|
|
|
|
* Convert a HSV color to RGB
|
|
|
|
* @param h hue [0..359]
|
|
|
|
* @param s saturation [0..100]
|
|
|
|
* @param v value [0..100]
|
2017-11-23 21:28:36 +01:00
|
|
|
* @return the given RGB color in RGB (with LV_COLOR_DEPTH depth)
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
2017-11-23 21:28:36 +01:00
|
|
|
lv_color_t lv_color_hsv_to_rgb(uint16_t h, uint8_t s, uint8_t v);
|
2017-11-23 20:42:14 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-22 13:07:01 -07:00
|
|
|
* Convert a 32-bit RGB color to HSV
|
|
|
|
* @param r8 8-bit red
|
|
|
|
* @param g8 8-bit green
|
|
|
|
* @param b8 8-bit blue
|
|
|
|
* @return the given RGB color in HSV
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
2019-10-22 13:07:01 -07:00
|
|
|
lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r8, uint8_t g8, uint8_t b8);
|
2017-11-23 20:42:14 +01:00
|
|
|
|
2019-10-22 13:07:01 -07:00
|
|
|
/**
|
|
|
|
* Convert a color to HSV
|
|
|
|
* @param color color
|
|
|
|
* @return the given color in HSV
|
2017-11-23 20:42:14 +01:00
|
|
|
*/
|
2019-10-22 13:02:31 -07:00
|
|
|
lv_color_hsv_t lv_color_to_hsv(lv_color_t color);
|
2017-11-23 20:42:14 +01:00
|
|
|
|
2021-03-15 02:03:27 +08:00
|
|
|
/*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/
|
2021-02-23 15:03:06 +01:00
|
|
|
|
2021-09-13 14:04:16 +02:00
|
|
|
static inline lv_color_t lv_color_white(void)
|
|
|
|
{
|
|
|
|
return lv_color_make(0xff, 0xff, 0xff);
|
|
|
|
}
|
|
|
|
static inline lv_color_t lv_color_black(void)
|
|
|
|
{
|
2023-02-20 20:50:58 +01:00
|
|
|
return lv_color_make(0x00, 0x00, 0x00);
|
2021-09-13 14:04:16 +02:00
|
|
|
}
|
2021-05-13 16:04:32 +02:00
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
/**********************
|
|
|
|
* MACROS
|
|
|
|
**********************/
|
|
|
|
|
2023-07-05 13:05:19 +02:00
|
|
|
#include "lv_palette.h"
|
|
|
|
#include "lv_color_op.h"
|
|
|
|
|
2017-11-23 20:42:14 +01:00
|
|
|
#ifdef __cplusplus
|
2021-03-15 02:03:27 +08:00
|
|
|
} /*extern "C"*/
|
2017-11-23 20:42:14 +01:00
|
|
|
#endif
|
2017-11-26 23:57:39 +01:00
|
|
|
|
2021-01-18 00:13:42 -08:00
|
|
|
#endif /*LV_COLOR_H*/
|