1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

perf(color): add faster lv_color_hex function (#2864)

* Add faster lv_color_hex method to convert from ARGB32 to lv_color_t, bypassing lv_color_make and component extraction

* Update src/misc/lv_color.h

Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
This commit is contained in:
X-Ryl669 2021-12-03 10:22:13 +01:00 committed by GitHub
parent 222af5ad4a
commit 0564b93d0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -596,7 +596,36 @@ static inline lv_color_t lv_color_make(uint8_t r, uint8_t g, uint8_t b)
static inline lv_color_t lv_color_hex(uint32_t c)
{
#if LV_COLOR_DEPTH == 16
lv_color_t r;
#if LV_COLOR_16_SWAP == 0
/* Convert a 4 bytes per pixel in format ARGB32 to R5G6B5 format
naive way (by calling lv_color_make with components):
r = ((c & 0xFF0000) >> 19)
g = ((c & 0xFF00) >> 10)
b = ((c & 0xFF) >> 3)
rgb565 = (r << 11) | (g << 5) | b
That's 3 mask, 5 bitshift and 2 or operations
A bit better:
r = ((c & 0xF80000) >> 8)
g = ((c & 0xFC00) >> 5)
b = ((c & 0xFF) >> 3)
rgb565 = r | g | b
That's 3 mask, 3 bitshifts and 2 or operations */
r.full = (uint16_t)(((c & 0xF80000) >> 8) | ((c & 0xFC00) >> 5) | ((c & 0xFF) >> 3));
#else
/* We want: rrrr rrrr GGGg gggg bbbb bbbb => gggb bbbb rrrr rGGG */
r.full = (uint16_t)(((c & 0xF80000) >> 16) | ((c & 0xFC00) >> 13) | ((c & 0x1C00) << 3) | ((c & 0xF8) << 5));
#endif
return r;
#elif LV_COLOR_DEPTH == 32
lv_color_t r;
r.full = c | 0xFF000000;
return r;
#else /*LV_COLOR_DEPTH == 8*/
return lv_color_make((uint8_t)((c >> 16) & 0xFF), (uint8_t)((c >> 8) & 0xFF), (uint8_t)(c & 0xFF));
#endif
}
static inline lv_color_t lv_color_hex3(uint32_t c)