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

◆ ColorFromPalette() [1/8]

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

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

1074 {
1075 if (blendType == LINEARBLEND_NOWRAP) {
1076 index = map8(index, 0, 239); // Blend range is affected by lo4 blend of
1077 // values, remap to avoid wrapping
1078 }
1079
1080 // hi4 = index >> 4;
1081 fl::u8 hi4 = lsrX4(index);
1082 fl::u8 lo4 = index & 0x0F;
1083
1084 // CRGB rgb1 = pal[ hi4];
1085 const CHSV *entry = &(pal[0]) + hi4;
1086
1087 fl::u8 hue1 = entry->hue;
1088 fl::u8 sat1 = entry->sat;
1089 fl::u8 val1 = entry->val;
1090
1091 fl::u8 blend = lo4 && (blendType != NOBLEND);
1092
1093 if (blend) {
1094
1095 if (hi4 == 15) {
1096 entry = &(pal[0]);
1097 } else {
1098 ++entry;
1099 }
1100
1101 fl::u8 f2 = lo4 << 4;
1102 fl::u8 f1 = 255 - f2;
1103
1104 fl::u8 hue2 = entry->hue;
1105 fl::u8 sat2 = entry->sat;
1106 fl::u8 val2 = entry->val;
1107
1108 // Now some special casing for blending to or from
1109 // either black or white. Black and white don't have
1110 // proper 'hue' of their own, so when ramping from
1111 // something else to/from black/white, we set the 'hue'
1112 // of the black/white color to be the same as the hue
1113 // of the other color, so that you get the expected
1114 // brightness or saturation ramp, with hue staying
1115 // constant:
1116
1117 // If we are starting from white (sat=0)
1118 // or black (val=0), adopt the target hue.
1119 if (sat1 == 0 || val1 == 0) {
1120 hue1 = hue2;
1121 }
1122
1123 // If we are ending at white (sat=0)
1124 // or black (val=0), adopt the starting hue.
1125 if (sat2 == 0 || val2 == 0) {
1126 hue2 = hue1;
1127 }
1128
1129 sat1 = scale8_LEAVING_R1_DIRTY(sat1, f1);
1130 val1 = scale8_LEAVING_R1_DIRTY(val1, f1);
1131
1132 sat2 = scale8_LEAVING_R1_DIRTY(sat2, f2);
1133 val2 = scale8_LEAVING_R1_DIRTY(val2, f2);
1134
1135 // cleanup_R1();
1136
1137 // These sums can't overflow, so no qadd8 needed.
1138 sat1 += sat2;
1139 val1 += val2;
1140
1141 fl::u8 deltaHue = (fl::u8)(hue2 - hue1);
1142 if (deltaHue & 0x80) {
1143 // go backwards
1144 hue1 -= scale8(256 - deltaHue, f2);
1145 } else {
1146 // go forwards
1147 hue1 += scale8(deltaHue, f2);
1148 }
1149
1150 cleanup_R1();
1151 }
1152
1153 if (brightness != 255) {
1154 val1 = scale8_video(val1, brightness);
1155 }
1156
1157 return CHSV(hue1, sat1, val1);
1158}
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
fl::u8 lsrX4(fl::u8 dividend) FL_NO_EXCEPT
Helper function to divide a number by 16, aka four logical shift right (LSR)'s.

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

+ Here is the call graph for this function: