1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/src/misc/lv_color.h

688 lines
22 KiB
C
Raw Normal View History

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
*********************/
#include "../lv_conf_internal.h"
#include "lv_math.h"
#include "lv_types.h"
2017-11-23 20:42:14 +01:00
/*Error checking*/
#if LV_COLOR_DEPTH == 24
#error "LV_COLOR_DEPTH 24 is deprecated. Use LV_COLOR_DEPTH 32 instead (lv_conf.h)"
#endif
#if LV_COLOR_DEPTH != 32 && LV_COLOR_SCREEN_TRANSP != 0
#error "LV_COLOR_SCREEN_TRANSP requires LV_COLOR_DEPTH == 32. Set it in lv_conf.h"
#endif
#if LV_COLOR_DEPTH != 16 && LV_COLOR_16_SWAP != 0
#error "LV_COLOR_16_SWAP requires LV_COLOR_DEPTH == 16. Set it in lv_conf.h"
#endif
2017-11-23 20:42:14 +01:00
#include <stdint.h>
/*********************
* DEFINES
*********************/
2017-11-23 21:28:36 +01:00
2019-06-27 18:07:26 -04:00
/**
* Opacity percentages.
*/
2019-01-14 15:08:54 +01:00
enum {
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
#define LV_OPA_MIN 2 /*Opacities below this will be transparent*/
#define LV_OPA_MAX 253 /*Opacities above this will fully cover*/
#if LV_COLOR_DEPTH == 1
2019-04-04 07:15:40 +02:00
#define LV_COLOR_SIZE 8
#elif LV_COLOR_DEPTH == 8
2019-04-04 07:15:40 +02:00
#define LV_COLOR_SIZE 8
#elif LV_COLOR_DEPTH == 16
2019-04-04 07:15:40 +02:00
#define LV_COLOR_SIZE 16
#elif LV_COLOR_DEPTH == 32
2019-04-04 07:15:40 +02:00
#define LV_COLOR_SIZE 32
#else
#error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!"
#endif
2017-11-23 20:42:14 +01:00
/**
* Adjust color mix functions rounding.
* GPUs might calculate color mix (blending) differently.
* Should be in range of 0..254
* 0: no adjustment, get the integer part of the result (round down)
* 64: round up from x.75
* 128: round up from half
* 192: round up from x.25
* 254: round up*/
#ifndef LV_COLOR_MIX_ROUND_OFS
#if LV_COLOR_DEPTH == 32
#define LV_COLOR_MIX_ROUND_OFS 0
#else
#define LV_COLOR_MIX_ROUND_OFS 128
#endif
#endif
#if defined(__cplusplus) && !defined(_LV_COLOR_HAS_MODERN_CPP)
/**
* MSVC compiler's definition of the __cplusplus indicating 199711L regardless to C++ standard version
* see https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-cplusplus
* so we use _MSC_VER macro instead of __cplusplus
*/
#ifdef _MSC_VER
#if _MSC_VER >= 1900 /*Visual Studio 2015*/
#define _LV_COLOR_HAS_MODERN_CPP 1
#endif
#else
#if __cplusplus >= 201103L
#define _LV_COLOR_HAS_MODERN_CPP 1
#endif
#endif
#endif /*__cplusplus*/
#ifndef _LV_COLOR_HAS_MODERN_CPP
#define _LV_COLOR_HAS_MODERN_CPP 0
#endif
#if _LV_COLOR_HAS_MODERN_CPP
/*Fix msvc compiler error C4576 inside C++ code*/
#define _LV_COLOR_MAKE_TYPE_HELPER lv_color_t
#else
#define _LV_COLOR_MAKE_TYPE_HELPER (lv_color_t)
#endif
/*---------------------------------------
* Macros for all existing color depths
* to set/get values of the color channels
*------------------------------------------*/
# define LV_COLOR_SET_R1(c, v) (c).ch.red = (uint8_t)((v) & 0x1)
# define LV_COLOR_SET_G1(c, v) (c).ch.green = (uint8_t)((v) & 0x1)
# define LV_COLOR_SET_B1(c, v) (c).ch.blue = (uint8_t)((v) & 0x1)
# define LV_COLOR_SET_A1(c, v) do {} while(0)
# define LV_COLOR_GET_R1(c) (c).ch.red
# define LV_COLOR_GET_G1(c) (c).ch.green
# define LV_COLOR_GET_B1(c) (c).ch.blue
# define LV_COLOR_GET_A1(c) 0xFF
# define _LV_COLOR_ZERO_INITIALIZER1 {0x00}
2021-04-24 17:43:33 +02:00
# define LV_COLOR_MAKE1(r8, g8, b8) {(uint8_t)((b8 >> 7) | (g8 >> 7) | (r8 >> 7))}
# define LV_COLOR_SET_R8(c, v) (c).ch.red = (uint8_t)((v) & 0x7U)
# define LV_COLOR_SET_G8(c, v) (c).ch.green = (uint8_t)((v) & 0x7U)
# define LV_COLOR_SET_B8(c, v) (c).ch.blue = (uint8_t)((v) & 0x3U)
2019-11-14 20:38:47 -05:00
# define LV_COLOR_SET_A8(c, v) do {} while(0)
# define LV_COLOR_GET_R8(c) (c).ch.red
# define LV_COLOR_GET_G8(c) (c).ch.green
# define LV_COLOR_GET_B8(c) (c).ch.blue
# define LV_COLOR_GET_A8(c) 0xFF
2021-04-24 17:43:33 +02:00
# define _LV_COLOR_ZERO_INITIALIZER8 _LV_COLOR_MAKE_TYPE_HELPER {{0x00, 0x00, 0x00}}
# define LV_COLOR_MAKE8(r8, g8, b8) _LV_COLOR_MAKE_TYPE_HELPER {{(uint8_t)((b8 >> 6) & 0x3U), (uint8_t)((g8 >> 5) & 0x7U), (uint8_t)((r8 >> 5) & 0x7U)}}
# define LV_COLOR_SET_R16(c, v) (c).ch.red = (uint8_t)((v) & 0x1FU)
#if LV_COLOR_16_SWAP == 0
# define LV_COLOR_SET_G16(c, v) (c).ch.green = (uint8_t)((v) & 0x3FU)
#else
# define LV_COLOR_SET_G16(c, v) {(c).ch.green_h = (uint8_t)(((v) >> 3) & 0x7); (c).ch.green_l = (uint8_t)((v) & 0x7);}
#endif
# define LV_COLOR_SET_B16(c, v) (c).ch.blue = (uint8_t)((v) & 0x1FU)
2019-11-14 20:38:47 -05:00
# define LV_COLOR_SET_A16(c, v) do {} while(0)
# define LV_COLOR_GET_R16(c) (c).ch.red
#if LV_COLOR_16_SWAP == 0
# define LV_COLOR_GET_G16(c) (c).ch.green
#else
# define LV_COLOR_GET_G16(c) (((c).ch.green_h << 3) + (c).ch.green_l)
#endif
# define LV_COLOR_GET_B16(c) (c).ch.blue
# define LV_COLOR_GET_A16(c) 0xFF
#if LV_COLOR_16_SWAP == 0
2021-04-24 17:43:33 +02:00
# define _LV_COLOR_ZERO_INITIALIZER16 {{0x00, 0x00, 0x00}}
# define LV_COLOR_MAKE16(r8, g8, b8) {{(uint8_t)((b8 >> 3) & 0x1FU), (uint8_t)((g8 >> 2) & 0x3FU), (uint8_t)((r8 >> 3) & 0x1FU)}}
#else
# define _LV_COLOR_ZERO_INITIALIZER16 {{0x00, 0x00, 0x00, 0x00}}
2021-04-24 17:43:33 +02:00
# 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)}}
#endif
# define LV_COLOR_SET_R32(c, v) (c).ch.red = (uint8_t)((v) & 0xFF)
# define LV_COLOR_SET_G32(c, v) (c).ch.green = (uint8_t)((v) & 0xFF)
# define LV_COLOR_SET_B32(c, v) (c).ch.blue = (uint8_t)((v) & 0xFF)
# define LV_COLOR_SET_A32(c, v) (c).ch.alpha = (uint8_t)((v) & 0xFF)
# define LV_COLOR_GET_R32(c) (c).ch.red
# define LV_COLOR_GET_G32(c) (c).ch.green
# define LV_COLOR_GET_B32(c) (c).ch.blue
# define LV_COLOR_GET_A32(c) (c).ch.alpha
2021-04-24 17:43:33 +02:00
# define _LV_COLOR_ZERO_INITIALIZER32 {{0x00, 0x00, 0x00, 0x00}}
# define LV_COLOR_MAKE32(r8, g8, b8) {{b8, g8, r8, 0xff}} /*Fix 0xff alpha*/
/*---------------------------------------
* Macros for the current color depth
* to set/get values of the color channels
*------------------------------------------*/
#define LV_COLOR_SET_R(c, v) LV_CONCAT(LV_COLOR_SET_R, LV_COLOR_DEPTH)(c, v)
#define LV_COLOR_SET_G(c, v) LV_CONCAT(LV_COLOR_SET_G, LV_COLOR_DEPTH)(c, v)
#define LV_COLOR_SET_B(c, v) LV_CONCAT(LV_COLOR_SET_B, LV_COLOR_DEPTH)(c, v)
#define LV_COLOR_SET_A(c, v) LV_CONCAT(LV_COLOR_SET_A, LV_COLOR_DEPTH)(c, v)
#define LV_COLOR_GET_R(c) LV_CONCAT(LV_COLOR_GET_R, LV_COLOR_DEPTH)(c)
#define LV_COLOR_GET_G(c) LV_CONCAT(LV_COLOR_GET_G, LV_COLOR_DEPTH)(c)
#define LV_COLOR_GET_B(c) LV_CONCAT(LV_COLOR_GET_B, LV_COLOR_DEPTH)(c)
#define LV_COLOR_GET_A(c) LV_CONCAT(LV_COLOR_GET_A, LV_COLOR_DEPTH)(c)
#define _LV_COLOR_ZERO_INITIALIZER LV_CONCAT(_LV_COLOR_ZERO_INITIALIZER, LV_COLOR_DEPTH)
#define LV_COLOR_MAKE(r8, g8, b8) LV_CONCAT(LV_COLOR_MAKE, LV_COLOR_DEPTH)(r8, g8, b8)
2021-01-23 20:46:42 +01:00
#define LV_UDIV255(x) ((uint32_t)((uint32_t) (x) * 0x8081) >> 0x17)
2017-11-23 20:42:14 +01:00
/**********************
* TYPEDEFS
**********************/
2020-02-26 19:48:27 +01:00
typedef union {
uint8_t full; /*must be declared first to set all bits of byte via initializer list*/
2020-05-01 16:54:13 +02:00
union {
uint8_t blue : 1;
uint8_t green : 1;
uint8_t red : 1;
} ch;
2018-06-19 09:49:58 +02:00
} lv_color1_t;
2017-11-23 20:42:14 +01:00
2020-02-26 19:48:27 +01:00
typedef union {
struct {
2019-04-04 07:15:40 +02:00
uint8_t blue : 2;
uint8_t green : 3;
uint8_t red : 3;
} ch;
2017-11-23 20:42:14 +01:00
uint8_t full;
2018-06-19 09:49:58 +02:00
} lv_color8_t;
2017-11-23 20:42:14 +01:00
2020-02-26 19:48:27 +01:00
typedef union {
struct {
2018-08-07 08:20:34 +02:00
#if LV_COLOR_16_SWAP == 0
2019-04-04 07:15:40 +02:00
uint16_t blue : 5;
uint16_t green : 6;
uint16_t red : 5;
2018-08-07 08:20:34 +02:00
#else
2019-04-04 07:15:40 +02:00
uint16_t green_h : 3;
uint16_t red : 5;
uint16_t blue : 5;
uint16_t green_l : 3;
2018-08-07 08:20:34 +02:00
#endif
2019-04-04 07:15:40 +02:00
} ch;
2017-11-23 20:42:14 +01:00
uint16_t full;
2018-06-19 09:49:58 +02:00
} lv_color16_t;
2017-11-23 20:42:14 +01:00
2020-02-26 19:48:27 +01:00
typedef union {
struct {
2017-11-23 20:42:14 +01:00
uint8_t blue;
uint8_t green;
uint8_t red;
uint8_t alpha;
2019-04-04 07:15:40 +02:00
} ch;
2017-11-23 20:42:14 +01:00
uint32_t full;
} lv_color32_t;
2017-11-23 20:42:14 +01:00
typedef LV_CONCAT3(uint, LV_COLOR_SIZE, _t) lv_color_int_t;
typedef LV_CONCAT3(lv_color, LV_COLOR_DEPTH, _t) lv_color_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
2020-04-17 08:58:34 +02:00
//! @cond Doxygen_Suppress
/*No idea where the guard is required but else throws warnings in the docs*/
typedef uint8_t lv_opa_t;
//! @endcond
struct _lv_color_filter_dsc_t;
2020-12-19 06:51:14 +01:00
typedef lv_color_t (*lv_color_filter_cb_t)(const struct _lv_color_filter_dsc_t *, lv_color_t, lv_opa_t);
typedef struct _lv_color_filter_dsc_t {
lv_color_filter_cb_t filter_cb;
void * user_data;
}lv_color_filter_dsc_t;
2020-12-19 06:51:14 +01:00
typedef enum {
LV_PALETTE_RED,
LV_PALETTE_PINK,
LV_PALETTE_PURPLE,
LV_PALETTE_DEEP_PURPLE,
LV_PALETTE_INDIGO,
LV_PALETTE_BLUE,
LV_PALETTE_LIGHT_BLUE,
LV_PALETTE_CYAN,
LV_PALETTE_TEAL,
LV_PALETTE_GREEN,
LV_PALETTE_LIGHT_GREEN,
LV_PALETTE_LIME,
LV_PALETTE_YELLOW,
LV_PALETTE_AMBER,
LV_PALETTE_ORANGE,
LV_PALETTE_DEEP_ORANGE,
LV_PALETTE_BROWN,
LV_PALETTE_BLUE_GREY,
LV_PALETTE_GREY,
_LV_PALETTE_LAST,
LV_PALETTE_NONE = 0xff,
}lv_palette_t;
2017-11-23 20:42:14 +01:00
/**********************
* GLOBAL PROTOTYPES
**********************/
/*In color conversations:
2018-06-19 09:49:58 +02:00
* - When converting to bigger color type the LSB weight of 1 LSB is calculated
2017-11-23 20:42:14 +01:00
* E.g. 16 bit Red has 5 bits
* 8 bit Red has 3 bits
2017-11-23 20:42:14 +01:00
* ----------------------
* 8 bit red LSB = (2^5 - 1) / (2^3 - 1) = 31 / 7 = 4
2018-06-19 09:49:58 +02:00
*
2017-11-23 20:42:14 +01:00
* - When calculating to smaller color type simply shift out the LSBs
* E.g. 8 bit Red has 3 bits
2017-11-23 20:42:14 +01:00
* 16 bit Red has 5 bits
* ----------------------
* Shift right with 5 - 3 = 2
*/
2017-11-23 21:28:36 +01:00
static inline uint8_t lv_color_to1(lv_color_t color)
2017-11-23 20:42:14 +01:00
{
2017-11-23 21:28:36 +01:00
#if LV_COLOR_DEPTH == 1
2018-06-19 09:49:58 +02:00
return color.full;
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 8
if((LV_COLOR_GET_R(color) & 0x4) || (LV_COLOR_GET_G(color) & 0x4) || (LV_COLOR_GET_B(color) & 0x2)) {
2018-06-19 09:49:58 +02:00
return 1;
2020-02-26 19:48:27 +01:00
}
else {
2018-06-19 09:49:58 +02:00
return 0;
2017-11-23 20:42:14 +01:00
}
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 16
if((LV_COLOR_GET_R(color) & 0x10) || (LV_COLOR_GET_G(color) & 0x20) || (LV_COLOR_GET_B(color) & 0x10)) {
2018-10-05 17:22:49 +02:00
return 1;
2020-02-26 19:48:27 +01:00
}
else {
2018-06-19 09:49:58 +02:00
return 0;
2017-11-23 20:42:14 +01:00
}
#elif LV_COLOR_DEPTH == 32
if((LV_COLOR_GET_R(color) & 0x80) || (LV_COLOR_GET_G(color) & 0x80) || (LV_COLOR_GET_B(color) & 0x80)) {
2018-06-19 09:49:58 +02:00
return 1;
2020-02-26 19:48:27 +01:00
}
else {
2018-06-19 09:49:58 +02:00
return 0;
2017-11-23 20:42:14 +01:00
}
#endif
}
2017-11-23 21:28:36 +01:00
static inline uint8_t lv_color_to8(lv_color_t color)
2017-11-23 20:42:14 +01:00
{
2017-11-23 21:28:36 +01:00
#if LV_COLOR_DEPTH == 1
2019-04-04 07:15:40 +02:00
if(color.full == 0)
return 0;
else
return 0xFF;
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 8
2017-11-23 20:42:14 +01:00
return color.full;
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 16
lv_color8_t ret;
LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 2); /*5 - 3 = 2*/
LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 3); /*6 - 3 = 3*/
LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 3); /*5 - 2 = 3*/
2017-11-23 20:42:14 +01:00
return ret.full;
#elif LV_COLOR_DEPTH == 32
2017-11-23 21:28:36 +01:00
lv_color8_t ret;
LV_COLOR_SET_R8(ret, LV_COLOR_GET_R(color) >> 5); /*8 - 3 = 5*/
LV_COLOR_SET_G8(ret, LV_COLOR_GET_G(color) >> 5); /*8 - 3 = 5*/
LV_COLOR_SET_B8(ret, LV_COLOR_GET_B(color) >> 6); /*8 - 2 = 6*/
2017-11-23 20:42:14 +01:00
return ret.full;
#endif
}
2017-11-23 21:28:36 +01:00
static inline uint16_t lv_color_to16(lv_color_t color)
2017-11-23 20:42:14 +01:00
{
2017-11-23 21:28:36 +01:00
#if LV_COLOR_DEPTH == 1
2019-04-04 07:15:40 +02:00
if(color.full == 0)
return 0;
else
return 0xFFFF;
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 8
lv_color16_t ret;
LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) * 4); /*(2^5 - 1)/(2^3 - 1) = 31/7 = 4*/
LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) * 9); /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) * 10); /*(2^5 - 1)/(2^2 - 1) = 31/3 = 10*/
2017-11-23 20:42:14 +01:00
return ret.full;
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 16
2017-11-23 20:42:14 +01:00
return color.full;
#elif LV_COLOR_DEPTH == 32
2017-11-23 21:28:36 +01:00
lv_color16_t ret;
LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) >> 3); /*8 - 5 = 3*/
LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) >> 2); /*8 - 6 = 2*/
LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) >> 3); /*8 - 5 = 3*/
2017-11-23 20:42:14 +01:00
return ret.full;
2020-02-26 19:48:27 +01:00
#endif
2017-11-23 20:42:14 +01:00
}
static inline uint32_t lv_color_to32(lv_color_t color)
2017-11-23 20:42:14 +01:00
{
2017-11-23 21:28:36 +01:00
#if LV_COLOR_DEPTH == 1
2019-04-04 07:15:40 +02:00
if(color.full == 0)
2020-07-17 11:41:49 +02:00
return 0xFF000000;
2019-04-04 07:15:40 +02:00
else
return 0xFFFFFFFF;
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 8
lv_color32_t ret;
LV_COLOR_SET_R32(ret, LV_COLOR_GET_R(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
LV_COLOR_SET_G32(ret, LV_COLOR_GET_G(color) * 36); /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
LV_COLOR_SET_B32(ret, LV_COLOR_GET_B(color) * 85); /*(2^8 - 1)/(2^2 - 1) = 255/3 = 85*/
LV_COLOR_SET_A32(ret, 0xFF);
2017-11-23 20:42:14 +01:00
return ret.full;
2017-11-23 21:28:36 +01:00
#elif LV_COLOR_DEPTH == 16
/**
2019-10-19 05:17:22 -07:00
* The floating point math for conversion is:
* valueto = valuefrom * ( (2^bitsto - 1) / (float)(2^bitsfrom - 1) )
* The faster integer math for conversion is:
* valueto = ( valuefrom * multiplier + adder ) >> divisor
* multiplier = FLOOR( ( (2^bitsto - 1) << divisor ) / (float)(2^bitsfrom - 1) )
2020-02-26 19:48:27 +01:00
*
2019-10-19 05:26:18 -07:00
* Find the first divisor where ( adder >> divisor ) <= 0
2020-02-26 19:48:27 +01:00
*
2019-10-19 05:17:22 -07:00
* 5-bit to 8-bit: ( 31 * multiplier + adder ) >> divisor = 255
* divisor multiplier adder min (0) max (31)
* 0 8 7 7 255
* 1 16 14 7 255
* 2 32 28 7 255
* 3 65 25 3 255
* 4 131 19 1 255
* 5 263 7 0 255
2020-02-26 19:48:27 +01:00
*
2019-10-19 05:17:22 -07:00
* 6-bit to 8-bit: 255 = ( 63 * multiplier + adder ) >> divisor
* divisor multiplier adder min (0) max (63)
* 0 4 3 3 255
* 1 8 6 3 255
* 2 16 12 3 255
* 3 32 24 3 255
* 4 64 48 3 255
* 5 129 33 1 255
* 6 259 3 0 255
*/
2020-04-17 08:58:34 +02:00
lv_color32_t ret;
2020-02-26 19:48:27 +01:00
LV_COLOR_SET_R32(ret, (LV_COLOR_GET_R(color) * 263 + 7) >> 5);
LV_COLOR_SET_G32(ret, (LV_COLOR_GET_G(color) * 259 + 3) >> 6);
LV_COLOR_SET_B32(ret, (LV_COLOR_GET_B(color) * 263 + 7) >> 5);
LV_COLOR_SET_A32(ret, 0xFF);
2018-08-07 08:20:34 +02:00
return ret.full;
#elif LV_COLOR_DEPTH == 32
2017-11-23 20:42:14 +01:00
return color.full;
#endif
}
2020-05-18 11:03:10 +02:00
//! @cond Doxygen_Suppress
2019-10-02 07:02:36 +02:00
/**
* Mix two colors with a given ratio.
2020-04-17 08:58:34 +02:00
* @param c1 the first color to mix (usually the foreground)
* @param c2 the second color to mix (usually the background)
2019-10-02 07:02:36 +02:00
* @param mix The ratio of the colors. 0: full `c2`, 255: full `c1`, 127: half `c1` and half`c2`
* @return the mixed color
*/
2020-04-29 08:38:59 +02:00
LV_ATTRIBUTE_FAST_MEM static inline lv_color_t lv_color_mix(lv_color_t c1, lv_color_t c2, uint8_t mix)
2017-11-23 20:42:14 +01:00
{
2017-11-23 21:28:36 +01:00
lv_color_t ret;
#if LV_COLOR_DEPTH != 1
/*LV_COLOR_DEPTH == 8, 16 or 32*/
2021-01-23 20:46:42 +01:00
LV_COLOR_SET_R(ret, LV_UDIV255((uint16_t) LV_COLOR_GET_R(c1) * mix + LV_COLOR_GET_R(c2) *
2020-06-16 13:47:04 +02:00
(255 - mix) + LV_COLOR_MIX_ROUND_OFS));
2021-01-23 20:46:42 +01:00
LV_COLOR_SET_G(ret, LV_UDIV255((uint16_t) LV_COLOR_GET_G(c1) * mix + LV_COLOR_GET_G(c2) *
2020-06-16 13:47:04 +02:00
(255 - mix) + LV_COLOR_MIX_ROUND_OFS));
2021-01-23 20:46:42 +01:00
LV_COLOR_SET_B(ret, LV_UDIV255((uint16_t) LV_COLOR_GET_B(c1) * mix + LV_COLOR_GET_B(c2) *
2020-06-16 13:47:04 +02:00
(255 - mix) + LV_COLOR_MIX_ROUND_OFS));
LV_COLOR_SET_A(ret, 0xFF);
#else
/*LV_COLOR_DEPTH == 1*/
ret.full = mix > LV_OPA_50 ? c1.full : c2.full;
#endif
2017-11-23 20:42:14 +01:00
return ret;
}
2020-04-29 08:38:59 +02:00
LV_ATTRIBUTE_FAST_MEM static inline void lv_color_premult(lv_color_t c, uint8_t mix, uint16_t * out)
2020-03-19 15:26:55 +01:00
{
#if LV_COLOR_DEPTH != 1
2020-03-24 10:13:52 +01:00
out[0] = (uint16_t) LV_COLOR_GET_R(c) * mix;
out[1] = (uint16_t) LV_COLOR_GET_G(c) * mix;
out[2] = (uint16_t) LV_COLOR_GET_B(c) * mix;
2020-03-19 15:26:55 +01:00
#else
2020-05-24 13:13:07 +02:00
(void) mix;
2020-03-24 10:13:52 +01:00
/*Pre-multiplication can't be used with 1 bpp*/
out[0] = LV_COLOR_GET_R(c);
out[1] = LV_COLOR_GET_G(c);
out[2] = LV_COLOR_GET_B(c);
2020-03-19 15:26:55 +01:00
#endif
}
/**
* Mix two colors with a given ratio. It runs faster then `lv_color_mix` but requires some pre computation.
* @param premult_c1 The first color. Should be preprocessed with `lv_color_premult(c1)`
2020-03-19 15:26:55 +01:00
* @param c2 The second color. As it is no pre computation required on it
* @param mix The ratio of the colors. 0: full `c1`, 255: full `c2`, 127: half `c1` and half `c2`.
2020-03-19 15:26:55 +01:00
* Should be modified like mix = `255 - mix`
* @return the mixed color
* @note 255 won't give clearly `c1`.
*/
2020-04-29 08:38:59 +02:00
LV_ATTRIBUTE_FAST_MEM static inline lv_color_t lv_color_mix_premult(uint16_t * premult_c1, lv_color_t c2, uint8_t mix)
2020-03-19 15:26:55 +01:00
{
lv_color_t ret;
#if LV_COLOR_DEPTH != 1
/*LV_COLOR_DEPTH == 8, 16 or 32*/
2021-01-23 23:50:00 +01:00
LV_COLOR_SET_R(ret, LV_UDIV255(premult_c1[0] + LV_COLOR_GET_R(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
LV_COLOR_SET_G(ret, LV_UDIV255(premult_c1[1] + LV_COLOR_GET_G(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
LV_COLOR_SET_B(ret, LV_UDIV255(premult_c1[2] + LV_COLOR_GET_B(c2) * mix + LV_COLOR_MIX_ROUND_OFS));
2020-03-19 15:26:55 +01:00
LV_COLOR_SET_A(ret, 0xFF);
#else
/*LV_COLOR_DEPTH == 1*/
/*Restore color1*/
lv_color_t c1;
LV_COLOR_SET_R(c1, premult_c1[0]);
LV_COLOR_SET_G(c1, premult_c1[1]);
LV_COLOR_SET_B(c1, premult_c1[2]);
ret.full = mix > LV_OPA_50 ? c2.full : c1.full;
2020-03-19 15:26:55 +01:00
#endif
return ret;
}
/**
* Mix two colors. Both color can have alpha value.
* @param bg_color background color
* @param bg_opa alpha of the background color
* @param fg_color foreground color
* @param fg_opa alpha of the foreground color
* @param res_color the result color
* @param res_opa the result opacity
*/
2020-05-01 11:17:43 +02:00
LV_ATTRIBUTE_FAST_MEM static inline void 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)
{
/*Pick the foreground if it's fully opaque or the Background is fully transparent*/
2020-03-25 16:14:30 +01:00
if(fg_opa >= LV_OPA_MAX || bg_opa <= LV_OPA_MIN) {
res_color->full = fg_color.full;
*res_opa = fg_opa;
}
/*Transparent foreground: use the Background*/
else if(fg_opa <= LV_OPA_MIN) {
res_color->full = bg_color.full;
*res_opa = bg_opa;
}
/*Opaque background: use simple mix*/
else if(bg_opa >= LV_OPA_MAX) {
*res_color = lv_color_mix(fg_color, bg_color, fg_opa);
*res_opa = LV_OPA_COVER;
}
/*Both colors have alpha. Expensive calculation need to be applied*/
else {
/*Save the parameters and the result. If they will be asked again don't compute again*/
static lv_opa_t fg_opa_save = 0;
static lv_opa_t bg_opa_save = 0;
static lv_color_t fg_color_save = _LV_COLOR_ZERO_INITIALIZER;
static lv_color_t bg_color_save = _LV_COLOR_ZERO_INITIALIZER;
static lv_color_t res_color_saved = _LV_COLOR_ZERO_INITIALIZER;
static lv_opa_t res_opa_saved = 0;
if(fg_opa != fg_opa_save || bg_opa != bg_opa_save || fg_color.full != fg_color_save.full ||
2020-02-26 19:48:27 +01:00
bg_color.full != bg_color_save.full) {
fg_opa_save = fg_opa;
bg_opa_save = bg_opa;
fg_color_save.full = fg_color.full;
bg_color_save.full = bg_color.full;
/*Info:
* https://en.wikipedia.org/wiki/Alpha_compositing#Analytical_derivation_of_the_over_operator*/
res_opa_saved = 255 - ((uint16_t)((uint16_t)(255 - fg_opa) * (255 - bg_opa)) >> 8);
if(res_opa_saved == 0) {
while(1)
;
}
lv_opa_t ratio = (uint16_t)((uint16_t)fg_opa * 255) / res_opa_saved;
res_color_saved = lv_color_mix(fg_color, bg_color, ratio);
}
res_color->full = res_color_saved.full;
*res_opa = res_opa_saved;
}
}
2020-05-18 11:03:10 +02:00
//! @endcond
2017-11-23 20:42:14 +01:00
/**
* Get the brightness of a color
* @param color a color
* @return the brightness [0..255]
*/
2018-06-19 09:49:58 +02:00
static inline uint8_t lv_color_brightness(lv_color_t color)
2017-11-23 20:42:14 +01:00
{
lv_color32_t c32;
2019-04-04 07:15:40 +02:00
c32.full = lv_color_to32(color);
2019-11-14 20:38:47 -05:00
uint16_t bright = (uint16_t)(3u * LV_COLOR_GET_R32(c32) + LV_COLOR_GET_B32(c32) + 4u * LV_COLOR_GET_G32(c32));
return (uint8_t)(bright >> 3);
2017-11-23 20:42:14 +01:00
}
2019-11-25 06:42:11 +01:00
static inline lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b)
2019-04-04 07:15:40 +02:00
{
2021-04-24 17:43:33 +02:00
return _LV_COLOR_MAKE_TYPE_HELPER LV_COLOR_MAKE(r, g, b);
}
2019-04-04 07:15:40 +02:00
static inline lv_color_t lv_color_hex(uint32_t c)
{
2019-06-06 06:05:40 +02:00
return lv_color_make((uint8_t)((c >> 16) & 0xFF), (uint8_t)((c >> 8) & 0xFF), (uint8_t)(c & 0xFF));
}
2019-04-04 07:15:40 +02:00
static inline lv_color_t lv_color_hex3(uint32_t c)
{
2019-06-06 06:05:40 +02:00
return lv_color_make((uint8_t)(((c >> 4) & 0xF0) | ((c >> 8) & 0xF)), (uint8_t)((c & 0xF0) | ((c & 0xF0) >> 4)),
2019-04-04 07:15:40 +02:00
(uint8_t)((c & 0xF) | ((c & 0xF) << 4)));
}
2017-11-23 20:42:14 +01:00
static inline void lv_color_filter_dsc_init(lv_color_filter_dsc_t * dsc, lv_color_filter_cb_t cb)
{
dsc->filter_cb = cb;
}
2020-05-18 11:03:10 +02:00
//! @cond Doxygen_Suppress
//!
LV_ATTRIBUTE_FAST_MEM void lv_color_fill(lv_color_t * buf, lv_color_t color, uint32_t px_num);
2020-04-16 11:12:20 +02:00
2020-05-18 11:03:10 +02:00
//! @endcond
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);
2020-12-19 06:51:14 +01:00
lv_color_t lv_color_change_lightness(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
*/
lv_color_hsv_t lv_color_to_hsv(lv_color_t color);
2017-11-23 20:42:14 +01:00
/**
* Just a wrapper around LV_COLOR_CHROMA_KEY because it might be more convenient to use a function is some cases
* @return LV_COLOR_CHROMA_KEY
*/
static inline lv_color_t lv_color_chroma_key(void)
{
return LV_COLOR_CHROMA_KEY;
}
/**********************
* PREDEFINED COLORS
**********************/
/*Source: https://vuetifyjs.com/en/styles/colors/#material-colors*/
lv_color_t lv_palette_main(lv_palette_t p);
2021-04-24 17:43:33 +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) { return lv_color_make(0x00, 0x0, 0x00);}
lv_color_t lv_palette_lighten(lv_palette_t p, uint8_t lvl);
lv_color_t lv_palette_darken(lv_palette_t p, uint8_t lvl);
2017-11-23 20:42:14 +01:00
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /*extern "C"*/
2017-11-23 20:42:14 +01:00
#endif
2017-11-26 23:57:39 +01:00
#endif /*LV_COLOR_H*/