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

◆ ColorFromPalette() [4/8]

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

645{
646 if ( blendType == LINEARBLEND_NOWRAP) {
647 index = map8(index, 0, 239); // Blend range is affected by lo4 blend of values, remap to avoid wrapping
648 }
649
650 // hi4 = index >> 4;
651 uint8_t hi4 = lsrX4(index);
652 uint8_t lo4 = index & 0x0F;
653
654 // const CRGB* entry = &(pal[0]) + hi4;
655 // since hi4 is always 0..15, hi4 * sizeof(CRGB) can be a single-byte value,
656 // instead of the two byte 'int' that avr-gcc defaults to.
657 // So, we multiply hi4 X sizeof(CRGB), giving hi4XsizeofCRGB;
658 uint8_t hi4XsizeofCRGB = hi4 * sizeof(CRGB);
659 // We then add that to a base array pointer.
660 const CRGB* entry = (CRGB*)( (uint8_t*)(&(pal[0])) + hi4XsizeofCRGB);
661
662 uint8_t blend = lo4 && (blendType != NOBLEND);
663
664 uint8_t red1 = entry->red;
665 uint8_t green1 = entry->green;
666 uint8_t blue1 = entry->blue;
667
668
669 if( blend ) {
670
671 if( hi4 == 15 ) {
672 entry = &(pal[0]);
673 } else {
674 ++entry;
675 }
676
677 uint8_t f2 = lo4 << 4;
678 uint8_t f1 = 255 - f2;
679
680 // rgb1.nscale8(f1);
681 uint8_t red2 = entry->red;
682 red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
683 red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
684 red1 += red2;
685
686 uint8_t green2 = entry->green;
687 green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
688 green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
689 green1 += green2;
690
691 uint8_t blue2 = entry->blue;
692 blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
693 blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
694 blue1 += blue2;
695
696 cleanup_R1();
697 }
698
699 if( brightness != 255) {
700 if( brightness ) {
701 ++brightness; // adjust for rounding
702 // Now, since brightness is nonzero, we don't need the full scale8_video logic;
703 // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
704 if( red1 ) {
705 red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
706#if !(FASTLED_SCALE8_FIXED==1)
707 ++red1;
708#endif
709 }
710 if( green1 ) {
711 green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
712#if !(FASTLED_SCALE8_FIXED==1)
713 ++green1;
714#endif
715 }
716 if( blue1 ) {
717 blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
718#if !(FASTLED_SCALE8_FIXED==1)
719 ++blue1;
720#endif
721 }
722 cleanup_R1();
723 } else {
724 red1 = 0;
725 green1 = 0;
726 blue1 = 0;
727 }
728 }
729
730 return CRGB( red1, green1, blue1);
731}
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.
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, lsrX4(), map8(), NOBLEND, and scale8_LEAVING_R1_DIRTY().

Referenced by bpm(), fl::DemoReel100::bpm(), computeOneTwinkle(), fl::TwinkleFox::computeOneTwinkle(), fl::Fire2012::draw(), fill_palette(), fill_palette_circular(), FillLEDsFromPaletteColors(), Fire2012WithPalette(), Fire2023(), loop(), map_data_into_colors_through_palette(), fl::NoisePalette::mapNoiseToLEDsUsingPalette(), mapNoiseToLEDsUsingPalette(), fl::WaveCrgbGradientMap::mapWaveToLEDs(), fl::Pacifica::pacifica_one_layer(), pacifica_one_layer(), UpscalePalette(), UpscalePalette(), UpscalePalette(), and UpscalePalette().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: