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

◆ five_bit_bitshift() [2/2]

void fl::five_bit_bitshift ( uint16_t r16,
uint16_t g16,
uint16_t b16,
uint8_t brightness,
CRGB * out,
uint8_t * out_power_5bit )
inline

Definition at line 125 of file five_bit_hd_gamma.h.

127 {
128
129 // NEW in 3.10.2: A new closed form solution has been found!
130 // Thank you https://github.com/gwgill!
131 // It's okay if you don't know how this works, few do, but it tests
132 // very well and is better than the old iterative approach which had
133 // bad quantization issues (sudden jumps in brightness in certain intervals).
134
135 // ix/31 * 255/65536 * 256 scaling factors, valid for indexes 1..31
136 static uint32_t bright_scale[32] = {
137 0, 2023680, 1011840, 674560, 505920, 404736, 337280, 289097,
138 252960, 224853, 202368, 183971, 168640, 155668, 144549, 134912,
139 126480, 119040, 112427, 106509, 101184, 96366, 91985, 87986,
140 84320, 80947, 77834, 74951, 72274, 69782, 67456, 65280};
141
142 auto max3 = [](u16 a, u16 b, u16 c) { return fl_max(fl_max(a, b), c); };
143
144
145 if (brightness == 0) {
146 *out = CRGB(0, 0, 0);
147 *out_power_5bit = 0;
148 return;
149 }
150 if (r16 == 0 && g16 == 0 && b16 == 0) {
151 *out = CRGB(0, 0, 0);
152 *out_power_5bit = (brightness <= 31) ? brightness : 31;
153 return;
154 }
155
156 uint8_t r8 = 0, g8 = 0, b8 = 0;
157
158 // Apply any brightness setting (we assume brightness is 0..255)
159 if (brightness != 0xff) {
160 r16 = scale16by8(r16, brightness);
161 g16 = scale16by8(g16, brightness);
162 b16 = scale16by8(b16, brightness);
163 }
164
165 // Locate the largest value to set the brightness/scale factor
166 uint16_t scale = max3(r16, g16, b16);
167
168 if (scale == 0) {
169 *out = CRGB(0, 0, 0);
170 *out_power_5bit = 0;
171 return;
172 } else {
173 uint32_t scalef;
174
175 // Compute 5 bit quantized scale that is at or above the maximum value.
176 scale = (scale + (2047 - (scale >> 5))) >> 11;
177
178 // Adjust the 16 bit values to account for the scale, then round to 8
179 // bits
180 scalef = bright_scale[scale];
181 r8 = (r16 * scalef + 0x808000) >> 24;
182 g8 = (g16 * scalef + 0x808000) >> 24;
183 b8 = (b16 * scalef + 0x808000) >> 24;
184
185 *out = CRGB(r8, g8, b8);
186 *out_power_5bit = static_cast<uint8_t>(scale);
187 return;
188 }
189}
uint16_t scale
Definition Noise.ino:74
UISlider brightness("Brightness", 128, 0, 255, 1)
LIB8STATIC_ALWAYS_INLINE uint16_t scale16by8(uint16_t i, fract8 scale)
Scale a 16-bit unsigned value by an 8-bit value, which is treated as the numerator of a fraction whos...
Definition scale8.h:478
common_type_t< T, U > fl_max(T a, U b)
Definition math_macros.h:29

References brightness(), fl_max(), scale, and scale16by8().

+ Here is the call graph for this function: