FastLED 3.9.15
Loading...
Searching...
No Matches

◆ loadDynamicGradientPalette()

CRGBPalette32 & CRGBPalette32::loadDynamicGradientPalette ( TDynamicRGBGradientPalette_bytes gpal)
inline

Creates a palette from a gradient palette in dynamic (heap) memory.

Gradient palettes are loaded into CRGBPalettes in such a way that, if possible, every color represented in the gradient palette is also represented in the CRGBPalette.

For example, consider a gradient palette that is all black except for a single, one-element-wide (1/256th!) spike of red in the middle:

0, 0,0,0
124, 0,0,0
125, 255,0,0 // one 1/256th-palette-wide red stripe
126, 0,0,0
255, 0,0,0

A naive conversion of this 256-element palette to a 16-element palette might accidentally completely eliminate the red spike, rendering the palette completely black.

However, the conversions provided here would attempt to include a the red stripe in the output, more-or-less as faithfully as possible. So in this case, the resulting CRGBPalette16 palette would have a red stripe in the middle which was 1/16th of a palette wide – the narrowest possible in a CRGBPalette16.

This means that the relative width of stripes in a CRGBPalette16 will be, by definition, different from the widths in the gradient palette. This code attempts to preserve "all the colors", rather than the exact stripe widths at the expense of dropping some colors.

Definition at line 1697 of file colorutils.h.

1698 {
1699 TRGBGradientPaletteEntryUnion* ent = (TRGBGradientPaletteEntryUnion*)(gpal);
1700 TRGBGradientPaletteEntryUnion u;
1701
1702 // Count entries
1703 uint16_t count = 0;
1704 do {
1705 u = *(ent + count);
1706 ++count;
1707 } while ( u.index != 255);
1708
1709 int8_t lastSlotUsed = -1;
1710
1711
1712 u = *ent;
1713 CRGB rgbstart( u.r, u.g, u.b);
1714
1715 int indexstart = 0;
1716 uint8_t istart8 = 0;
1717 uint8_t iend8 = 0;
1718 while( indexstart < 255) {
1719 ++ent;
1720 u = *ent;
1721 int indexend = u.index;
1722 CRGB rgbend( u.r, u.g, u.b);
1723 istart8 = indexstart / 8;
1724 iend8 = indexend / 8;
1725 if( count < 16) {
1726 if( (istart8 <= lastSlotUsed) && (lastSlotUsed < 31)) {
1727 istart8 = lastSlotUsed + 1;
1728 if( iend8 < istart8) {
1729 iend8 = istart8;
1730 }
1731 }
1732 lastSlotUsed = iend8;
1733 }
1734 fill_gradient_RGB( &(entries[0]), istart8, rgbstart, iend8, rgbend);
1735 indexstart = indexend;
1736 rgbstart = rgbend;
1737 }
1738 return *this;
1739 }
CRGB entries[32]
the color entries that make up the palette
void fill_gradient_RGB(CRGB *leds, uint16_t startpos, CRGB startcolor, uint16_t endpos, CRGB endcolor)
Fill a range of LEDs with a smooth RGB gradient between two RGB colors.

References CRGBPalette32(), entries, and fill_gradient_RGB().

+ Here is the call graph for this function: