Color Palette Crossfades
Example code showing how to continuously cross-fade between multiple color palettes on the fly. https://gist.github.com/kriegsman/1f7ccbbfa492a73c015e
(NOTE: this requires the very latest FastLED v3.1 from github. Go get it now if you want to try this example.)
Theory of operation: you have two palette objects at all times. One of them, “currentPalette” is the palette that colors are being rendered from, via ColorFromPalette. This palette will be almost continuously modified – but there’s a function that will take care of this for you. The other palette object, the “targetPalette” is where you assign the next set of colors that you’d like the animation to fade to. By repeatedly calling
nblendPaletteTowardPalette( currentPalette, targetPalette);
inside your loop function, any time you assign a new color palette into targetPalette, the currentPalette will automatically start cross-fading toward the new target.
So, for example, if you assign a red palette to targetPalette, your animation will slowly turn red. If you then assign a blue palette to targetPalette, your animation will slowly cross-fade to blue colors. All you have to do when you want to start a new cross-fade is assign a new palette to targetPalette.
If you want continuous cross-fades, you never have to ‘manually’ update the currentPalette, or deal with the cross-fade math. Just periodically assign new colors into targetPalette, and the currentPalette will follow along automatically.
Some control is already provided for adjusting the speed of the cross-fade. Every time you call nblendPaletteTowardPalette, it may make a few changes to currentPalette to adjust it to be more like targetPalette. You can specify the maximum number of small changes that are made in each call. Changing one R, G, or B value in one palette entry counts as one change; since there are 16 palette entries, the maximum number of changes that can be made in each call is 16 x 3 = 48. I recommend starting with the default value of 24; lower numbers mean fewer changes per call, which means a slower crossfade. If you want the cross-fade faster then “48” gives you, call nblendPaletteTowardPalette twice each time through your loop function. [API SUBJECT TO CHANGE – and actually likely to change a bit, but here it is to play with.] http://www.youtube.com/watch?v=LUfIQTVlkrU
Clever hack / side-effect: if you initialize your currentPalette to all black, and targetPalette to your initial color palette, e.g., in setup() do this:
currentPalette = CRGBPalette16( CRGB::Black);
targetPalette = PartyColors_p;
then you’ll automatically get a fade-in-from-black when your animation starts up!
If you want it to snap to full brightness (as has been traditional), then initialize both currentPalette and targetPalette to your ‘real’ initial color palette, e.g.:
currentPalette = PartyColors_p;
targetPalette = PartyColors_p;
And you can use this second technique any time you want to ‘snap’ to a new color palette: just assign it to both targetPalette and currentPalette.
I’ve got the latest FastLED3.1 branch code but there’s no nblendPaletteTowardPalette() function anywhere. Since that function isn’t in the gist sketch where can I get it? Did you forget to commit/push a new file or something?
Hehe, I forget to commit/push stuff all the time. It’s so easy to do… Your brain has just come off a coding session and it isn’t quite ready for the git command yet
The commit was just yesterday (Saturday), so do a git pull or grab a new snapshot?
Maybe there’s an old version of the library lurking somewhere that needs cleaning out too?
Hi Mark, I’m testing out this code and having trouble. I combined it with your Noise-Plus-Palette code, and added in my own color palettes.
It compiles, it uploads, and it even runs (and looks quite awesome!). But halfway through the 3rd color (Yellow) it just freezes, and starts over again from the beginning, missing out on the other 16 palettes I programmed in. It’s quite strange, I can’t understand what’s programmed wrong. Usually if I have some code wrong somewhere it just won’t compile/upload at all. Do you see anything in my code here that I don’t?
My un-educated guess is that you’re quite right. Fortunately I have 5 Megas waiting at home for me that came in the mail while I was visiting family. Unfortunately my family’s LED Christmas gifts this year will have to deal with non-blended palettes. Oh well. I’ll bring home some Megas next year and upgrade them (and I’m sure I’ll have some way cooler code by then too!