FastLED 3.10.6
Loading...
Searching...
No Matches

◆ ColorFromPalette() [1/8]

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

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

1073 {
1074 if (blendType == LINEARBLEND_NOWRAP) {
1075 index = map8(index, 0, 239); // Blend range is affected by lo4 blend of
1076 // values, remap to avoid wrapping
1077 }
1078
1079 // hi4 = index >> 4;
1080 fl::u8 hi4 = lsrX4(index);
1081 fl::u8 lo4 = index & 0x0F;
1082
1083 // CRGB rgb1 = pal[ hi4];
1084 const CHSV *entry = &(pal[0]) + hi4;
1085
1086 fl::u8 hue1 = entry->hue;
1087 fl::u8 sat1 = entry->sat;
1088 fl::u8 val1 = entry->val;
1089
1090 fl::u8 blend = lo4 && (blendType != NOBLEND);
1091
1092 if (blend) {
1093
1094 if (hi4 == 15) {
1095 entry = &(pal[0]);
1096 } else {
1097 ++entry;
1098 }
1099
1100 fl::u8 f2 = lo4 << 4;
1101 fl::u8 f1 = 255 - f2;
1102
1103 fl::u8 hue2 = entry->hue;
1104 fl::u8 sat2 = entry->sat;
1105 fl::u8 val2 = entry->val;
1106
1107 // Now some special casing for blending to or from
1108 // either black or white. Black and white don't have
1109 // proper 'hue' of their own, so when ramping from
1110 // something else to/from black/white, we set the 'hue'
1111 // of the black/white color to be the same as the hue
1112 // of the other color, so that you get the expected
1113 // brightness or saturation ramp, with hue staying
1114 // constant:
1115
1116 // If we are starting from white (sat=0)
1117 // or black (val=0), adopt the target hue.
1118 if (sat1 == 0 || val1 == 0) {
1119 hue1 = hue2;
1120 }
1121
1122 // If we are ending at white (sat=0)
1123 // or black (val=0), adopt the starting hue.
1124 if (sat2 == 0 || val2 == 0) {
1125 hue2 = hue1;
1126 }
1127
1128 sat1 = scale8_LEAVING_R1_DIRTY(sat1, f1);
1129 val1 = scale8_LEAVING_R1_DIRTY(val1, f1);
1130
1131 sat2 = scale8_LEAVING_R1_DIRTY(sat2, f2);
1132 val2 = scale8_LEAVING_R1_DIRTY(val2, f2);
1133
1134 // cleanup_R1();
1135
1136 // These sums can't overflow, so no qadd8 needed.
1137 sat1 += sat2;
1138 val1 += val2;
1139
1140 fl::u8 deltaHue = (fl::u8)(hue2 - hue1);
1141 if (deltaHue & 0x80) {
1142 // go backwards
1143 hue1 -= scale8(256 - deltaHue, f2);
1144 } else {
1145 // go forwards
1146 hue1 += scale8(deltaHue, f2);
1147 }
1148
1149 cleanup_R1();
1150 }
1151
1152 if (brightness != 255) {
1153 val1 = scale8_video(val1, brightness);
1154 }
1155
1156 return CHSV(hue1, sat1, val1);
1157}
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 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: