Shifting bugfix & size tuning in fade_out

This commit is contained in:
Blaž Kristan 2025-01-07 20:39:44 +01:00
parent 7ce96c190f
commit 08d12f9ee6

View File

@ -1206,44 +1206,25 @@ void Segment::fade_out(uint8_t rate) {
const int cols = is2D() ? vWidth() : vLength();
const int rows = vHeight(); // will be 1 for 1D
rate = (255-rate) >> 1;
//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);
int r2 = R(color);
int g2 = G(color);
int b2 = B(color);
rate = (256-rate) >> 1;
const int mappedRate = 256 / (rate + 1);
for (int y = 0; y < rows; y++) for (int x = 0; x < cols; x++) {
color = is2D() ? getPixelColorXY(x, y) : getPixelColor(x);
uint32_t color = is2D() ? getPixelColorXY(x, y) : getPixelColor(x);
if (color == colors[1]) continue; // already at target color
int w1 = W(color);
int r1 = R(color);
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) >> 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;
rdelta += (r2 == r1) ? 0 : (r2 > r1) ? 1 : -1;
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);
uint32_t newColor = RGBW32(r1 + rdelta, g1 + gdelta, b1 + bdelta, w1 + wdelta);
if (is2D()) setPixelColorXY(x, y, newColor);
else setPixelColor(x, newColor);
for (int i = 0; i < 32; i += 8) {
uint8_t c2 = (colors[1]>>i); // get background channel
uint8_t c1 = (color>>i); // get foreground channel
// we can't use bitshift since we are using int
int delta = (c2 - c1) * mappedRate / 256;
// if fade isn't complete, make sure delta is at least 1 (fixes rounding issues)
if (delta == 0) delta += (c2 == c1) ? 0 : (c2 > c1) ? 1 : -1;
// stuff new value back into color
color &= ~(0xFF<<i);
color |= ((c1 + delta) & 0xFF) << i;
}
if (is2D()) setPixelColorXY(x, y, color);
else setPixelColor(x, color);
}
}