FastLED 3.10.8
Loading...
Searching...
No Matches

◆ ColorFromPalette() [3/8]

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

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

1161 {
1162 if (blendType == LINEARBLEND_NOWRAP) {
1163 index = map8(index, 0, 247); // Blend range is affected by lo3 blend of
1164 // values, remap to avoid wrapping
1165 }
1166
1167 fl::u8 hi5 = index;
1168#if defined(FL_IS_AVR)
1169 hi5 /= 2;
1170 hi5 /= 2;
1171 hi5 /= 2;
1172#else
1173 hi5 >>= 3;
1174#endif
1175 fl::u8 lo3 = index & 0x07;
1176
1177 fl::u8 hi5XsizeofCHSV = hi5 * sizeof(CHSV);
1178 const CHSV *entry = (CHSV *)((fl::u8 *)(&(pal[0])) + hi5XsizeofCHSV);
1179
1180 fl::u8 hue1 = entry->hue;
1181 fl::u8 sat1 = entry->sat;
1182 fl::u8 val1 = entry->val;
1183
1184 fl::u8 blend = lo3 && (blendType != NOBLEND);
1185
1186 if (blend) {
1187
1188 if (hi5 == 31) {
1189 entry = &(pal[0]);
1190 } else {
1191 ++entry;
1192 }
1193
1194 fl::u8 f2 = lo3 << 5;
1195 fl::u8 f1 = 255 - f2;
1196
1197 fl::u8 hue2 = entry->hue;
1198 fl::u8 sat2 = entry->sat;
1199 fl::u8 val2 = entry->val;
1200
1201 // Now some special casing for blending to or from
1202 // either black or white. Black and white don't have
1203 // proper 'hue' of their own, so when ramping from
1204 // something else to/from black/white, we set the 'hue'
1205 // of the black/white color to be the same as the hue
1206 // of the other color, so that you get the expected
1207 // brightness or saturation ramp, with hue staying
1208 // constant:
1209
1210 // If we are starting from white (sat=0)
1211 // or black (val=0), adopt the target hue.
1212 if (sat1 == 0 || val1 == 0) {
1213 hue1 = hue2;
1214 }
1215
1216 // If we are ending at white (sat=0)
1217 // or black (val=0), adopt the starting hue.
1218 if (sat2 == 0 || val2 == 0) {
1219 hue2 = hue1;
1220 }
1221
1222 sat1 = scale8_LEAVING_R1_DIRTY(sat1, f1);
1223 val1 = scale8_LEAVING_R1_DIRTY(val1, f1);
1224
1225 sat2 = scale8_LEAVING_R1_DIRTY(sat2, f2);
1226 val2 = scale8_LEAVING_R1_DIRTY(val2, f2);
1227
1228 // cleanup_R1();
1229
1230 // These sums can't overflow, so no qadd8 needed.
1231 sat1 += sat2;
1232 val1 += val2;
1233
1234 fl::u8 deltaHue = (fl::u8)(hue2 - hue1);
1235 if (deltaHue & 0x80) {
1236 // go backwards
1237 hue1 -= scale8(256 - deltaHue, f2);
1238 } else {
1239 // go forwards
1240 hue1 += scale8(deltaHue, f2);
1241 }
1242
1243 cleanup_R1();
1244 }
1245
1246 if (brightness != 255) {
1247 val1 = scale8_video(val1, brightness);
1248 }
1249
1250 return CHSV(hue1, sat1, val1);
1251}
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NO_EXCEPT
fl::hsv8 CHSV
Definition chsv.h:11
LIB8STATIC fl::u8 map8(fl::u8 in, fl::u8 rangeStart, fl::u8 rangeEnd) FL_NO_EXCEPT
Map from one full-range 8-bit value into a narrower range of 8-bit values, possibly a range of hues.
Definition lib8tion.h:468
unsigned char u8
Definition stdint.h:131

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

+ Here is the call graph for this function: