bugfix & code formatting, removed color_scale

also replaced scale8_video with 32bit calculation in color_fade for consistency and speed.
This commit is contained in:
Damian Schneider 2024-04-16 10:43:06 +02:00
parent 459156fe57
commit 084fc2fcd1
4 changed files with 32 additions and 42 deletions

View File

@ -175,7 +175,7 @@ void IRAM_ATTR Segment::setPixelColorXY(int x, int y, uint32_t col)
uint8_t _bri_t = currentBri(); uint8_t _bri_t = currentBri();
if (_bri_t < 255) { if (_bri_t < 255) {
col = color_scale(col, _bri_t); col = color_fade(col, _bri_t);
} }
if (reverse ) x = virtualWidth() - x - 1; if (reverse ) x = virtualWidth() - x - 1;
@ -274,15 +274,13 @@ uint32_t IRAM_ATTR Segment::getPixelColorXY(int x, int y) {
} }
// blurRow: perform a blur on a row of a rectangular matrix // blurRow: perform a blur on a row of a rectangular matrix
void Segment::blurRow(uint32_t row, fract8 blur_amount, bool smear) void Segment::blurRow(uint32_t row, fract8 blur_amount, bool smear){
{
if (!isActive() || blur_amount == 0) if (!isActive() || blur_amount == 0)
return; // not active return; // not active
const uint_fast16_t cols = virtualWidth(); const uint_fast16_t cols = virtualWidth();
const uint_fast16_t rows = virtualHeight(); const uint_fast16_t rows = virtualHeight();
if (row >= rows) if (row >= rows) return;
return;
// blur one row // blur one row
uint8_t keep = smear ? 255 : 255 - blur_amount; uint8_t keep = smear ? 255 : 255 - blur_amount;
uint8_t seep = blur_amount >> 1; uint8_t seep = blur_amount >> 1;
@ -292,8 +290,8 @@ void Segment::blurRow(uint32_t row, fract8 blur_amount, bool smear)
uint32_t curnew; uint32_t curnew;
for (unsigned x = 0; x < cols; x++) { for (unsigned x = 0; x < cols; x++) {
uint32_t cur = getPixelColorXY(x, row); uint32_t cur = getPixelColorXY(x, row);
uint32_t part = color_scale(cur, seep); uint32_t part = color_fade(cur, seep);
curnew = color_scale(cur, keep); curnew = color_fade(cur, keep);
if (x > 0) { if (x > 0) {
if (carryover) if (carryover)
curnew = color_add(curnew, carryover, true); curnew = color_add(curnew, carryover, true);
@ -301,7 +299,7 @@ void Segment::blurRow(uint32_t row, fract8 blur_amount, bool smear)
if (last != prev) // optimization: only set pixel if color has changed if (last != prev) // optimization: only set pixel if color has changed
setPixelColorXY(x - 1, row, prev); setPixelColorXY(x - 1, row, prev);
} }
else // first pixel or last pixel else // first pixel
setPixelColorXY(x, row, curnew); setPixelColorXY(x, row, curnew);
lastnew = curnew; lastnew = curnew;
last = cur; // save original value for comparison on next iteration last = cur; // save original value for comparison on next iteration
@ -326,8 +324,8 @@ void Segment::blurCol(uint32_t col, fract8 blur_amount, bool smear) {
uint32_t curnew; uint32_t curnew;
for (unsigned y = 0; y < rows; y++) { for (unsigned y = 0; y < rows; y++) {
uint32_t cur = getPixelColorXY(col, y); uint32_t cur = getPixelColorXY(col, y);
uint32_t part = color_scale(cur, seep); uint32_t part = color_fade(cur, seep);
curnew = color_scale(cur, keep); curnew = color_fade(cur, keep);
if (y > 0) { if (y > 0) {
if (carryover) if (carryover)
curnew = color_add(curnew, carryover, true); curnew = color_add(curnew, carryover, true);

View File

@ -735,7 +735,7 @@ void IRAM_ATTR Segment::setPixelColor(int i, uint32_t col)
uint16_t len = length(); uint16_t len = length();
uint8_t _bri_t = currentBri(); uint8_t _bri_t = currentBri();
if (_bri_t < 255) { if (_bri_t < 255) {
col = color_scale(col, _bri_t); col = color_fade(col, _bri_t);
} }
// expand pixel (taking into account start, grouping, spacing [and offset]) // expand pixel (taking into account start, grouping, spacing [and offset])
@ -1003,7 +1003,7 @@ void Segment::blur(uint8_t blur_amount, bool smear) {
return; return;
} }
#endif #endif
uint8_t keep = smear ? 250 : 255 - blur_amount; uint8_t keep = smear ? 255 : 255 - blur_amount;
uint8_t seep = blur_amount >> 1; uint8_t seep = blur_amount >> 1;
unsigned vlength = virtualLength(); unsigned vlength = virtualLength();
uint32_t carryover = BLACK; uint32_t carryover = BLACK;
@ -1012,10 +1012,9 @@ void Segment::blur(uint8_t blur_amount, bool smear) {
uint32_t curnew; uint32_t curnew;
for (unsigned i = 0; i < vlength; i++) { for (unsigned i = 0; i < vlength; i++) {
uint32_t cur = getPixelColor(i); uint32_t cur = getPixelColor(i);
uint32_t part = color_scale(cur, seep); uint32_t part = color_fade(cur, seep);
curnew = color_scale(cur, keep); curnew = color_fade(cur, keep);
if (i > 0) if (i > 0) {
{
if (carryover) if (carryover)
curnew = color_add(curnew, carryover, true); curnew = color_add(curnew, carryover, true);
uint32_t prev = color_add(lastnew, part, true); uint32_t prev = color_add(lastnew, part, true);

View File

@ -61,21 +61,6 @@ uint32_t color_add(uint32_t c1, uint32_t c2, bool fast)
} }
} }
/*
* color scale function that replaces scale8 for 32bit colors
*/
uint32_t color_scale(uint32_t c1, uint8_t scale)
{
uint32_t fixedscale = 1 + scale;
uint32_t scaledcolor; //color order is: W R G B from MSB to LSB
scaledcolor = ((R(c1) * fixedscale) >> 8) << 16;
scaledcolor |= ((G(c1) * fixedscale) >> 8) << 8;
scaledcolor |= (B(c1) * fixedscale) >> 8;
scaledcolor |= ((W(c1) * fixedscale) >> 8) << 24;
return scaledcolor;
}
/* /*
* fades color toward black * fades color toward black
* if using "video" method the resulting color will never become black unless it is already black * if using "video" method the resulting color will never become black unless it is already black
@ -83,17 +68,26 @@ uint32_t color_scale(uint32_t c1, uint8_t scale)
uint32_t color_fade(uint32_t c1, uint8_t amount, bool video) uint32_t color_fade(uint32_t c1, uint8_t amount, bool video)
{ {
if (video) uint32_t scaledcolor; // color order is: W R G B from MSB to LSB
{ uint32_t r = R(c1);
uint8_t r = scale8_video(R(c1), amount); uint32_t g = G(c1);
uint8_t g = scale8_video(G(c1), amount); uint32_t b = B(c1);
uint8_t b = scale8_video(B(c1), amount); uint32_t w = W(c1);
uint8_t w = scale8_video(W(c1), amount); if (video) {
return RGBW32(r, g, b, w); uint32_t scale = amount; // 32bit for faster calculation
scaledcolor = (((r * scale) >> 8) << 16) + ((r && scale) ? 1 : 0);
scaledcolor |= (((g * scale) >> 8) << 8) + ((g && scale) ? 1 : 0);
scaledcolor |= ((b * scale) >> 8) + ((b && scale) ? 1 : 0);
scaledcolor |= (((w * scale) >> 8) << 24) + ((w && scale) ? 1 : 0);
return scaledcolor;
} }
else else {
{ uint32_t scale = 1 + amount;
return color_scale(c1, amount); scaledcolor = ((r * scale) >> 8) << 16;
scaledcolor |= ((g * scale) >> 8) << 8;
scaledcolor |= (b * scale) >> 8;
scaledcolor |= ((w * scale) >> 8) << 24;
return scaledcolor;
} }
} }

View File

@ -80,7 +80,6 @@ class NeoGammaWLEDMethod {
#define gamma8(c) NeoGammaWLEDMethod::rawGamma8(c) #define gamma8(c) NeoGammaWLEDMethod::rawGamma8(c)
uint32_t color_blend(uint32_t,uint32_t,uint16_t,bool b16=false); uint32_t color_blend(uint32_t,uint32_t,uint16_t,bool b16=false);
uint32_t color_add(uint32_t,uint32_t, bool fast=false); uint32_t color_add(uint32_t,uint32_t, bool fast=false);
uint32_t color_scale(uint32_t c1, uint8_t scale);
uint32_t color_fade(uint32_t c1, uint8_t amount, bool video=false); uint32_t color_fade(uint32_t c1, uint8_t amount, bool video=false);
CRGBPalette16 generateHarmonicRandomPalette(CRGBPalette16 &basepalette); CRGBPalette16 generateHarmonicRandomPalette(CRGBPalette16 &basepalette);
CRGBPalette16 generateRandomPalette(void); CRGBPalette16 generateRandomPalette(void);