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

◆ ColorFromPalette() [3/8]

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

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

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

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

+ Here is the call graph for this function: