From 2edfcb33437386d32d8327a3c91b1c54c8df68e1 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Thu, 20 Jun 2024 12:26:43 +0200 Subject: [PATCH] small optimization for color_blend * Early exit when color1 == color2 (nothing to blend) * pre-calculate `blendmax - blend` (repeated 4 times) --- wled00/colors.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/wled00/colors.cpp b/wled00/colors.cpp index 6546043b..465fefbb 100644 --- a/wled00/colors.cpp +++ b/wled00/colors.cpp @@ -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); }