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

◆ ColorFromPalette() [1/8]

CHSV fl::ColorFromPalette ( const CHSVPalette16 & pal,
uint8_t index,
uint8_t brightness,
TBlendType blendType )

Definition at line 771 of file colorutils.cpp.

772 {
773 if (blendType == LINEARBLEND_NOWRAP) {
774 index = map8(index, 0, 239); // Blend range is affected by lo4 blend of
775 // values, remap to avoid wrapping
776 }
777
778 // hi4 = index >> 4;
779 uint8_t hi4 = lsrX4(index);
780 uint8_t lo4 = index & 0x0F;
781
782 // CRGB rgb1 = pal[ hi4];
783 const CHSV *entry = &(pal[0]) + hi4;
784
785 uint8_t hue1 = entry->hue;
786 uint8_t sat1 = entry->sat;
787 uint8_t val1 = entry->val;
788
789 uint8_t blend = lo4 && (blendType != NOBLEND);
790
791 if (blend) {
792
793 if (hi4 == 15) {
794 entry = &(pal[0]);
795 } else {
796 ++entry;
797 }
798
799 uint8_t f2 = lo4 << 4;
800 uint8_t f1 = 255 - f2;
801
802 uint8_t hue2 = entry->hue;
803 uint8_t sat2 = entry->sat;
804 uint8_t val2 = entry->val;
805
806 // Now some special casing for blending to or from
807 // either black or white. Black and white don't have
808 // proper 'hue' of their own, so when ramping from
809 // something else to/from black/white, we set the 'hue'
810 // of the black/white color to be the same as the hue
811 // of the other color, so that you get the expected
812 // brightness or saturation ramp, with hue staying
813 // constant:
814
815 // If we are starting from white (sat=0)
816 // or black (val=0), adopt the target hue.
817 if (sat1 == 0 || val1 == 0) {
818 hue1 = hue2;
819 }
820
821 // If we are ending at white (sat=0)
822 // or black (val=0), adopt the starting hue.
823 if (sat2 == 0 || val2 == 0) {
824 hue2 = hue1;
825 }
826
827 sat1 = scale8_LEAVING_R1_DIRTY(sat1, f1);
828 val1 = scale8_LEAVING_R1_DIRTY(val1, f1);
829
830 sat2 = scale8_LEAVING_R1_DIRTY(sat2, f2);
831 val2 = scale8_LEAVING_R1_DIRTY(val2, f2);
832
833 // cleanup_R1();
834
835 // These sums can't overflow, so no qadd8 needed.
836 sat1 += sat2;
837 val1 += val2;
838
839 uint8_t deltaHue = (uint8_t)(hue2 - hue1);
840 if (deltaHue & 0x80) {
841 // go backwards
842 hue1 -= scale8(256 - deltaHue, f2);
843 } else {
844 // go forwards
845 hue1 += scale8(deltaHue, f2);
846 }
847
848 cleanup_R1();
849 }
850
851 if (brightness != 255) {
852 val1 = scale8_video(val1, brightness);
853 }
854
855 return CHSV(hue1, sat1, val1);
856}
UISlider brightness("Brightness", 255, 0, 255, 1)
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
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:541
LIB8STATIC_ALWAYS_INLINE void cleanup_R1()
Clean up the r1 register after a series of *LEAVING_R1_DIRTY calls.
Definition scale8.h:339
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:176
LIB8STATIC_ALWAYS_INLINE uint8_t scale8_video(uint8_t i, fract8 scale)
The "video" version of scale8() guarantees that the output will be only be zero if one or both of the...
Definition scale8.h:123
LIB8STATIC_ALWAYS_INLINE uint8_t scale8(uint8_t i, fract8 scale)
Scale one byte by a second one, which is treated as the numerator of a fraction whose denominator is ...
Definition scale8.h:40
uint8_t lsrX4(uint8_t dividend)
Helper function to divide a number by 16, aka four logical shift right (LSR)'s.
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16

References blend(), brightness, cleanup_R1(), lsrX4(), map8(), scale8(), scale8_LEAVING_R1_DIRTY(), and scale8_video().

+ Here is the call graph for this function: