FastLED 3.9.15
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages

◆ ColorFromPalette() [3/8]

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

Definition at line 858 of file colorutils.cpp.

859 {
860 if (blendType == LINEARBLEND_NOWRAP) {
861 index = map8(index, 0, 247); // Blend range is affected by lo3 blend of
862 // values, remap to avoid wrapping
863 }
864
865 uint8_t hi5 = index;
866#if defined(__AVR__)
867 hi5 /= 2;
868 hi5 /= 2;
869 hi5 /= 2;
870#else
871 hi5 >>= 3;
872#endif
873 uint8_t lo3 = index & 0x07;
874
875 uint8_t hi5XsizeofCHSV = hi5 * sizeof(CHSV);
876 const CHSV *entry = (CHSV *)((uint8_t *)(&(pal[0])) + hi5XsizeofCHSV);
877
878 uint8_t hue1 = entry->hue;
879 uint8_t sat1 = entry->sat;
880 uint8_t val1 = entry->val;
881
882 uint8_t blend = lo3 && (blendType != NOBLEND);
883
884 if (blend) {
885
886 if (hi5 == 31) {
887 entry = &(pal[0]);
888 } else {
889 ++entry;
890 }
891
892 uint8_t f2 = lo3 << 5;
893 uint8_t f1 = 255 - f2;
894
895 uint8_t hue2 = entry->hue;
896 uint8_t sat2 = entry->sat;
897 uint8_t val2 = entry->val;
898
899 // Now some special casing for blending to or from
900 // either black or white. Black and white don't have
901 // proper 'hue' of their own, so when ramping from
902 // something else to/from black/white, we set the 'hue'
903 // of the black/white color to be the same as the hue
904 // of the other color, so that you get the expected
905 // brightness or saturation ramp, with hue staying
906 // constant:
907
908 // If we are starting from white (sat=0)
909 // or black (val=0), adopt the target hue.
910 if (sat1 == 0 || val1 == 0) {
911 hue1 = hue2;
912 }
913
914 // If we are ending at white (sat=0)
915 // or black (val=0), adopt the starting hue.
916 if (sat2 == 0 || val2 == 0) {
917 hue2 = hue1;
918 }
919
920 sat1 = scale8_LEAVING_R1_DIRTY(sat1, f1);
921 val1 = scale8_LEAVING_R1_DIRTY(val1, f1);
922
923 sat2 = scale8_LEAVING_R1_DIRTY(sat2, f2);
924 val2 = scale8_LEAVING_R1_DIRTY(val2, f2);
925
926 // cleanup_R1();
927
928 // These sums can't overflow, so no qadd8 needed.
929 sat1 += sat2;
930 val1 += val2;
931
932 uint8_t deltaHue = (uint8_t)(hue2 - hue1);
933 if (deltaHue & 0x80) {
934 // go backwards
935 hue1 -= scale8(256 - deltaHue, f2);
936 } else {
937 // go forwards
938 hue1 += scale8(deltaHue, f2);
939 }
940
941 cleanup_R1();
942 }
943
944 if (brightness != 255) {
945 val1 = scale8_video(val1, brightness);
946 }
947
948 return CHSV(hue1, sat1, val1);
949}
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
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16

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

+ Here is the call graph for this function: