ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
display_color_utils.h
Go to the documentation of this file.
1#pragma once
3
4namespace esphome::display {
7inline static uint8_t esp_scale(uint8_t i, uint8_t scale, uint8_t max_value = 255) { return (max_value * i / scale); }
8
9class ColorUtil {
10 public:
11 static Color to_color(uint32_t colorcode, ColorOrder color_order,
12 ColorBitness color_bitness = ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned = true) {
13 uint8_t first_color, second_color, third_color;
14 uint8_t first_bits = 0;
15 uint8_t second_bits = 0;
16 uint8_t third_bits = 0;
17
18 switch (color_bitness) {
20 first_bits = 8;
21 second_bits = 8;
22 third_bits = 8;
23 break;
25 first_bits = 5;
26 second_bits = 6;
27 third_bits = 5;
28 break;
30 first_bits = 3;
31 second_bits = 3;
32 third_bits = 2;
33 break;
34 }
35
36 first_color = right_bit_aligned ? esp_scale(((colorcode >> (second_bits + third_bits)) & ((1 << first_bits) - 1)),
37 ((1 << first_bits) - 1))
38 : esp_scale(((colorcode >> 16) & 0xFF), (1 << first_bits) - 1);
39
40 second_color = right_bit_aligned
41 ? esp_scale(((colorcode >> third_bits) & ((1 << second_bits) - 1)), ((1 << second_bits) - 1))
42 : esp_scale(((colorcode >> 8) & 0xFF), ((1 << second_bits) - 1));
43
44 third_color = (right_bit_aligned ? esp_scale(((colorcode >> 0) & ((1 << third_bits) - 1)), ((1 << third_bits) - 1))
45 : esp_scale(((colorcode >> 0) & 0xFF), (1 << third_bits) - 1));
46
47 Color color_return;
48
49 switch (color_order) {
50 case COLOR_ORDER_RGB:
51 color_return.r = first_color;
52 color_return.g = second_color;
53 color_return.b = third_color;
54 break;
55 case COLOR_ORDER_BGR:
56 color_return.b = first_color;
57 color_return.g = second_color;
58 color_return.r = third_color;
59 break;
60 case COLOR_ORDER_GRB:
61 color_return.g = first_color;
62 color_return.r = second_color;
63 color_return.b = third_color;
64 break;
65 }
66 return color_return;
67 }
68 static inline Color rgb332_to_color(uint8_t rgb332_color) {
69 return to_color((uint32_t) rgb332_color, COLOR_ORDER_RGB, COLOR_BITNESS_332);
70 }
71 static uint8_t color_to_332(Color color, ColorOrder color_order = ColorOrder::COLOR_ORDER_RGB) {
72 uint16_t red_color, green_color, blue_color;
73
74 red_color = esp_scale8(color.red, ((1 << 3) - 1));
75 green_color = esp_scale8(color.green, ((1 << 3) - 1));
76 blue_color = esp_scale8(color.blue, (1 << 2) - 1);
77
78 switch (color_order) {
79 case COLOR_ORDER_RGB:
80 return red_color << 5 | green_color << 2 | blue_color;
81 case COLOR_ORDER_BGR:
82 return blue_color << 6 | green_color << 3 | red_color;
83 case COLOR_ORDER_GRB:
84 return green_color << 5 | red_color << 2 | blue_color;
85 }
86 return 0;
87 }
88 static uint16_t color_to_565(Color color, ColorOrder color_order = ColorOrder::COLOR_ORDER_RGB) {
89 uint16_t red_color, green_color, blue_color;
90
91 red_color = esp_scale8(color.red, ((1 << 5) - 1));
92 green_color = esp_scale8(color.green, ((1 << 6) - 1));
93 blue_color = esp_scale8(color.blue, (1 << 5) - 1);
94
95 switch (color_order) {
96 case COLOR_ORDER_RGB:
97 return red_color << 11 | green_color << 5 | blue_color;
98 case COLOR_ORDER_BGR:
99 return blue_color << 11 | green_color << 5 | red_color;
100 case COLOR_ORDER_GRB:
101 return green_color << 10 | red_color << 5 | blue_color;
102 }
103 return 0;
104 }
106 uint32_t gs4 = esp_scale8(color.white, 15);
107 return gs4;
108 }
109 /***
110 * Converts a Color value to an 8bit index using a 24bit 888 palette.
111 * Uses euclidiean distance to calculate the linear distance between
112 * two points in an RGB cube, then iterates through the full palette
113 * returning the closest match.
114 * @param[in] color The target color.
115 * @param[in] palette The 256*3 byte RGB palette.
116 * @return The 8 bit index of the closest color (e.g. for display buffer).
117 */
118 // static uint8_t color_to_index8_palette888(Color color, uint8_t *palette) {
119 static uint8_t color_to_index8_palette888(Color color, const uint8_t *palette) {
120 uint8_t closest_index = 0;
121 uint32_t minimum_dist2 = UINT32_MAX; // Smallest distance^2 to the target
122 // so far
123 // int8_t(*plt)[][3] = palette;
124 int16_t tgt_r = color.r;
125 int16_t tgt_g = color.g;
126 int16_t tgt_b = color.b;
127 uint16_t x, y, z;
128 // Loop through each row of the palette
129 for (uint16_t i = 0; i < 256; i++) {
130 // Get the pallet rgb color
131 int16_t plt_r = (int16_t) palette[i * 3 + 0];
132 int16_t plt_g = (int16_t) palette[i * 3 + 1];
133 int16_t plt_b = (int16_t) palette[i * 3 + 2];
134 // Calculate euclidean distance (linear distance in rgb cube).
135 x = (uint32_t) std::abs(tgt_r - plt_r);
136 y = (uint32_t) std::abs(tgt_g - plt_g);
137 z = (uint32_t) std::abs(tgt_b - plt_b);
138 uint32_t dist2 = x * x + y * y + z * z;
139 if (dist2 < minimum_dist2) {
140 minimum_dist2 = dist2;
141 closest_index = (uint8_t) i;
142 }
143 }
144 return closest_index;
145 }
146 /***
147 * Converts an 8bit palette index (e.g. from a display buffer) to a color.
148 * @param[in] index The index to look up.
149 * @param[in] palette The 256*3 byte RGB palette.
150 * @return The RGBW Color object looked up by the palette.
151 */
152 static Color index8_to_color_palette888(uint8_t index, const uint8_t *palette) {
153 Color color = Color(palette[index * 3 + 0], palette[index * 3 + 1], palette[index * 3 + 2], 0);
154 return color;
155 }
156};
157} // namespace esphome::display
static Color rgb332_to_color(uint8_t rgb332_color)
static uint32_t color_to_grayscale4(Color color)
static uint8_t color_to_index8_palette888(Color color, const uint8_t *palette)
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
static Color index8_to_color_palette888(uint8_t index, const uint8_t *palette)
static uint8_t color_to_332(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
static Color to_color(uint32_t colorcode, ColorOrder color_order, ColorBitness color_bitness=ColorBitness::COLOR_BITNESS_888, bool right_bit_aligned=true)
bool z
Definition msa3xx.h:1
static void uint32_t
uint8_t red
Definition color.h:31
uint8_t g
Definition color.h:34
uint8_t white
Definition color.h:43
uint8_t green
Definition color.h:35
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30
uint8_t blue
Definition color.h:39
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6