Skip to content

Commit 2edfcb3

Browse files
authored
small optimization for color_blend
* Early exit when color1 == color2 (nothing to blend) * pre-calculate `blendmax - blend` (repeated 4 times)
1 parent fc173b3 commit 2edfcb3

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

wled00/colors.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
*/
1010
IRAM_ATTR_YN uint32_t color_blend(uint32_t color1, uint32_t color2, uint_fast16_t blend, bool b16) {
1111
if(blend == 0) return color1;
12-
uint_fast16_t blendmax = b16 ? 0xFFFF : 0xFF;
12+
if (color1 == color2) return color1; // WLEDMM shortcut
13+
const uint_fast16_t blendmax = b16 ? 0xFFFF : 0xFF;
1314
if(blend == blendmax) return color2;
1415
const uint_fast8_t shift = b16 ? 16 : 8;
16+
const uint_fast16_t blend2 = blendmax - blend; // WLEDMM pre-calculate value
1517

1618
uint32_t w1 = W(color1);
1719
uint32_t r1 = R(color1);
@@ -23,10 +25,10 @@ IRAM_ATTR_YN uint32_t color_blend(uint32_t color1, uint32_t color2, uint_fast16_
2325
uint32_t g2 = G(color2);
2426
uint32_t b2 = B(color2);
2527

26-
uint32_t w3 = ((w2 * blend) + (w1 * (blendmax - blend))) >> shift;
27-
uint32_t r3 = ((r2 * blend) + (r1 * (blendmax - blend))) >> shift;
28-
uint32_t g3 = ((g2 * blend) + (g1 * (blendmax - blend))) >> shift;
29-
uint32_t b3 = ((b2 * blend) + (b1 * (blendmax - blend))) >> shift;
28+
uint32_t w3 = ((w2 * blend) + (w1 * blend2)) >> shift;
29+
uint32_t r3 = ((r2 * blend) + (r1 * blend2)) >> shift;
30+
uint32_t g3 = ((g2 * blend) + (g1 * blend2)) >> shift;
31+
uint32_t b3 = ((b2 * blend) + (b1 * blend2)) >> shift;
3032

3133
return RGBW32(r3, g3, b3, w3);
3234
}

0 commit comments

Comments
 (0)