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

◆ ColorFromPalette() [6/8]

CRGB fl::ColorFromPalette ( const CRGBPalette32 & pal,
uint8_t index,
uint8_t brightness,
TBlendType blendType )

Definition at line 520 of file colorutils.cpp.

521 {
522 if (blendType == LINEARBLEND_NOWRAP) {
523 index = map8(index, 0, 247); // Blend range is affected by lo3 blend of
524 // values, remap to avoid wrapping
525 }
526
527 uint8_t hi5 = index;
528#if defined(__AVR__)
529 hi5 /= 2;
530 hi5 /= 2;
531 hi5 /= 2;
532#else
533 hi5 >>= 3;
534#endif
535 uint8_t lo3 = index & 0x07;
536
537 // const CRGB* entry = &(pal[0]) + hi5;
538 // since hi5 is always 0..31, hi4 * sizeof(CRGB) can be a single-byte value,
539 // instead of the two byte 'int' that avr-gcc defaults to.
540 // So, we multiply hi5 X sizeof(CRGB), giving hi5XsizeofCRGB;
541 uint8_t hi5XsizeofCRGB = hi5 * sizeof(CRGB);
542 // We then add that to a base array pointer.
543 const CRGB *entry = (CRGB *)((uint8_t *)(&(pal[0])) + hi5XsizeofCRGB);
544
545 uint8_t red1 = entry->red;
546 uint8_t green1 = entry->green;
547 uint8_t blue1 = entry->blue;
548
549 uint8_t blend = lo3 && (blendType != NOBLEND);
550
551 if (blend) {
552
553 if (hi5 == 31) {
554 entry = &(pal[0]);
555 } else {
556 ++entry;
557 }
558
559 uint8_t f2 = lo3 << 5;
560 uint8_t f1 = 255 - f2;
561
562 uint8_t red2 = entry->red;
563 red1 = scale8_LEAVING_R1_DIRTY(red1, f1);
564 red2 = scale8_LEAVING_R1_DIRTY(red2, f2);
565 red1 += red2;
566
567 uint8_t green2 = entry->green;
568 green1 = scale8_LEAVING_R1_DIRTY(green1, f1);
569 green2 = scale8_LEAVING_R1_DIRTY(green2, f2);
570 green1 += green2;
571
572 uint8_t blue2 = entry->blue;
573 blue1 = scale8_LEAVING_R1_DIRTY(blue1, f1);
574 blue2 = scale8_LEAVING_R1_DIRTY(blue2, f2);
575 blue1 += blue2;
576
577 cleanup_R1();
578 }
579
580 if (brightness != 255) {
581 if (brightness) {
582 ++brightness; // adjust for rounding
583 // Now, since brightness is nonzero, we don't need the full
584 // scale8_video logic; we can just to scale8 and then add one
585 // (unless scale8 fixed) to all nonzero inputs.
586 if (red1) {
588#if !(FASTLED_SCALE8_FIXED == 1)
589 ++red1;
590#endif
591 }
592 if (green1) {
593 green1 = scale8_LEAVING_R1_DIRTY(green1, brightness);
594#if !(FASTLED_SCALE8_FIXED == 1)
595 ++green1;
596#endif
597 }
598 if (blue1) {
599 blue1 = scale8_LEAVING_R1_DIRTY(blue1, brightness);
600#if !(FASTLED_SCALE8_FIXED == 1)
601 ++blue1;
602#endif
603 }
604 cleanup_R1();
605 } else {
606 red1 = 0;
607 green1 = 0;
608 blue1 = 0;
609 }
610 }
611
612 return CRGB(red1, green1, blue1);
613}
UISlider brightness("Brightness", 1, 0, 1)
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
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:541
LIB8STATIC_ALWAYS_INLINE void cleanup_R1()
Clean up the r1 register after a series of *LEAVING_R1_DIRTY calls.
Definition scale8.h:339
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:176
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55

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

+ Here is the call graph for this function: