10inline static uint8_t esp_scale(uint8_t i, uint8_t scale, uint8_t max_value = 255) {
return (max_value * i / scale); }
50 template<ColorBitness BITNESS>
53 const size_t idx = index * 2;
55 return (
ptr[idx] << 8) +
ptr[idx + 1];
56 return ptr[idx] + (
ptr[idx + 1] << 8);
59 const size_t idx = index * 3;
60 return ptr[idx + 0] + (
ptr[idx + 1] << 8) + (
ptr[idx + 2] << 16);
68 bool right_bit_aligned =
true) {
69 uint8_t first_bits = 0;
70 uint8_t second_bits = 0;
71 uint8_t third_bits = 0;
93 ?
expand_field((colorcode >> (second_bits + third_bits)) & ((1 << first_bits) - 1), first_bits)
94 : esp_scale(((colorcode >> 16) & 0xFF), (1 << first_bits) - 1);
96 const uint8_t green = right_bit_aligned
97 ?
expand_field((colorcode >> third_bits) & ((1 << second_bits) - 1), second_bits)
98 : esp_scale(((colorcode >> 8) & 0xFF), ((1 << second_bits) - 1));
100 const uint8_t blue = right_bit_aligned ?
expand_field(colorcode & ((1 << third_bits) - 1), third_bits)
101 : esp_scale(((colorcode >> 0) & 0xFF), (1 << third_bits) - 1);
103 return Color(red, green, blue);
106 ESPDEPRECATED(
"Packed colors are always RGB, drop the ColorOrder argument. Removed in 2027.5.0",
"2026.11.0")
111 static inline Color rgb332_to_color(uint8_t rgb332_color) {
114 static uint8_t color_to_332(
Color color) {
115 const uint16_t red_color = esp_scale8(color.
red, ((1 << 3) - 1));
116 const uint16_t green_color = esp_scale8(color.
green, ((1 << 3) - 1));
117 const uint16_t blue_color = esp_scale8(color.
blue, (1 << 2) - 1);
118 return red_color << 5 | green_color << 2 | blue_color;
121 ESPDEPRECATED(
"Packed colors are always RGB, drop the ColorOrder argument. Removed in 2027.5.0",
"2026.11.0")
124 return color_to_332(color);
126 const uint16_t red_color = esp_scale8(color.red, ((1 << 3) - 1));
127 const uint16_t green_color = esp_scale8(color.green, ((1 << 3) - 1));
129 return esp_scale8(color.blue, (1 << 2) - 1) << 6 | green_color << 3 | red_color;
130 return green_color << 5 | red_color << 2 | esp_scale8(color.blue, (1 << 2) - 1);
132 static uint16_t color_to_565(Color color) {
133 const uint16_t red_color = esp_scale8(color.red, ((1 << 5) - 1));
134 const uint16_t green_color = esp_scale8(color.green, ((1 << 6) - 1));
135 const uint16_t blue_color = esp_scale8(color.blue, (1 << 5) - 1);
136 return red_color << 11 | green_color << 5 | blue_color;
139 ESPDEPRECATED(
"Packed colors are always RGB, drop the ColorOrder argument. Removed in 2027.5.0",
"2026.11.0")
143 return color_to_565(Color(color.b, color.g, color.r));
146 return esp_scale8(color.green, (1 << 6) - 1) << 10 | esp_scale8(color.red, (1 << 5) - 1) << 5 |
147 esp_scale8(color.blue, (1 << 5) - 1);
149 return color_to_565(color);
152 static uint32_t color_to_grayscale4(Color color) {
153 uint32_t gs4 = esp_scale8(color.white, 15);
166 static uint8_t color_to_index8_palette888(Color color,
const uint8_t *palette) {
167 uint8_t closest_index = 0;
168 uint32_t minimum_dist2 = UINT32_MAX;
171 int16_t tgt_r = color.r;
172 int16_t tgt_g = color.g;
173 int16_t tgt_b = color.b;
176 for (uint16_t i = 0; i < 256; i++) {
178 int16_t plt_r = (int16_t) palette[i * 3 + 0];
179 int16_t plt_g = (int16_t) palette[i * 3 + 1];
180 int16_t plt_b = (int16_t) palette[i * 3 + 2];
186 if (dist2 < minimum_dist2) {
187 minimum_dist2 = dist2;
188 closest_index = (uint8_t) i;
191 return closest_index;
199 static Color index8_to_color_palette888(uint8_t index,
const uint8_t *palette) {
200 Color color = Color(palette[index * 3 + 0], palette[index * 3 + 1], palette[index * 3 + 2], 0);
static Color to_color(uint32_t colorcode, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
Unpacks a color of the given bitness, red in the most significant bits.
constexpr uint8_t expand_field(uint8_t value, uint8_t bits)
esp_scale(value, (1 << bits) - 1) for a field of 2, 3, 5, 6 or 8 bits, as a multiply and shift that g...