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

◆ ColorFromPalette() [3/8]

CHSV ColorFromPalette ( const CHSVPalette32 & pal,
uint8_t index,
uint8_t brightness = 255,
TBlendType blendType = LINEARBLEND )

Get a color from a palette.

These are the main functions for getting and using palette colors. Regardless of the number of entries in the base palette, this function will interpolate between entries to turn the discrete colors into a smooth gradient.

Parameters
palthe palette to retrieve the color from
indexthe position in the palette to retrieve the color for (0-255)
brightnessoptional brightness value to scale the resulting color
blendTypewhether to take the palette entries directly (NOBLEND) or blend linearly between palette entries (LINEARBLEND)

Definition at line 1201 of file colorutils.cpp.

1202{
1203 if ( blendType == LINEARBLEND_NOWRAP) {
1204 index = map8(index, 0, 247); // Blend range is affected by lo3 blend of values, remap to avoid wrapping
1205 }
1206
1207 uint8_t hi5 = index;
1208#if defined(__AVR__)
1209 hi5 /= 2;
1210 hi5 /= 2;
1211 hi5 /= 2;
1212#else
1213 hi5 >>= 3;
1214#endif
1215 uint8_t lo3 = index & 0x07;
1216
1217 uint8_t hi5XsizeofCHSV = hi5 * sizeof(CHSV);
1218 const CHSV* entry = (CHSV*)( (uint8_t*)(&(pal[0])) + hi5XsizeofCHSV);
1219
1220 uint8_t hue1 = entry->hue;
1221 uint8_t sat1 = entry->sat;
1222 uint8_t val1 = entry->val;
1223
1224 uint8_t blend = lo3 && (blendType != NOBLEND);
1225
1226 if( blend ) {
1227
1228 if( hi5 == 31 ) {
1229 entry = &(pal[0]);
1230 } else {
1231 ++entry;
1232 }
1233
1234 uint8_t f2 = lo3 << 5;
1235 uint8_t f1 = 255 - f2;
1236
1237 uint8_t hue2 = entry->hue;
1238 uint8_t sat2 = entry->sat;
1239 uint8_t val2 = entry->val;
1240
1241 // Now some special casing for blending to or from
1242 // either black or white. Black and white don't have
1243 // proper 'hue' of their own, so when ramping from
1244 // something else to/from black/white, we set the 'hue'
1245 // of the black/white color to be the same as the hue
1246 // of the other color, so that you get the expected
1247 // brightness or saturation ramp, with hue staying
1248 // constant:
1249
1250 // If we are starting from white (sat=0)
1251 // or black (val=0), adopt the target hue.
1252 if( sat1 == 0 || val1 == 0) {
1253 hue1 = hue2;
1254 }
1255
1256 // If we are ending at white (sat=0)
1257 // or black (val=0), adopt the starting hue.
1258 if( sat2 == 0 || val2 == 0) {
1259 hue2 = hue1;
1260 }
1261
1262
1263 sat1 = scale8_LEAVING_R1_DIRTY( sat1, f1);
1264 val1 = scale8_LEAVING_R1_DIRTY( val1, f1);
1265
1266 sat2 = scale8_LEAVING_R1_DIRTY( sat2, f2);
1267 val2 = scale8_LEAVING_R1_DIRTY( val2, f2);
1268
1269 // cleanup_R1();
1270
1271 // These sums can't overflow, so no qadd8 needed.
1272 sat1 += sat2;
1273 val1 += val2;
1274
1275 uint8_t deltaHue = (uint8_t)(hue2 - hue1);
1276 if( deltaHue & 0x80 ) {
1277 // go backwards
1278 hue1 -= scale8( 256 - deltaHue, f2);
1279 } else {
1280 // go forwards
1281 hue1 += scale8( deltaHue, f2);
1282 }
1283
1284 cleanup_R1();
1285 }
1286
1287 if( brightness != 255) {
1288 val1 = scale8_video( val1, brightness);
1289 }
1290
1291 return CHSV( hue1, sat1, val1);
1292}
UISlider brightness("Brightness", 255, 0, 255, 1)
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
Computes a new color blended some fraction of the way between two other colors.
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:559
@ NOBLEND
No interpolation between palette entries.
@ LINEARBLEND_NOWRAP
Linear interpolation between palette entries, but no wrap-around.
LIB8STATIC_ALWAYS_INLINE void cleanup_R1()
Clean up the r1 register after a series of *LEAVING_R1_DIRTY calls.
Definition scale8.h:333
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:170
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:117
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:34
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition chsv.h:16

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

+ Here is the call graph for this function: