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

◆ ColorFromPalette() [3/8]

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

Definition at line 859 of file colorutils.cpp.

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

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

+ Here is the call graph for this function: