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

◆ ColorFromPalette() [1/8]

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

Definition at line 991 of file colorutils.cpp.hpp.

992 {
993 if (blendType == LINEARBLEND_NOWRAP) {
994 index = map8(index, 0, 239); // Blend range is affected by lo4 blend of
995 // values, remap to avoid wrapping
996 }
997
998 // hi4 = index >> 4;
999 fl::u8 hi4 = lsrX4(index);
1000 fl::u8 lo4 = index & 0x0F;
1001
1002 // CRGB rgb1 = pal[ hi4];
1003 const CHSV *entry = &(pal[0]) + hi4;
1004
1005 fl::u8 hue1 = entry->hue;
1006 fl::u8 sat1 = entry->sat;
1007 fl::u8 val1 = entry->val;
1008
1009 fl::u8 blend = lo4 && (blendType != NOBLEND);
1010
1011 if (blend) {
1012
1013 if (hi4 == 15) {
1014 entry = &(pal[0]);
1015 } else {
1016 ++entry;
1017 }
1018
1019 fl::u8 f2 = lo4 << 4;
1020 fl::u8 f1 = 255 - f2;
1021
1022 fl::u8 hue2 = entry->hue;
1023 fl::u8 sat2 = entry->sat;
1024 fl::u8 val2 = entry->val;
1025
1026 // Now some special casing for blending to or from
1027 // either black or white. Black and white don't have
1028 // proper 'hue' of their own, so when ramping from
1029 // something else to/from black/white, we set the 'hue'
1030 // of the black/white color to be the same as the hue
1031 // of the other color, so that you get the expected
1032 // brightness or saturation ramp, with hue staying
1033 // constant:
1034
1035 // If we are starting from white (sat=0)
1036 // or black (val=0), adopt the target hue.
1037 if (sat1 == 0 || val1 == 0) {
1038 hue1 = hue2;
1039 }
1040
1041 // If we are ending at white (sat=0)
1042 // or black (val=0), adopt the starting hue.
1043 if (sat2 == 0 || val2 == 0) {
1044 hue2 = hue1;
1045 }
1046
1047 sat1 = scale8_LEAVING_R1_DIRTY(sat1, f1);
1048 val1 = scale8_LEAVING_R1_DIRTY(val1, f1);
1049
1050 sat2 = scale8_LEAVING_R1_DIRTY(sat2, f2);
1051 val2 = scale8_LEAVING_R1_DIRTY(val2, f2);
1052
1053 // cleanup_R1();
1054
1055 // These sums can't overflow, so no qadd8 needed.
1056 sat1 += sat2;
1057 val1 += val2;
1058
1059 fl::u8 deltaHue = (fl::u8)(hue2 - hue1);
1060 if (deltaHue & 0x80) {
1061 // go backwards
1062 hue1 -= scale8(256 - deltaHue, f2);
1063 } else {
1064 // go forwards
1065 hue1 += scale8(deltaHue, f2);
1066 }
1067
1068 cleanup_R1();
1069 }
1070
1071 if (brightness != 255) {
1072 val1 = scale8_video(val1, brightness);
1073 }
1074
1075 return CHSV(hue1, sat1, val1);
1076}
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
fl::hsv8 CHSV
Definition chsv.h:11
LIB8STATIC fl::u8 map8(fl::u8 in, fl::u8 rangeStart, fl::u8 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:467
unsigned char u8
Definition s16x16x4.h:132
unsigned char u8
Definition stdint.h:131
fl::u8 lsrX4(fl::u8 dividend)
Helper function to divide a number by 16, aka four logical shift right (LSR)'s.

References blend(), brightness, lsrX4(), and map8().

+ Here is the call graph for this function: