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

◆ ColorFromPalette() [3/8]

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

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

1079 {
1080 if (blendType == LINEARBLEND_NOWRAP) {
1081 index = map8(index, 0, 247); // Blend range is affected by lo3 blend of
1082 // values, remap to avoid wrapping
1083 }
1084
1085 fl::u8 hi5 = index;
1086#if defined(FL_IS_AVR)
1087 hi5 /= 2;
1088 hi5 /= 2;
1089 hi5 /= 2;
1090#else
1091 hi5 >>= 3;
1092#endif
1093 fl::u8 lo3 = index & 0x07;
1094
1095 fl::u8 hi5XsizeofCHSV = hi5 * sizeof(CHSV);
1096 const CHSV *entry = (CHSV *)((fl::u8 *)(&(pal[0])) + hi5XsizeofCHSV);
1097
1098 fl::u8 hue1 = entry->hue;
1099 fl::u8 sat1 = entry->sat;
1100 fl::u8 val1 = entry->val;
1101
1102 fl::u8 blend = lo3 && (blendType != NOBLEND);
1103
1104 if (blend) {
1105
1106 if (hi5 == 31) {
1107 entry = &(pal[0]);
1108 } else {
1109 ++entry;
1110 }
1111
1112 fl::u8 f2 = lo3 << 5;
1113 fl::u8 f1 = 255 - f2;
1114
1115 fl::u8 hue2 = entry->hue;
1116 fl::u8 sat2 = entry->sat;
1117 fl::u8 val2 = entry->val;
1118
1119 // Now some special casing for blending to or from
1120 // either black or white. Black and white don't have
1121 // proper 'hue' of their own, so when ramping from
1122 // something else to/from black/white, we set the 'hue'
1123 // of the black/white color to be the same as the hue
1124 // of the other color, so that you get the expected
1125 // brightness or saturation ramp, with hue staying
1126 // constant:
1127
1128 // If we are starting from white (sat=0)
1129 // or black (val=0), adopt the target hue.
1130 if (sat1 == 0 || val1 == 0) {
1131 hue1 = hue2;
1132 }
1133
1134 // If we are ending at white (sat=0)
1135 // or black (val=0), adopt the starting hue.
1136 if (sat2 == 0 || val2 == 0) {
1137 hue2 = hue1;
1138 }
1139
1140 sat1 = scale8_LEAVING_R1_DIRTY(sat1, f1);
1141 val1 = scale8_LEAVING_R1_DIRTY(val1, f1);
1142
1143 sat2 = scale8_LEAVING_R1_DIRTY(sat2, f2);
1144 val2 = scale8_LEAVING_R1_DIRTY(val2, f2);
1145
1146 // cleanup_R1();
1147
1148 // These sums can't overflow, so no qadd8 needed.
1149 sat1 += sat2;
1150 val1 += val2;
1151
1152 fl::u8 deltaHue = (fl::u8)(hue2 - hue1);
1153 if (deltaHue & 0x80) {
1154 // go backwards
1155 hue1 -= scale8(256 - deltaHue, f2);
1156 } else {
1157 // go forwards
1158 hue1 += scale8(deltaHue, f2);
1159 }
1160
1161 cleanup_R1();
1162 }
1163
1164 if (brightness != 255) {
1165 val1 = scale8_video(val1, brightness);
1166 }
1167
1168 return CHSV(hue1, sat1, val1);
1169}
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 s16x16x4.h:132
unsigned char u8
Definition stdint.h:131

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

+ Here is the call graph for this function: