Minor speed & size tweaks

- new function fadeToSecondaryBy()
This commit is contained in:
Blaž Kristan
2024-12-29 19:52:11 +01:00
parent f4e7202dd0
commit a8cde3289a
3 changed files with 46 additions and 20 deletions

View File

@@ -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;
}