Hi, is it possible to define a gradient Palette at runtime?  the DEFINE_GRADIENT_PALETTE uses the

Hi,

is it possible to define a gradient Palette at runtime?

the DEFINE_GRADIENT_PALETTE uses the program memory.

In my situation I want to create a new Palette each frame dynamically, it is for a christmas candle effect where the gradient points are “candles” that change their brigntness based on an algorithm from here http://bienonline.magix.net/public/avr-advent.html (its german, but google translate is ok) and I want brigntness gradients between those virtual candles along a strip of 200 LEDs which I use as an indirect lighting in my living room. It shall give an effect to the room that it is lighted by a lot of flickering candles.

Yes, note the SetupXXXPalette functions in the ColorPalette example: https://github.com/FastLED/FastLED/blob/master/examples/ColorPalette/ColorPalette.ino

To my understanding those SetupXXXPalette functions create CRGBPalette16 objects, where 16 colors have an equal “distance” in the palette, while the gradient palettes define also the control points of the gradients. Sure, I can achieve something with the 16 element palettes but I have plenty of RAM so it would be nice to have the gradient function.

Yep, you can do this. Internally, all palettes are CRGBPalette16 (or 256), even the ones defined as a gradient – the gradient is just a convenient and compact way to define them.

So. You want this:

CRGBPalette16 myPal16;
myPal16.loadDynamicGradientPalette( dynPalDef);

That will load a gradient palette from ‘dynPalDef’ into the CRGBPalette16 named myPal16.

dynPalDef should just be an array of bytes in exactly the same format that you feed into DEFINE_GRADIENT_PALETTE:

index1, R1, G1, B1,
index2, R2, G2, B2,
index3, R3, G3, B3,
(etc until indexN == 255)

There’s no example code for this, but it is possible and it works. Set up a byte array with the gradient palette definition, create a CRGBPalette16, and then call myPal16.loadDynamicGradientPalette. Then use myPal16 as usual.

When you get it working, share your code here – other people might want to see it!

1 Like

great - it works - thank you very much!

For anybody curious, this is how I implemented this

typedef unsigned char byte;

byte bytes[8];

bytes[0] = 0;
bytes[1] = 255;
bytes[2] = 0;
bytes[3] = 0;

bytes[4] = 255;
bytes[5] = 0;
bytes[6] = 0;
bytes[7] = 255;

currentpalette.loadDynamicGradientPalette(bytes);

This is gold. I was trying to find a way to change the palette at runtime as well, as I have a web app that sends a request to my esp2866 server with the palette… so the palette can be created in the web app with a color picker.

Hello @twofingerrightclick

Glad you found a nugget! This is the FastLED G+ archives. Please visit our Reddit group for current discussions.

1 Like