@@ -84,25 +84,27 @@ IRAM_ATTR_YN __attribute__((hot)) uint32_t color_fade(uint32_t c1, uint8_t amoun
8484 if (amount == 255 ) return c1; // WLEDMM small optimization - plus it avoids over-fading in "video" mode
8585 if (amount == 0 ) return 0 ; // WLEDMM shortcut
8686
87- uint32_t scaledcolor; // color order is: W R G B from MSB to LSB
88- uint32_t r = R (c1);
89- uint32_t g = G (c1);
90- uint32_t b = B (c1);
91- uint32_t w = W (c1);
87+ uint32_t scaledcolor = 0 ; // color order is: W R G B from MSB to LSB
88+ uint16_t w = W (c1); // WLEDMM 16bit to make sure the compiler uses 32bit (not 64bit) for the math
89+ uint16_t r = R (c1);
90+ uint16_t g = G (c1);
91+ uint16_t b = B (c1);
9292 if (video) {
93- uint32_t scale = amount; // 32bit for faster calculation
94- scaledcolor = (((r * scale) >> 8 ) << 16 ) + ((r && scale) ? 1 : 0 );
95- scaledcolor |= (((g * scale) >> 8 ) << 8 ) + ((g && scale) ? 1 : 0 );
96- scaledcolor |= ((b * scale) >> 8 ) + ((b && scale) ? 1 : 0 );
97- if (w>0 ) scaledcolor |= (((w * scale) >> 8 ) << 24 ) + ((scale) ? 1 : 0 ); // WLEDMM small speedup when no white channel
93+ uint16_t scale = amount; // 32bit for faster calculation
94+ // bugfix: doing "+1" after shifting is obviously wrong
95+ // optimization: ((r && scale) ? 1 : 0) can be simplified to "if (r > 0) +1" ; if we arive here, then scale != 0 and scale < 255
96+ if (w>0 ) scaledcolor |= (((w * scale) >> 8 ) +1 ) << 24 ; // WLEDMM small speedup when no white channel
97+ if (r>0 ) scaledcolor |= (((r * scale) >> 8 ) +1 ) << 16 ;
98+ if (g>0 ) scaledcolor |= (((g * scale) >> 8 ) +1 ) << 8 ;
99+ if (b>0 ) scaledcolor |= ((b * scale) >> 8 ) +1 ;
98100 return scaledcolor;
99101 }
100102 else {
101- uint32_t scale = 1 + amount;
102- scaledcolor = ((r * scale) >> 8 ) << 16 ;
103- scaledcolor |= ((g * scale) >> 8 ) << 8 ;
104- scaledcolor |= (b * scale) >> 8 ;
103+ uint16_t scale = 1 + amount;
105104 if (w>0 ) scaledcolor |= ((w * scale) >> 8 ) << 24 ; // WLEDMM small speedup when no white channel
105+ scaledcolor |= ((r * scale) >> 8 ) << 16 ;
106+ scaledcolor |= (g * scale) & 0x0000FF00 ; // WLEDMM faster than right-left shift "" >>8 ) <<8"
107+ scaledcolor |= (b * scale) >> 8 ;
106108 return scaledcolor;
107109 }
108110}
0 commit comments