Repository navigation
Add 8-bit palette interpolation function and comparative example. - #202
generalelectrix wants to merge 1 commit into
Conversation
Also slightly enhanced documentation in colorutils.cpp. The SmoothPalette example directly compares the low-speed animation quality performance of 4-bit interpolation vs. 8-bit interpolation. 8-bit interpolation is essentially exactly as efficient as 4-bit as the scale functions are already 8-bit. This enables the use of compact palettes as useful tools in the creation of animations that are designed to run at very slow rates.
|
Thanks for the idea, suggestion, and code! |
|
@generalelectrix this is exactly what I need to slowly shift colors in a palette, thanks for this PR! |
|
nice work. |
|
Is this going to be merged? |
|
Hi, |
|
|
||
| // Calculate the scaling factor and scaled values for the lower palette value. | ||
|
|
||
| uint8_t f1 = 256 - offset; |
There was a problem hiding this comment.
| uint8_t f1 = 256 - offset; | |
| uint8_t f1 = 255 - offset; |
There was a problem hiding this comment.
Tested and this works. I didn't spot it 😊
|
This works beautifully. I guess it hasn't been merged because it's incomplete; it doesn't support the 32-entry palette type. That's a simple enough fix: CRGB ColorFromPaletteExtended(const CRGBPalette32& pal, uint16_t index, uint8_t brightness, TBlendType blendType) {
// Extract the five most significant bits of the index as a palette index.
uint8_t index_5bit = (index >> 11);
// Calculate the 8-bit offset from the palette index.
uint8_t offset = (uint8_t)(index >> 3);
// Get the palette entry from the 5-bit index
const CRGB* entry = &(pal[0]) + index_5bit;
uint8_t red1 = entry->red;
uint8_t green1 = entry->green;
uint8_t blue1 = entry->blue;
uint8_t blend = offset && (blendType != NOBLEND);
if (blend) {
if (index_5bit == 31) {
entry = &(pal[0]);
} else {
entry++;
}
// Calculate the scaling factor and scaled values for the lower palette value.
uint8_t f1 = 255 - offset;
red1 = scale8_LEAVING_R1_DIRTY(red1, f1);
green1 = scale8_LEAVING_R1_DIRTY(green1, f1);
blue1 = scale8_LEAVING_R1_DIRTY(blue1, f1);
// Calculate the scaled values for the neighbouring palette value.
uint8_t red2 = entry->red;
uint8_t green2 = entry->green;
uint8_t blue2 = entry->blue;
red2 = scale8_LEAVING_R1_DIRTY(red2, offset);
green2 = scale8_LEAVING_R1_DIRTY(green2, offset);
blue2 = scale8_LEAVING_R1_DIRTY(blue2, offset);
cleanup_R1();
// These sums can't overflow, so no qadd8 needed.
red1 += red2;
green1 += green2;
blue1 += blue2;
}
if (brightness != 255) {
nscale8x3_video(red1, green1, blue1, brightness);
}
return CRGB(red1, green1, blue1);
}Here's an example of the difference this function can make. |
|
Any updates? |
|
Hi there, this is a great feature that I would love to see included into a future release. I think there many applications where this would really add to a project. |
|
Just a reminder for removing janky color fades.... =) |
|
All I want for Christmas is ... Smooth colour fades 😅 |
|
It's been merged. Will go in on Monday for the next release. |
|
I love that a variant of this made it in! |
|
Here's the question: is this fast enough for AVR to be the default? |
The FastLED compact palettes are really nice creative tools for quickly creating color textures. However, the effective 4-bit interpolation between palette entries (256-color palette) implies that animating a color fade from one palette entry to another will appear stepped/jagged if the fade time is longer than 16 frames.
I defined an "extended" version of the ColorFromPalette function which uses the 12 most significant bits of a 16-bit index; like the regular 8-bit version, the four most significant bits index the color palette, and the next 8 less significant bits are used as an offset to interpolate between the palette entries (the four LSB are ignored). Essentially no major changes were required in the underlying function as the scale functions already accept 8-bit values. This implies full 8-bit color fades between palette entries, so very slow animations using compact palettes are perfectly smooth at no extra expense (with 4-bit interpolation, they are jagged and aesthetically displeasing except when all color fades are faster than about 500 ms - very fast animation!).
I'm not entirely happy with the "ignore the 4 LSB" approach, but it does keep the interface basically identical to the regular 8-bit palette indexing, allows the use of overflow to reset index counters, etc. Mostly it means that you have to use larger numbers to increment and index into palettes. I'm happy to hear feedback on this design decision.
This function was the difference in getting a project working perfectly and beautifully with very little effort. Load up 13 compact palettes, run the animations nice and slow, change them every minute or two, and boom.
Unfortunately the LED rig I wrote this to use on has left for the playa, so I don't have anything to test the example. In a couple weeks I'll run it on that platform and check to make sure it exhibits the correct behavior.
(btw Hi @focalintent, long time no cross paths. This is a very nice library you folks have put together! -Chris Macklin)