small optimization for color_blend

* Early exit when color1 == color2 (nothing to blend)
* pre-calculate `blendmax - blend` (repeated 4 times)
This commit is contained in:
Frank
2024-06-20 12:26:43 +02:00
committed by GitHub
parent fc173b3bc0
commit 2edfcb3343

View File

@@ -9,9 +9,11 @@
*/
IRAM_ATTR_YN uint32_t color_blend(uint32_t color1, uint32_t color2, uint_fast16_t blend, bool b16) {
if(blend == 0) return color1;
uint_fast16_t blendmax = b16 ? 0xFFFF : 0xFF;
if (color1 == color2) return color1; // WLEDMM shortcut
const uint_fast16_t blendmax = b16 ? 0xFFFF : 0xFF;
if(blend == blendmax) return color2;
const uint_fast8_t shift = b16 ? 16 : 8;
const uint_fast16_t blend2 = blendmax - blend; // WLEDMM pre-calculate value
uint32_t w1 = W(color1);
uint32_t r1 = R(color1);
@@ -23,10 +25,10 @@ IRAM_ATTR_YN uint32_t color_blend(uint32_t color1, uint32_t color2, uint_fast16_
uint32_t g2 = G(color2);
uint32_t b2 = B(color2);
uint32_t w3 = ((w2 * blend) + (w1 * (blendmax - blend))) >> shift;
uint32_t r3 = ((r2 * blend) + (r1 * (blendmax - blend))) >> shift;
uint32_t g3 = ((g2 * blend) + (g1 * (blendmax - blend))) >> shift;
uint32_t b3 = ((b2 * blend) + (b1 * (blendmax - blend))) >> shift;
uint32_t w3 = ((w2 * blend) + (w1 * blend2)) >> shift;
uint32_t r3 = ((r2 * blend) + (r1 * blend2)) >> shift;
uint32_t g3 = ((g2 * blend) + (g1 * blend2)) >> shift;
uint32_t b3 = ((b2 * blend) + (b1 * blend2)) >> shift;
return RGBW32(r3, g3, b3, w3);
}