add LV_COLOR_SET/GET for every color depth configuration

This commit is contained in:
Gabor Kiss-Vamosi
2019-11-12 06:39:26 +01:00
parent f3b88a57ca
commit 49e105917e

View File

@@ -90,53 +90,108 @@ enum {
#error "Invalid LV_COLOR_DEPTH in lv_conf.h! Set it to 1, 8, 16 or 32!"
#endif
/*-----------------------
* Macros to set/get values
* of the color channels
*-----------------------*/
#if LV_COLOR_DEPTH == 1
# define LV_COLOR_SET_R(c, v) (c).ch.red = v & 0x1;
# define LV_COLOR_SET_G(c, v) (c).ch.green = v & 0x1;
# define LV_COLOR_SET_B(c, v) (c).ch.blue = v & 0x1;
/*---------------------------------------
* Macros for all existing color depths
* to set/get values of the color channels
*------------------------------------------*/
# define LV_COLOR_SET_R1(c, v) (c).ch.red = (v) & 0x1;
# define LV_COLOR_SET_G1(c, v) (c).ch.green = (v) & 0x1;
# define LV_COLOR_SET_B1(c, v) (c).ch.blue = (v) & 0x1;
# define LV_COLOR_SET_A1(c, v)
# define LV_COLOR_GET_R(c) (c).ch.red
# define LV_COLOR_GET_G(c) (c).ch.green
# define LV_COLOR_GET_B(c) (c).ch.blue
# 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) 1
# define LV_COLOR_SET_R8(c, v) (c).ch.red = (v) & 0x7;
# define LV_COLOR_SET_G8(c, v) (c).ch.green = (v) & 0x7;
# define LV_COLOR_SET_B8(c, v) (c).ch.blue = (v) & 0x3;
# define LV_COLOR_SET_A8(c, v)
# 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
# define LV_COLOR_SET_R16(c, v) (c).ch.red = (v) & 0x1F;
# define LV_COLOR_SET_G16(c, v) (c).ch.green = (v) & 0x3F;
# define LV_COLOR_SET_G16_SWAP(c, v) {(c).ch.green_h = ((v) >> 3) & 0x7; (c).ch.green_l = (v) & 0x7;}
# define LV_COLOR_SET_B16(c, v) (c).ch.blue = (v) & 0x1F;
# define LV_COLOR_SET_A16(c, v)
# define LV_COLOR_GET_R16(c) (c).ch.red
# define LV_COLOR_GET_G16(c) (c).ch.green
# define LV_COLOR_GET_G16_SWAP(c) (((c).ch.green_h << 3) + (c).ch.green_l)
# define LV_COLOR_GET_B16(c) (c).ch.blue
# define LV_COLOR_GET_A16(c) 0xFF
# define LV_COLOR_SET_R32(c, v) (c).ch.red = (v) & 0xFF;
# define LV_COLOR_SET_G32(c, v) (c).ch.green = (v) & 0xFF;
# define LV_COLOR_SET_B32(c, v) (c).ch.blue = (v) & 0xFF;
# define LV_COLOR_SET_A32(c, v) (c).ch.alpha = (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
/*---------------------------------------
* Macros for the current color depth
* to set/get values of the color channels
*------------------------------------------*/
#if LV_COLOR_DEPTH == 1
# define LV_COLOR_SET_R(c, v) LV_COLOR_SET_R1(c,v)
# define LV_COLOR_SET_G(c, v) LV_COLOR_SET_G1(c,v)
# define LV_COLOR_SET_B(c, v) LV_COLOR_SET_B1(c,v)
# define LV_COLOR_SET_A(c, v) LV_COLOR_SET_A1(c,v)
# define LV_COLOR_GET_R(c) LV_COLOR_GET_R1(c)
# define LV_COLOR_GET_G(c) LV_COLOR_GET_G1(c)
# define LV_COLOR_GET_B(c) LV_COLOR_GET_B1(c)
# define LV_COLOR_GET_A(c) LV_COLOR_GET_A1(c)
#elif LV_COLOR_DEPTH == 8
# define LV_COLOR_SET_R(c, v) (c).ch.red = v & 0x7;
# define LV_COLOR_SET_G(c, v) (c).ch.green = v & 0x7;
# define LV_COLOR_SET_B(c, v) c(c).ch.blue = v & 0x3;
# define LV_COLOR_SET_R(c, v) LV_COLOR_SET_R8(c,v)
# define LV_COLOR_SET_G(c, v) LV_COLOR_SET_G8(c,v)
# define LV_COLOR_SET_B(c, v) LV_COLOR_SET_B8(c,v)
# define LV_COLOR_SET_A(c, v) LV_COLOR_SET_A8(c,v)
# define LV_COLOR_GET_R(c) (c).ch.red
# define LV_COLOR_GET_G(c) (c).ch.green
# define LV_COLOR_GET_B(c) (c).ch.blue
# define LV_COLOR_GET_R(c) LV_COLOR_GET_R8(c)
# define LV_COLOR_GET_G(c) LV_COLOR_GET_G8(c)
# define LV_COLOR_GET_B(c) LV_COLOR_GET_B8(c)
# define LV_COLOR_GET_A(c) LV_COLOR_GET_A8(c)
#elif LV_COLOR_DEPTH == 16
# define LV_COLOR_SET_R(c, v) (c).ch.red = v & 0x1F;
# define LV_COLOR_SET_R(c, v) LV_COLOR_SET_R16(c,v)
# if LV_COLOR_16_SWAP == 0
# define LV_COLOR_SET_G(c, v) (c).ch.green = v & 0x3F;
# define LV_COLOR_SET_G(c, v) LV_COLOR_SET_G16(c,v)
# else
# define LV_COLOR_SET_G(c, v) {(c).ch.green_h = ((v) >> 3) & 0x3; (c).ch.green_l = (v) & 0x3;}
# define LV_COLOR_SET_G(c, v) LV_COLOR_SET_G16_SWAP(c,v)
# endif
# define LV_COLOR_SET_B(c, v) (c).ch.blue= v & 0x1F;
# define LV_COLOR_SET_B(c, v) LV_COLOR_SET_B16(c,v)
# define LV_COLOR_SET_A(c, v) LV_COLOR_SET_A16(c,v)
# define LV_COLOR_GET_R(c) (c).ch.red
# define LV_COLOR_GET_R(c) LV_COLOR_GET_R16(c)
# if LV_COLOR_16_SWAP == 0
# define LV_COLOR_GET_G(c) (c).ch.green
# define LV_COLOR_GET_G(c) LV_COLOR_GET_G16(c)
# else
# define LV_COLOR_GET_G(c) (((c).ch.green_h << 3) + (c).ch.green_l)
# define LV_COLOR_GET_G(c) LV_COLOR_GET_G16_SWAP(c)
# endif
# define LV_COLOR_GET_B(c) (c).ch.blue
# define LV_COLOR_GET_B(c) LV_COLOR_GET_B16(c)
# define LV_COLOR_GET_A(c) LV_COLOR_GET_A16(c)
#elif LV_COLOR_DEPTH == 32
# define LV_COLOR_SET_R(c, v) (c).ch.red = v & 0xFF;
# define LV_COLOR_SET_G(c, v) (c).ch.green = v & 0xFF;
# define LV_COLOR_SET_B(c, v) (c).ch.blue = v & 0xFF;
# define LV_COLOR_SET_R(c, v) LV_COLOR_SET_R32(c,v)
# define LV_COLOR_SET_G(c, v) LV_COLOR_SET_G32(c,v)
# define LV_COLOR_SET_B(c, v) LV_COLOR_SET_B32(c,v)
# define LV_COLOR_SET_A(c, v) LV_COLOR_SET_A32(c,v)
# define LV_COLOR_GET_R(c) (c).ch.red
# define LV_COLOR_GET_G(c) (c).ch.green
# define LV_COLOR_GET_B(c) (c).ch.blue
# define LV_COLOR_GET_R(c) LV_COLOR_GET_R32(c)
# define LV_COLOR_GET_G(c) LV_COLOR_GET_G32(c)
# define LV_COLOR_GET_B(c) LV_COLOR_GET_B32(c)
# define LV_COLOR_GET_A(c) LV_COLOR_GET_A32(c)
#endif
/**********************
@@ -255,7 +310,7 @@ static inline uint8_t lv_color_to1(lv_color_t color)
return 0;
}
#elif LV_COLOR_DEPTH == 32
if((cLV_COLOR_GET_R(color) & 0x80) || (LV_COLOR_GET_G(color) & 0x80) || (LV_COLOR_GET_B(color) & 0x80)) {
if((LV_COLOR_GET_R(color) & 0x80) || (LV_COLOR_GET_G(color) & 0x80) || (LV_COLOR_GET_B(color) & 0x80)) {
return 1;
} else {
return 0;
@@ -274,21 +329,22 @@ static inline uint8_t lv_color_to8(lv_color_t color)
return color.full;
#elif LV_COLOR_DEPTH == 16
lv_color8_t ret;
ret.ch.red = LV_COLOR_GET_R(color) >> 2; /* 5 - 3 = 2*/
ret.ch.green = LV_COLOR_GET_G(color) >> 3; /* 6 - 3 = 3*/
ret.ch.blue = LV_COLOR_GET_B(color) >> 3; /* 5 - 2 = 3*/
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*/
return ret.full;
#elif LV_COLOR_DEPTH == 32
lv_color8_t ret;
ret.ch.red = LV_COLOR_GET_R(color) >> 5; /* 8 - 3 = 5*/
ret.ch.green = LV_COLOR_GET_G(color) >> 5; /* 8 - 3 = 5*/
ret.ch.blue = LV_COLOR_GET_B(color) >> 6; /* 8 - 2 = 6*/
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*/
return ret.full;
#endif
}
static inline uint16_t lv_color_to16(lv_color_t color)
{
#if LV_COLOR_DEPTH == 1
if(color.full == 0)
return 0;
@@ -296,31 +352,30 @@ static inline uint16_t lv_color_to16(lv_color_t color)
return 0xFFFF;
#elif LV_COLOR_DEPTH == 8
lv_color16_t ret;
ret.ch.red = LV_COLOR_GET_R(color) * 4; /*(2^5 - 1)/(2^3 - 1) = 31/7 = 4*/
LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) * 4); /*(2^5 - 1)/(2^3 - 1) = 31/7 = 4*/
#if LV_COLOR_16_SWAP == 0
ret.ch.green = LV_COLOR_GET_G(color) * 9; /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) * 9); /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
#else
ret.ch.green_h = (LV_COLOR_GET_G(color) * 9) >> 3; /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
ret.ch.green_l = (LV_COLOR_GET_G(color) * 9) & 0x7;
LV_COLOR_SET_G16_SWAP(ret, (LV_COLOR_GET_G(color) * 9)); /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
#endif
ret.ch.blue = LV_COLOR_GET_B(color) * 10; /*(2^5 - 1)/(2^2 - 1) = 31/3 = 10*/
LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) * 10); /*(2^5 - 1)/(2^2 - 1) = 31/3 = 10*/
return ret.full;
#elif LV_COLOR_DEPTH == 16
return color.full;
#elif LV_COLOR_DEPTH == 32
lv_color16_t ret;
ret.ch.red = LV_COLOR_GET_R(color) >> 3; /* 8 - 5 = 3*/
LV_COLOR_SET_R16(ret, LV_COLOR_GET_R(color) >> 3); /* 8 - 5 = 3*/
#if LV_COLOR_16_SWAP == 0
ret.ch.green = LV_COLOR_GET_G(color) >> 2; /* 8 - 6 = 2*/
LV_COLOR_SET_G16(ret, LV_COLOR_GET_G(color) >> 2); /* 8 - 6 = 2*/
#else
ret.ch.green_h = (LV_COLOR_GET_G(color) >> 5; /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
ret.ch.green_l = (LV_COLOR_GET_G(color) >> 2) & 0x7;
LV_COLOR_SET_G16_SWAP(ret, ret.ch.green_h = (LV_COLOR_GET_G(color) >> 2); /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
#endif
ret.ch.green = LV_COLOR_GET_G(color) >> 2; /* 8 - 6 = 2*/
ret.ch.blue = LV_COLOR_GET_B(color) >> 3; /* 8 - 5 = 3*/
LV_COLOR_SET_B16(ret, LV_COLOR_GET_B(color) >> 3); /* 8 - 5 = 3*/
return ret.full;
#endif
return 0;
}
static inline uint32_t lv_color_to32(lv_color_t color)
@@ -332,10 +387,10 @@ static inline uint32_t lv_color_to32(lv_color_t color)
return 0xFFFFFFFF;
#elif LV_COLOR_DEPTH == 8
lv_color32_t ret;
ret.ch.red = LV_COLOR_GET_R(color) * 36; /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
ret.ch.green = LV_COLOR_GET_G(color) * 36; /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
ret.ch.blue = LV_COLOR_GET_B(color) * 85; /*(2^8 - 1)/(2^2 - 1) = 255/3 = 85*/
ret.ch.alpha = 0xFF;
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(color, 0xFF);
return ret.full;
#elif LV_COLOR_DEPTH == 16
/**
@@ -367,10 +422,10 @@ static inline uint32_t lv_color_to32(lv_color_t color)
* 6 259 3 0 255
*/
lv_color32_t ret;
ret.ch.red = (LV_COLOR_GET_R(color) * 263 + 7 ) >> 5;
ret.ch.green = (LV_COLOR_GET_G(color) * 259 + 3 ) >> 6;
ret.ch.blue = (LV_COLOR_GET_B(color) * 263 + 7 ) >> 5;
ret.ch.alpha = 0xFF;
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);
return ret.full;
#elif LV_COLOR_DEPTH == 32
return color.full;
@@ -385,9 +440,7 @@ static inline lv_color_t lv_color_mix(lv_color_t c1, lv_color_t c2, uint8_t mix)
LV_COLOR_SET_R(ret, (uint16_t)((uint16_t) LV_COLOR_GET_R(c1) * mix + LV_COLOR_GET_R(c2) * (255 - mix)) >> 8);
LV_COLOR_SET_G(ret, (uint16_t)((uint16_t) LV_COLOR_GET_G(c1) * mix + LV_COLOR_GET_G(c2) * (255 - mix)) >> 8);
LV_COLOR_SET_B(ret, (uint16_t)((uint16_t) LV_COLOR_GET_B(c1) * mix + LV_COLOR_GET_B(c2) * (255 - mix)) >> 8);
#if LV_COLOR_DEPTH == 32
ret.ch.alpha = 0xFF;
#endif
LV_COLOR_SET_A(ret, 0xFF);
#else
/*LV_COLOR_DEPTH == 1*/
ret.full = mix > LV_OPA_50 ? c1.full : c2.full;
@@ -405,7 +458,7 @@ static inline uint8_t lv_color_brightness(lv_color_t color)
{
lv_color32_t c32;
c32.full = lv_color_to32(color);
uint16_t bright = 3 * LV_COLOR_GET_R(c32) + LV_COLOR_GET_B(c32) + 4 * LV_COLOR_GET_G(c32);
uint16_t bright = 3 * LV_COLOR_GET_R32(c32) + LV_COLOR_GET_B32(c32) + 4 * LV_COLOR_GET_G32(c32);
return (uint16_t)bright >> 3;
}
@@ -423,43 +476,36 @@ static inline lv_color_t lv_color_make(int r8, int g8, int b8)
static inline lv_color_t lv_color_make(uint8_t r8, int g8, int b8)
{
lv_color_t color;
color.ch.blue = b8 >> 6;
color.ch.green = g8 >> 5;
color.ch.red = r8 >> 5;
LV_COLOR_SET_B(color, b8 >> 6);
LV_COLOR_SET_G(color, g8 >> 5);
LV_COLOR_SET_R(color, r8 >> 5);
LV_COLOR_SET_A(color, 0xFF);
return color;
}
#elif LV_COLOR_DEPTH == 16
#if LV_COLOR_16_SWAP == 0
#define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{b8 >> 3, g8 >> 2, r8 >> 3}})
static inline lv_color_t lv_color_make(uint8_t r8, uint8_t g8, uint8_t b8)
{
lv_color_t color;
color.ch.blue = (uint16_t)(b8 >> 3);
color.ch.green = (uint16_t)(g8 >> 2);
color.ch.red = (uint16_t)(r8 >> 3);
return color;
}
#else
#define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{g8 >> 5, r8 >> 3, b8 >> 3, (g8 >> 2) & 0x7}})
#endif
static inline lv_color_t lv_color_make(uint8_t r8, uint8_t g8, uint8_t b8)
{
lv_color_t color;
color.ch.green_h = (uint16_t)(g8 >> 5);
color.ch.red = (uint16_t)(r8 >> 3);
color.ch.blue = (uint16_t)(b8 >> 3);
color.ch.green_l = (uint16_t)((g8 >> 2) & 0x7);
LV_COLOR_SET_B(color, b8 >> 3);
LV_COLOR_SET_G(color, g8 >> 2);
LV_COLOR_SET_R(color, r8 >> 3);
LV_COLOR_SET_A(color, 0xFF);
return color;
}
#endif
#elif LV_COLOR_DEPTH == 32
#define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{b8, g8, r8, 0xff}}) /*Fix 0xff alpha*/
static inline lv_color_t lv_color_make(uint8_t r8, uint8_t g8, uint8_t b8)
{
lv_color_t color;
color.ch.blue = b8;
color.ch.green = g8;
color.ch.red = r8;
color.ch.alpha = 0xff;
LV_COLOR_SET_B(color, b8);
LV_COLOR_SET_G(color, g8);
LV_COLOR_SET_R(color, r8);
LV_COLOR_SET_A(color, 0xFF);
return color;
}
#endif