Color Palette Crossfades Example code showing how to continuously cross-fade between multiple color palettes

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.

Ooh, totally going to use this when switching color palettes in Aurora. Thanks!

Wonderful addition Mark!

Yeah! That´s way more than I hoped to get. Powerful feature. Thanks, Mark!

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? :slight_smile:

Huh. Yeah. Lemme check what happened.

Sorry about that-- should be available now.

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 :smiley:

I’m trying to run this one and having the same "no nblendPaletteTowardPalette() error. I did just download the newest code I could find at https://github.com/FastLED/FastLED/tree/FastLED3.1

Is there a different branch I should know about?

@Erin_St_Blaine ​ git checkout FastLED3.1 # (it’s in a different branch than master)

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?

I just grabbed it today and I thought I was on the 3.1 branch. Hm. I might just not understand how to use github…

The new fuctions are in the latest v3.1 branch: https://github.com/FastLED/FastLED/tree/FastLED3.1
The example itself is here: https://gist.github.com/kriegsman/1f7ccbbfa492a73c015e
Does that help, @Erin_St_Blaine ?

Yes that worked. Thank you!

I only just saw this! Gonna try and reprogram my lamp with this before I leave for home! Thanks!

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?

http://pastebin.com/7GxvK11f

Without having looked at the code in depth (yet), in going to guess that it’s running out of RAM. I’ll try to take a look tonight.

Ohhhhh. You mean the Uno that I’m running it on doesn’t have enough RAM to complete the process. Would using a 2560 Mega fix that?

(This is the first time anything like this has happened)

Yeah, I’m reading this right now:
http://arduino.cc/en/Tutorial/Memory

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 :smiley: (and I’m sure I’ll have some way cooler code by then too!