@@ -87,29 +87,31 @@ uint32_t color_fade(uint32_t c1, uint8_t amount, bool video)
8787uint32_t ColorFromPaletteWLED (const CRGBPalette16& pal, unsigned index, uint8_t brightness, TBlendType blendType)
8888{
8989 if (blendType == LINEARBLEND_NOWRAP) {
90- index = (index* 240 ) >> 8 ; // Blend range is affected by lo4 blend of values, remap to avoid wrapping
90+ index = (index * 0xF0 ) >> 8 ; // Blend range is affected by lo4 blend of values, remap to avoid wrapping
9191 }
92+ uint32_t clr32;
9293 unsigned hi4 = byte (index) >> 4 ;
93- const CRGB* entry = (CRGB*)((uint8_t *)(&(pal[0 ])) + (hi4 * sizeof (CRGB)));
94- unsigned red1 = entry->r ;
95- unsigned green1 = entry->g ;
96- unsigned blue1 = entry->b ;
97- if (blendType != NOBLEND) {
94+ unsigned lo4 = (index & 0x0F );
95+ const CRGB* entry = (CRGB*)&(pal[0 ]) + hi4;
96+ if (lo4 && blendType != NOBLEND) {
97+ unsigned red1 = entry->r ;
98+ unsigned green1 = entry->g ;
99+ unsigned blue1 = entry->b ;
98100 if (hi4 == 15 ) entry = &(pal[0 ]);
99101 else ++entry;
100- unsigned f2 = ((index & 0x0F ) << 4 ) + 1 ; // +1 so we scale by 256 as a max value, then result can just be shifted by 8
101- unsigned f1 = ( 257 - f2); // f2 is 1 minimum, so this is 256 max
102- red1 = (red1 * f1 + (unsigned )entry->r * f2) >> 8 ;
102+ unsigned f2 = (lo4 << 4 );
103+ unsigned f1 = 256 - f2;
104+ red1 = (red1 * f1 + (unsigned )entry->r * f2) >> 8 ; // note: using color_blend() is 20% slower
103105 green1 = (green1 * f1 + (unsigned )entry->g * f2) >> 8 ;
104106 blue1 = (blue1 * f1 + (unsigned )entry->b * f2) >> 8 ;
107+ clr32 = RGBW32 (red1, green1, blue1, 0 );
105108 }
109+ else
110+ clr32 = RGBW32 (entry->r , entry->g , entry->b , 0 );
106111 if (brightness < 255 ) { // note: zero checking could be done to return black but that is hardly ever used so it is omitted
107- uint32_t scale = brightness + 1 ; // adjust for rounding (bitshift)
108- red1 = (red1 * scale) >> 8 ;
109- green1 = (green1 * scale) >> 8 ;
110- blue1 = (blue1 * scale) >> 8 ;
112+ clr32 = color_fade (clr32, brightness);
111113 }
112- return RGBW32 (red1,green1,blue1, 0 ) ;
114+ return clr32 ;
113115}
114116
115117void setRandomColor (byte* rgb)
0 commit comments