sgl_color_t sgl_color_mixer(sgl_color_t fg_color, sgl_color_t bg_color, uint8_t factor)
{
sgl_color_t ret;
#if (CONFIG_SGL_PANEL_PIXEL_DEPTH == SGL_COLOR_RGB233)
... /* 原代码不动 */
#elif (CONFIG_SGL_PANEL_PIXEL_DEPTH == SGL_COLOR_RGB565)
/* 1. 统一把 16-bit 颜色放到 32-bit 寄存器,避免 16→32 的符号扩展 */
uint32_t bg = bg_color.full;
uint32_t fg = fg_color.full;
#if CONFIG_SGL_COLOR16_SWAP
/* 2. 字节序交换:一条 ROR 指令即可(编译器会优化成 rev16) */
bg = (bg << 8) | (bg >> 8);
fg = (fg << 8) | (fg >> 8);
#endif
/* 3. 32-bit 一次乘加:
mask = 0xF81F07E0 把 R/B 和 G 同时分离出来
低 5+6+5 = 16 位,高 16 位清零,可直接用 32-bit 乘法 */
const uint32_t mask = 0xF81F07E0U;
uint32_t diff = (fg - bg) & mask; /* 带符号差值,高位清零 */
uint32_t blend = (diff * factor) >> 8; /* 8-bit α 缩放 */
uint32_t out = bg + blend; /* 32-bit 加,溢出位自动丢弃 */
#if CONFIG_SGL_COLOR16_SWAP
/* 4. 再交换回来 */
out = (out << 8) | (out >> 8);
#endif
ret.full = (uint16_t)out;
#elif (CONFIG_SGL_PANEL_PIXEL_DEPTH == SGL_COLOR_RGB888)
... /* 原代码不动 */
AI给出了对16bit的优化代码:
不知道测试如何