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

◆ ColorFromPalette() [8/8]

CRGB ColorFromPalette ( const TProgmemRGBPalette32 & pal,
uint8_t index,
uint8_t brightness = 255,
TBlendType blendType = LINEARBLEND )

Get a color from a palette.

These are the main functions for getting and using palette colors. Regardless of the number of entries in the base palette, this function will interpolate between entries to turn the discrete colors into a smooth gradient.

Parameters
palthe palette to retrieve the color from
indexthe position in the palette to retrieve the color for (0-255)
brightnessoptional brightness value to scale the resulting color
blendTypewhether to take the palette entries directly (NOBLEND) or blend linearly between palette entries (LINEARBLEND)

Definition at line 957 of file colorutils.cpp.

958{
959 if ( blendType == LINEARBLEND_NOWRAP) {
960 index = map8(index, 0, 247); // Blend range is affected by lo3 blend of values, remap to avoid wrapping
961 }
962
963 uint8_t hi5 = index;
964#if defined(__AVR__)
965 hi5 /= 2;
966 hi5 /= 2;
967 hi5 /= 2;
968#else
969 hi5 >>= 3;
970#endif
971 uint8_t lo3 = index & 0x07;
972
973 CRGB entry(FL_PGM_READ_DWORD_NEAR( &(pal[0]) + hi5));
974
975 uint8_t red1 = entry.red;
976 uint8_t green1 = entry.green;
977 uint8_t blue1 = entry.blue;
978
979 uint8_t blend = lo3 && (blendType != NOBLEND);
980
981 if( blend ) {
982
983 if( hi5 == 31 ) {
984 entry = FL_PGM_READ_DWORD_NEAR( &(pal[0]) );
985 } else {
986 entry = FL_PGM_READ_DWORD_NEAR( &(pal[1]) + hi5 );
987 }
988
989 uint8_t f2 = lo3 << 5;
990 uint8_t f1 = 255 - f2;
991
992 uint8_t red2 = entry.red;
993 red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
994 red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
995 red1 += red2;
996
997 uint8_t green2 = entry.green;
998 green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
999 green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
1000 green1 += green2;
1001
1002 uint8_t blue2 = entry.blue;
1003 blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
1004 blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
1005 blue1 += blue2;
1006
1007 cleanup_R1();
1008 }
1009
1010 if( brightness != 255) {
1011 if( brightness ) {
1012 ++brightness; // adjust for rounding
1013 // Now, since brightness is nonzero, we don't need the full scale8_video logic;
1014 // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
1015 if( red1 ) {
1016 red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
1017#if !(FASTLED_SCALE8_FIXED==1)
1018 ++red1;
1019#endif
1020 }
1021 if( green1 ) {
1022 green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
1023#if !(FASTLED_SCALE8_FIXED==1)
1024 ++green1;
1025#endif
1026 }
1027 if( blue1 ) {
1028 blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
1029#if !(FASTLED_SCALE8_FIXED==1)
1030 ++blue1;
1031#endif
1032 }
1033 cleanup_R1();
1034 } else {
1035 red1 = 0;
1036 green1 = 0;
1037 blue1 = 0;
1038 }
1039 }
1040
1041 return CRGB( red1, green1, blue1);
1042}
UISlider brightness("Brightness", 255, 0, 255, 1)
#define FL_PGM_READ_DWORD_NEAR(x)
Read a double word (32-bit) from PROGMEM memory.
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
Computes a new color blended some fraction of the way between two other colors.
LIB8STATIC uint8_t map8(uint8_t in, uint8_t rangeStart, uint8_t rangeEnd)
Map from one full-range 8-bit value into a narrower range of 8-bit values, possibly a range of hues.
Definition lib8tion.h:559
@ NOBLEND
No interpolation between palette entries.
@ LINEARBLEND_NOWRAP
Linear interpolation between palette entries, but no wrap-around.
LIB8STATIC_ALWAYS_INLINE void cleanup_R1()
Clean up the r1 register after a series of *LEAVING_R1_DIRTY calls.
Definition scale8.h:333
LIB8STATIC_ALWAYS_INLINE uint8_t scale8_LEAVING_R1_DIRTY(uint8_t i, fract8 scale)
This version of scale8() does not clean up the R1 register on AVR.
Definition scale8.h:170
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54

References blend(), brightness, cleanup_R1(), FL_PGM_READ_DWORD_NEAR, LINEARBLEND_NOWRAP, map8(), NOBLEND, and scale8_LEAVING_R1_DIRTY().

+ Here is the call graph for this function: