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

◆ five_bit_bitshift()

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

Definition at line 73 of file five_bit_hd_gamma.cpp.

74 {
75 if (brightness == 0) {
76 *out = CRGB(0, 0, 0);
77 *out_power_5bit = 0;
78 return 0;
79 }
80 if (r16 == 0 && g16 == 0 && b16 == 0) {
81 *out = CRGB(0, 0, 0);
82 *out_power_5bit = (brightness <= 31) ? brightness : 31;
83 return brightness;
84 }
85
86 // Note: One day someone smarter than me will come along and invent a closed
87 // form solution for this. However, the numerical method works extremely
88 // well and has been optimized to avoid division performance penalties as
89 // much as possible.
90
91 // Step 1: Initialize brightness
92 static const uint8_t kStartBrightness = 0b00010000;
93 uint8_t v5 = kStartBrightness;
94 // Step 2: Boost brightness by swapping power with the driver brightness.
96
97 // Step 3: Boost brightness of the color channels by swapping power with the
98 // driver brightness.
99 uint16_t max_component = max3(r16, g16, b16);
100 // five_bit_color_bitshift(&r16, &g16, &b16, &v5);
101 uint8_t shifts = brightness_bitshifter16(&v5, &max_component, 4, 2);
102 if (shifts) {
103 r16 = r16 << shifts;
104 g16 = g16 << shifts;
105 b16 = b16 << shifts;
106 }
107
108 // Step 4: scale by final brightness factor.
109 if (brightness != 0xff) {
110 r16 = scale16by8(r16, brightness);
111 g16 = scale16by8(g16, brightness);
112 b16 = scale16by8(b16, brightness);
113 }
114
115
116 // brighten hardware brightness by turning on low order bits
117 if (v5 > 1) {
118 // since v5 is a power of two, subtracting one will invert the leading bit
119 // and invert all the bits below it.
120 // Example: 0b00010000 -1 = 0b00001111
121 // So 0b00010000 | 0b00001111 = 0b00011111
122 v5 = v5 | (v5 - 1);
123 }
124 // Step 5: Convert back to 8-bit and output.
125 *out = CRGB(map16_to_8(r16), map16_to_8(g16), map16_to_8(b16));
126 *out_power_5bit = v5;
127 return brightness;
128}
UISlider brightness("Brightness", 255, 0, 255, 1)
uint8_t brightness_bitshifter8(uint8_t *brightness_src, uint8_t *brightness_dst, uint8_t max_shifts)
uint8_t brightness_bitshifter16(uint8_t *brightness_src, uint16_t *brightness_dst, uint8_t max_shifts, uint8_t steps=2)
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:468
LIB8STATIC_ALWAYS_INLINE uint8_t map16_to_8(uint16_t x)
Definition intmap.h:31
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54

References brightness, brightness_bitshifter16(), brightness_bitshifter8(), map16_to_8(), and scale16by8().

Referenced by __builtin_five_bit_hd_gamma_bitshift().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: