Custom gradient palette not displaying properly.

Hello!

I send a gradient over from my app which is formatted like this:

90#4153083/0#14355470/100#

The first number is the direction. Every set of hashtags after that includes a 24bit RGB value and a number (0 to 100) to indicate its percent in the gradient.

I wanted to convert this to a FastLED palette to display on my led strips. I did so following this example.
The code to add the colours and anchors to an array is this:

rawp[index] = anch;
rawp[index + 1] = (rgb >> 16) & 0xFF;
rawp[index + 2] = (rgb >> 8) & 0xFF;
rawp[index + 3] = rgb & 0xFF;

rawp is a uint8_t array. I set the first value to the anchor point, and the next 3 to the r,g,b values (these are split out of the string via strtok) I then increment index by 4.
To display it I first convert the array to a CRGBPalette256 like so:

CRGBPalette256 pal;
pal = m_cd.rawPal;

I then loop over all leds and use ColorFromPalette

leds[j] = ColorFromPalette(pal, j * 255 / (m_clc->size() - 1), m_cd.bri, LINEARBLEND);

However, when I run this it displays a totally wrong gradient (there is a gradient it just shows two wrong colours). I double checked the RGB values that get unpacked (from the 24bit int) and they match what is shown on my app. I tried 2 standard FastLED palettes and they displayed correctly. I have also checked things like making sure I have allocated the correct size for the "rawp" array.

Does anyone know why the custom palette would not display correctly? (I do have my colour order set correctly, and a common ground)

An RGB colour is in cubic colour space. You have a start point colour but you also need an end point colour. Integers will not do because the smaller of the two colour components are not incremented by a whole number but by a fraction. So when you set that component you convert it to an int before using it.

Grumpy_Mike:
An RGB colour is in cubic colour space. You have a start point colour but you also need an end point colour.

That was it! I looked at the example to see how they did their end colour stop and I noticed the comment that I didn't before about the last stop needing to be 255. That's when I remembered gradients are a percentage from 0 to 100. All I needed to to do was map the incoming anchor point from 0 to 100 to 0 to 255.
Thanks for the help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.