Shifting bugfix & size tuning in fade_out

This commit is contained in:
Blaž Kristan 2025-01-07 20:39:44 +01:00
parent 249c124176
commit 47a9e4aa51

View File

@ -279,30 +279,31 @@ void Segment::startTransition(uint16_t dur) {
_t->_briT = on ? opacity : 0; _t->_briT = on ? opacity : 0;
_t->_cctT = cct; _t->_cctT = cct;
#ifndef WLED_DISABLE_MODE_BLEND #ifndef WLED_DISABLE_MODE_BLEND
swapSegenv(_t->_segT); // copy runtime data to temporary if (modeBlending) {
_t->_modeT = mode; swapSegenv(_t->_segT); // copy runtime data to temporary
_t->_segT._dataLenT = 0; _t->_modeT = mode;
_t->_segT._dataT = nullptr; _t->_segT._dataLenT = 0;
if (_dataLen > 0 && data) { _t->_segT._dataT = nullptr;
_t->_segT._dataT = (byte *)malloc(_dataLen); if (_dataLen > 0 && data) {
if (_t->_segT._dataT) { _t->_segT._dataT = (byte *)malloc(_dataLen);
//DEBUG_PRINTF_P(PSTR("-- Allocated duplicate data (%d) for %p: %p\n"), _dataLen, this, _t->_segT._dataT); if (_t->_segT._dataT) {
memcpy(_t->_segT._dataT, data, _dataLen); //DEBUG_PRINTF_P(PSTR("-- Allocated duplicate data (%d) for %p: %p\n"), _dataLen, this, _t->_segT._dataT);
_t->_segT._dataLenT = _dataLen; memcpy(_t->_segT._dataT, data, _dataLen);
_t->_segT._dataLenT = _dataLen;
}
} else {
for (size_t i=0; i<NUM_COLORS; i++) _t->_segT._colorT[i] = colors[i];
} }
} else { DEBUG_PRINTF_P(PSTR("-- pal: %d, bri: %d, C:[%08X,%08X,%08X], m: %d\n"),
for (size_t i=0; i<NUM_COLORS; i++) _t->_segT._colorT[i] = colors[i]; (int)_t->_palTid,
} (int)_t->_briT,
DEBUG_PRINTF_P(PSTR("-- pal: %d, bri: %d, C:[%08X,%08X,%08X], m: %d\n"), _t->_segT._colorT[0],
(int)_t->_palTid, _t->_segT._colorT[1],
(int)_t->_briT, _t->_segT._colorT[2],
_t->_segT._colorT[0], (int)_t->_modeT);
_t->_segT._colorT[1], } else
_t->_segT._colorT[2], # endif
(int)_t->_modeT);
#else
for (size_t i=0; i<NUM_COLORS; i++) _t->_colorT[i] = colors[i]; for (size_t i=0; i<NUM_COLORS; i++) _t->_colorT[i] = colors[i];
#endif
} }
void Segment::stopTransition() { void Segment::stopTransition() {
@ -1086,36 +1087,25 @@ void Segment::fade_out(uint8_t rate) {
const int cols = is2D() ? virtualWidth() : virtualLength(); const int cols = is2D() ? virtualWidth() : virtualLength();
const int rows = virtualHeight(); // will be 1 for 1D const int rows = virtualHeight(); // will be 1 for 1D
rate = (255-rate) >> 1; rate = (256-rate) >> 1;
float mappedRate = 1.0f / (float(rate) + 1.1f); const 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);
for (int y = 0; y < rows; y++) for (int x = 0; x < cols; x++) { 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 if (color == colors[1]) continue; // already at target color
int w1 = W(color); for (int i = 0; i < 32; i += 8) {
int r1 = R(color); uint8_t c2 = (colors[1]>>i); // get background channel
int g1 = G(color); uint8_t c1 = (color>>i); // get foreground channel
int b1 = B(color); // we can't use bitshift since we are using int
int delta = (c2 - c1) * mappedRate / 256;
int wdelta = (w2 - w1) * mappedRate; // if fade isn't complete, make sure delta is at least 1 (fixes rounding issues)
int rdelta = (r2 - r1) * mappedRate; if (delta == 0) delta += (c2 == c1) ? 0 : (c2 > c1) ? 1 : -1;
int gdelta = (g2 - g1) * mappedRate; // stuff new value back into color
int bdelta = (b2 - b1) * mappedRate; color &= ~(0xFF<<i);
color |= ((c1 + delta) & 0xFF) << i;
// if fade isn't complete, make sure delta is at least 1 (fixes rounding issues) }
wdelta += (w2 == w1) ? 0 : (w2 > w1) ? 1 : -1; if (is2D()) setPixelColorXY(x, y, color);
rdelta += (r2 == r1) ? 0 : (r2 > r1) ? 1 : -1; else setPixelColor(x, color);
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);
} }
} }