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

◆ ColorFromPalette() [7/8]

CRGB ColorFromPalette ( const TProgmemRGBPalette16 & 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 779 of file colorutils.cpp.

780{
781 if ( blendType == LINEARBLEND_NOWRAP) {
782 index = map8(index, 0, 239); // Blend range is affected by lo4 blend of values, remap to avoid wrapping
783 }
784
785 // hi4 = index >> 4;
786 uint8_t hi4 = lsrX4(index);
787 uint8_t lo4 = index & 0x0F;
788
789 CRGB entry(FL_PGM_READ_DWORD_NEAR( &(pal[0]) + hi4 ));
790
791
792 uint8_t red1 = entry.red;
793 uint8_t green1 = entry.green;
794 uint8_t blue1 = entry.blue;
795
796 uint8_t blend = lo4 && (blendType != NOBLEND);
797
798 if( blend ) {
799
800 if( hi4 == 15 ) {
801 entry = FL_PGM_READ_DWORD_NEAR( &(pal[0]) );
802 } else {
803 entry = FL_PGM_READ_DWORD_NEAR( &(pal[1]) + hi4 );
804 }
805
806 uint8_t f2 = lo4 << 4;
807 uint8_t f1 = 255 - f2;
808
809 uint8_t red2 = entry.red;
810 red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
811 red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
812 red1 += red2;
813
814 uint8_t green2 = entry.green;
815 green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
816 green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
817 green1 += green2;
818
819 uint8_t blue2 = entry.blue;
820 blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
821 blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
822 blue1 += blue2;
823
824 cleanup_R1();
825 }
826
827 if( brightness != 255) {
828 if( brightness ) {
829 ++brightness; // adjust for rounding
830 // Now, since brightness is nonzero, we don't need the full scale8_video logic;
831 // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
832 if( red1 ) {
833 red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
834#if !(FASTLED_SCALE8_FIXED==1)
835 ++red1;
836#endif
837 }
838 if( green1 ) {
839 green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
840#if !(FASTLED_SCALE8_FIXED==1)
841 ++green1;
842#endif
843 }
844 if( blue1 ) {
845 blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
846#if !(FASTLED_SCALE8_FIXED==1)
847 ++blue1;
848#endif
849 }
850 cleanup_R1();
851 } else {
852 red1 = 0;
853 green1 = 0;
854 blue1 = 0;
855 }
856 }
857
858 return CRGB( red1, green1, blue1);
859}
UISlider brightness("Brightness", 255, 0, 255, 1)
uint8_t lsrX4(uint8_t dividend)
Helper function to divide a number by 16, aka four logical shift right (LSR)'s.
#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, lsrX4(), map8(), NOBLEND, and scale8_LEAVING_R1_DIRTY().

+ Here is the call graph for this function: