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

◆ ColorFromPalette() [6/8]

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

Definition at line 521 of file colorutils.cpp.

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

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

+ Here is the call graph for this function: