mirror of
https://github.com/wled/WLED.git
synced 2025-07-15 23:06:36 +00:00
Minor speed & size tweaks
- new function fadeToSecondaryBy()
This commit is contained in:
parent
f4e7202dd0
commit
a8cde3289a
@ -678,6 +678,7 @@ typedef struct Segment {
|
||||
void blur(uint8_t, bool smear = false);
|
||||
void fill(uint32_t c);
|
||||
void fade_out(uint8_t r);
|
||||
void fadeToSecondaryBy(uint8_t fadeBy);
|
||||
void fadeToBlackBy(uint8_t fadeBy);
|
||||
inline void blendPixelColor(int n, uint32_t color, uint8_t blend) { setPixelColor(n, color_blend(getPixelColor(n), color, blend)); }
|
||||
inline void blendPixelColor(int n, CRGB c, uint8_t blend) { blendPixelColor(n, RGBW32(c.r,c.g,c.b,0), blend); }
|
||||
|
@ -1178,6 +1178,8 @@ void Segment::fill(uint32_t c) {
|
||||
|
||||
/*
|
||||
* fade out function, higher rate = quicker fade
|
||||
* fading is highly dependant on frame rate (higher frame rates, faster fading)
|
||||
* each frame will fade at max 9% or as little as 0.8%
|
||||
*/
|
||||
void Segment::fade_out(uint8_t rate) {
|
||||
if (!isActive()) return; // not active
|
||||
@ -1185,7 +1187,8 @@ void Segment::fade_out(uint8_t rate) {
|
||||
const int rows = vHeight(); // will be 1 for 1D
|
||||
|
||||
rate = (255-rate) >> 1;
|
||||
float mappedRate = 1.0f / (float(rate) + 1.1f);
|
||||
//float mappedRate = 1.0f / (float(rate) + 1.1f); // mappedRate is in (nonlinear) range ~ [0.0078,0.9091] so roughly 1% to 91%
|
||||
int mappedRate = 256 / (rate + 1);
|
||||
|
||||
uint32_t color = colors[1]; // SEGCOLOR(1); // target color
|
||||
int w2 = W(color);
|
||||
@ -1201,10 +1204,14 @@ void Segment::fade_out(uint8_t rate) {
|
||||
int g1 = G(color);
|
||||
int b1 = B(color);
|
||||
|
||||
int wdelta = (w2 - w1) * mappedRate;
|
||||
int rdelta = (r2 - r1) * mappedRate;
|
||||
int gdelta = (g2 - g1) * mappedRate;
|
||||
int bdelta = (b2 - b1) * mappedRate;
|
||||
//int wdelta = (w2 - w1) * mappedRate;
|
||||
//int rdelta = (r2 - r1) * mappedRate;
|
||||
//int gdelta = (g2 - g1) * mappedRate;
|
||||
//int bdelta = (b2 - b1) * mappedRate;
|
||||
int wdelta = ((w2 - w1) * mappedRate) >> 8;
|
||||
int rdelta = ((r2 - r1) * mappedRate) >> 8;
|
||||
int gdelta = ((g2 - g1) * mappedRate) >> 8;
|
||||
int bdelta = ((b2 - b1) * mappedRate) >> 8;
|
||||
|
||||
// if fade isn't complete, make sure delta is at least 1 (fixes rounding issues)
|
||||
wdelta += (w2 == w1) ? 0 : (w2 > w1) ? 1 : -1;
|
||||
@ -1212,8 +1219,23 @@ void Segment::fade_out(uint8_t rate) {
|
||||
gdelta += (g2 == g1) ? 0 : (g2 > g1) ? 1 : -1;
|
||||
bdelta += (b2 == b1) ? 0 : (b2 > b1) ? 1 : -1;
|
||||
|
||||
if (is2D()) setPixelColorXY(x, y, r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
|
||||
else setPixelColor(x, r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
|
||||
//if (is2D()) setPixelColorXY(x, y, r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
|
||||
//else setPixelColor(x, r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
|
||||
uint32_t newColor = RGBW32(r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
|
||||
if (is2D()) setPixelColorXY(x, y, newColor);
|
||||
else setPixelColor(x, newColor);
|
||||
}
|
||||
}
|
||||
|
||||
// fades all pixels to secondary color
|
||||
void Segment::fadeToSecondaryBy(uint8_t fadeBy) {
|
||||
if (!isActive() || fadeBy == 0) return; // optimization - no scaling to apply
|
||||
const int cols = is2D() ? vWidth() : vLength();
|
||||
const int rows = vHeight(); // will be 1 for 1D
|
||||
|
||||
for (int y = 0; y < rows; y++) for (int x = 0; x < cols; x++) {
|
||||
if (is2D()) setPixelColorXY(x, y, color_blend(getPixelColorXY(x,y), colors[1], fadeBy));
|
||||
else setPixelColor(x, color_blend(getPixelColor(x), colors[1], fadeBy));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -10,12 +10,13 @@
|
||||
*/
|
||||
uint32_t color_blend(uint32_t color1, uint32_t color2, uint8_t blend) {
|
||||
// min / max blend checking is omitted: calls with 0 or 255 are rare, checking lowers overall performance
|
||||
uint32_t rb1 = color1 & 0x00FF00FF;
|
||||
uint32_t wg1 = (color1>>8) & 0x00FF00FF;
|
||||
uint32_t rb2 = color2 & 0x00FF00FF;
|
||||
uint32_t wg2 = (color2>>8) & 0x00FF00FF;
|
||||
uint32_t rb3 = ((((rb1 << 8) | rb2) + (rb2 * blend) - (rb1 * blend)) >> 8) & 0x00FF00FF;
|
||||
uint32_t wg3 = ((((wg1 << 8) | wg2) + (wg2 * blend) - (wg1 * blend))) & 0xFF00FF00;
|
||||
const uint32_t mask = 0x00FF00FF;
|
||||
uint32_t rb1 = color1 & mask;
|
||||
uint32_t wg1 = (color1 >> 8) & mask;
|
||||
uint32_t rb2 = color2 & mask;
|
||||
uint32_t wg2 = (color2 >> 8) & mask;
|
||||
uint32_t rb3 = ((((rb1 << 8) | rb2) + (rb2 * blend) - (rb1 * blend)) >> 8) & mask;
|
||||
uint32_t wg3 = ((((wg1 << 8) | wg2) + (wg2 * blend) - (wg1 * blend))) & mask;
|
||||
return rb3 | wg3;
|
||||
}
|
||||
|
||||
@ -28,8 +29,9 @@ uint32_t color_add(uint32_t c1, uint32_t c2, bool preserveCR)
|
||||
{
|
||||
if (c1 == BLACK) return c2;
|
||||
if (c2 == BLACK) return c1;
|
||||
uint32_t rb = (c1 & 0x00FF00FF) + (c2 & 0x00FF00FF); // mask and add two colors at once
|
||||
uint32_t wg = ((c1>>8) & 0x00FF00FF) + ((c2>>8) & 0x00FF00FF);
|
||||
const uint32_t mask = 0x00FF00FF;
|
||||
uint32_t rb = (c1 & mask) + (c2 & mask); // mask and add two colors at once
|
||||
uint32_t wg = ((c1>>8) & mask) + ((c2>>8) & mask);
|
||||
uint32_t r = rb >> 16; // extract single color values
|
||||
uint32_t b = rb & 0xFFFF;
|
||||
uint32_t w = wg >> 16;
|
||||
@ -44,9 +46,9 @@ uint32_t color_add(uint32_t c1, uint32_t c2, bool preserveCR)
|
||||
//max = b > max ? b : max;
|
||||
//max = w > max ? w : max;
|
||||
if (max > 255) {
|
||||
uint32_t scale = (uint32_t(255)<<8) / max; // division of two 8bit (shifted) values does not work -> use bit shifts and multiplaction instead
|
||||
rb = ((rb * scale) >> 8) & 0x00FF00FF; //
|
||||
wg = (wg * scale) & 0xFF00FF00;
|
||||
const uint32_t scale = (uint32_t(255)<<8) / max; // division of two 8bit (shifted) values does not work -> use bit shifts and multiplaction instead
|
||||
rb = ((rb * scale) >> 8) & mask; //
|
||||
wg = (wg * scale) & mask;
|
||||
} else wg = wg << 8; //shift white and green back to correct position
|
||||
return rb | wg;
|
||||
} else {
|
||||
@ -77,8 +79,9 @@ uint32_t color_fade(uint32_t c1, uint8_t amount, bool video)
|
||||
addRemains |= B(c1) ? 0x00000001 : 0;
|
||||
addRemains |= W(c1) ? 0x01000000 : 0;
|
||||
}
|
||||
uint32_t rb = (((c1 & 0x00FF00FF) * scale) >> 8) & 0x00FF00FF; // scale red and blue
|
||||
uint32_t wg = (((c1 & 0xFF00FF00) >> 8) * scale) & 0xFF00FF00; // scale white and green
|
||||
const uint32_t mask = 0x00FF00FF;
|
||||
uint32_t rb = (((c1 & mask) * scale) >> 8) & mask; // scale red and blue
|
||||
uint32_t wg = (((c1 >> 8) & mask) * scale) & (mask << 8); // scale white and green
|
||||
scaledcolor = (rb | wg) + addRemains;
|
||||
return scaledcolor;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user