I want to be able to create a palette on the fly but from

I want to be able to create a palette on the fly but from what I see palettes are statically defined such as:

DEFINE_GRADIENT_PALETTE( test ) {
0, 0, 0, 0,
255, 255, 255, 255
};

What I would like to do is change the starting color and ending colors while the program is running. Is there a way to do this?

Thanks!

It’s not required to be defined like that. Here’s a function that makes a palette with random colors each time it’s called.

// This function fills the palette with totally random colors.
void SetupTotallyRandomPalette()
{
for( int i = 0; i < 16; i++) {
currentPalette[i] = CHSV( random8(), 255, random8());
}
}

You could make a function that uses two colors you pass to it.

What if you don’t want to define all 16 points? How can you make it interpolate between fewer than 16 points? Whenever I try this with fewer than 16 points the ones in between just become black. @marmil

I found this seems to work. If you use the DEFINE_GRADIENT_PALETTE (which is really just array of bytes) you can’t change elements of the array since it’s in read only memory.

Like the macro, the first byte of each four is the anchor point of the color described by the next 3 bytes. The first byte of the last four bytes needs to be 255.

//myPal = heatmap_gp;

uint8_t xyz[12];

memset(xyz, 0, sizeof(xyz));

xyz[0] = 0; // anchor of first color - must be zero
xyz[1] = 255;
xyz[2] = 0;
xyz[3] = 0;

xyz[4] = 128; // anchor - value between 1 and 254
xyz[5] = 255;
xyz[6] = 255;
xyz[7] = 0;

xyz[8] = 255; // anchor of last color - must be 255
xyz[9] = 255,
xyz[10] = 0;
xyz[11] = 255;

myPal = xyz;

for (int p = 0; p < 256; p++)
leds[p] = ColorFromPalette(myPal, p, 255, NOBLEND);

FastLED.show();

I like how this looks to work @Mike_Brown1 . I tried to put this into a program and test on a Teensy 3.0 but couldn’t get it to upload. Do you have a working example?

@Daniel_Heppner Are you only looking to use two colors, or something between 2 and 16?

#include <FastLED.h>

#if FASTLED_VERSION < 3001000
#error “Requires FastLED 3.1 or later; check github for latest code.”
#endif

#define LED_PIN 1
#define NUM_LEDS 256
#define BRIGHTNESS 42
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB

CRGB leds[NUM_LEDS];

CRGBPalette256 myPal;

void setup() {

delay( 3000 ); // power-up safety delay

FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
FastLED.setBrightness( BRIGHTNESS );

//myPal = heatmap_gp;

uint8_t xyz[12];

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

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

xyz[8] = 255;
xyz[9] = 255,
xyz[10] = 127;
xyz[11] = 0;

myPal = xyz;

for (int p = 0; p < NUM_LEDS; p++) {
leds[p] = ColorFromPalette(myPal, p, 255, NOBLEND);
}
}

void loop () {
FastLED.show();
FastLED.delay(100);
}

@marmil Between 2 and 16.
@Mike_Brown1 That looks okay but a little funky looking. I guess I could write a wrapper function for that. I would like to use CRGB named values to make things easier.

Thanks @Mike_Brown1 . I played with it some more and have it working now. I saved my version here:

@Daniel_Heppner can take it the the next level and have the number of colors be a variable too! :slight_smile: