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

◆ ColorFromPalette() [6/8]

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

863{
864 if ( blendType == LINEARBLEND_NOWRAP) {
865 index = map8(index, 0, 247); // Blend range is affected by lo3 blend of values, remap to avoid wrapping
866 }
867
868 uint8_t hi5 = index;
869#if defined(__AVR__)
870 hi5 /= 2;
871 hi5 /= 2;
872 hi5 /= 2;
873#else
874 hi5 >>= 3;
875#endif
876 uint8_t lo3 = index & 0x07;
877
878 // const CRGB* entry = &(pal[0]) + hi5;
879 // since hi5 is always 0..31, hi4 * sizeof(CRGB) can be a single-byte value,
880 // instead of the two byte 'int' that avr-gcc defaults to.
881 // So, we multiply hi5 X sizeof(CRGB), giving hi5XsizeofCRGB;
882 uint8_t hi5XsizeofCRGB = hi5 * sizeof(CRGB);
883 // We then add that to a base array pointer.
884 const CRGB* entry = (CRGB*)( (uint8_t*)(&(pal[0])) + hi5XsizeofCRGB);
885
886 uint8_t red1 = entry->red;
887 uint8_t green1 = entry->green;
888 uint8_t blue1 = entry->blue;
889
890 uint8_t blend = lo3 && (blendType != NOBLEND);
891
892 if( blend ) {
893
894 if( hi5 == 31 ) {
895 entry = &(pal[0]);
896 } else {
897 ++entry;
898 }
899
900 uint8_t f2 = lo3 << 5;
901 uint8_t f1 = 255 - f2;
902
903 uint8_t red2 = entry->red;
904 red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
905 red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
906 red1 += red2;
907
908 uint8_t green2 = entry->green;
909 green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
910 green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
911 green1 += green2;
912
913 uint8_t blue2 = entry->blue;
914 blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
915 blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
916 blue1 += blue2;
917
918 cleanup_R1();
919
920 }
921
922 if( brightness != 255) {
923 if( brightness ) {
924 ++brightness; // adjust for rounding
925 // Now, since brightness is nonzero, we don't need the full scale8_video logic;
926 // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
927 if( red1 ) {
928 red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
929#if !(FASTLED_SCALE8_FIXED==1)
930 ++red1;
931#endif
932 }
933 if( green1 ) {
934 green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
935#if !(FASTLED_SCALE8_FIXED==1)
936 ++green1;
937#endif
938 }
939 if( blue1 ) {
940 blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
941#if !(FASTLED_SCALE8_FIXED==1)
942 ++blue1;
943#endif
944 }
945 cleanup_R1();
946 } else {
947 red1 = 0;
948 green1 = 0;
949 blue1 = 0;
950 }
951 }
952
953 return CRGB( red1, green1, blue1);
954}
UISlider brightness("Brightness", 255, 0, 255, 1)
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(), LINEARBLEND_NOWRAP, map8(), NOBLEND, and scale8_LEAVING_R1_DIRTY().

+ Here is the call graph for this function: