ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
color_picker.h
Go to the documentation of this file.
1#pragma once
2
3#include "lvgl_esphome.h"
6
7#include <algorithm>
8#include <array>
9#include <cmath>
10#include <cstdio>
11#include <numbers>
12
13#ifdef USE_LVGL_COLOR_PICKER
14
15namespace esphome::lvgl {
16
17// The hue gradients need seven stops; gradient.py raises the limit when a colour picker is in use.
18static_assert(LV_GRADIENT_MAX_STOPS >= 7, "color_picker needs LV_GRADIENT_MAX_STOPS >= 7");
19
21 public:
31
40
41 explicit LvColorPickerType(uint8_t sliders, bool use_wheel) : sliders_mask_(sliders), use_wheel_(use_wheel) {}
42
43 // Returns null for a slider the widget was not built with.
44 lv_obj_t *get_slider(size_t index) const { return this->sliders_[index]; }
45
46 void set_tint_knobs(bool tint_knobs) { this->tint_knobs_ = tint_knobs; }
47
48 void set_color(lv_color_t color) {
49 lv_color32_t c32 = lv_color_to_32(color, LV_OPA_COVER);
50 this->state_ = Color(c32.red, c32.green, c32.blue);
51 if (this->obj != nullptr)
52 this->update_color_();
53 }
54 Color get_color() const { return this->state_; }
55
56 void set_obj(lv_obj_t *outer) override {
59 // The far end of these two follows the current hue.
60 init_bar(&this->saturation_bar_, LV_GRAD_DIR_HOR, lv_color_white(), lv_color_hex(0xFF0000));
61 init_bar(&this->brightness_bar_, LV_GRAD_DIR_HOR, lv_color_black(), lv_color_white());
62
63 auto channels = static_cast<int32_t>(this->enabled_(SLIDER_RED)) +
64 static_cast<int32_t>(this->enabled_(SLIDER_GREEN)) +
65 static_cast<int32_t>(this->enabled_(SLIDER_BLUE));
66
67 // No layout on the outer object, so LV_SIZE_CONTENT uses the size from self_size_cb.
68 lv_obj_add_event_cb(outer, self_size_cb, LV_EVENT_GET_SELF_SIZE, nullptr);
69 lv_obj_add_event_cb(outer, size_changed_cb, LV_EVENT_SIZE_CHANGED, this);
70 lv_obj_set_style_pad_all(outer, KNOB_OVERHANG, LV_PART_MAIN);
71
72 int32_t left_width = 0;
73 lv_obj_t *left = nullptr;
74 if (this->enabled_(SLIDER_HUE)) {
75 left_width = HUE_WIDTH_PCT;
76 left = this->create_slider_(outer, SLIDER_HUE, "Hue", true, &hue_bar, 360);
77 lv_obj_set_size(left, lv_pct(HUE_WIDTH_PCT), lv_pct(100));
78 }
79#ifdef USE_LVGL_COLOR_PICKER_WHEEL
80 else if (this->use_wheel_) {
81 left_width = WHEEL_WIDTH_PCT;
82 left = this->create_wheel_(outer);
83 lv_obj_set_size(left, lv_pct(WHEEL_WIDTH_PCT), lv_pct(WHEEL_WIDTH_PCT));
84 }
85#endif
86 if (left != nullptr) {
87 lv_obj_set_style_align(left, LV_ALIGN_LEFT_MID, LV_PART_MAIN);
88 // A narrow hue column may be slimmer than its label.
89 lv_obj_set_flag(left, LV_OBJ_FLAG_OVERFLOW_VISIBLE, true);
90 }
91
92 // The text rows keep their natural height and the bars between them take what is left,
93 // so a small widget gets thinner bars rather than clipped text.
94 auto *middle = create_flex(outer, LV_FLEX_FLOW_COLUMN, LV_FLEX_ALIGN_START);
95 lv_obj_set_style_align(middle, LV_ALIGN_RIGHT_MID, LV_PART_MAIN);
96 lv_obj_set_style_pad_row(middle, KNOB_OVERHANG, LV_PART_MAIN);
97 lv_obj_set_size(middle, lv_pct(100 - left_width), lv_pct(100));
98
99 // A dark bar needs light text, unlike every other value label.
100 if (this->enabled_(SLIDER_BRIGHTNESS))
101 this->create_row_slider_(middle, SLIDER_BRIGHTNESS, "Brightness", &this->brightness_bar_, lv_color_white());
102
103 auto *inner = create_flex(middle, LV_FLEX_FLOW_ROW, LV_FLEX_ALIGN_START);
104 lv_obj_set_width(inner, lv_pct(100));
105 lv_obj_set_flex_grow(inner, 1);
106
107 // The channel bars keep a fixed width each, so dropping one widens the swatch.
108 auto channels_width = CHANNEL_WIDTH_PCT * channels;
109 auto *indicator_container = create_flex(inner, LV_FLEX_FLOW_COLUMN, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
110 lv_obj_set_size(indicator_container, lv_pct(CONTENT_WIDTH_PCT - channels_width), lv_pct(100));
111 lv_obj_set_style_pad_ver(indicator_container, KNOB_OVERHANG, LV_PART_MAIN);
112 lv_obj_set_style_pad_row(indicator_container, KNOB_OVERHANG, LV_PART_MAIN);
113
114 this->color_text_ = lv_label_create(indicator_container);
115 this->color_indicator_ = lv_obj_create(indicator_container);
116 lv_obj_set_style_border_color(this->color_indicator_, lv_color_hex(0x808080), LV_PART_MAIN);
117 lv_obj_set_style_border_width(this->color_indicator_, 1, LV_PART_MAIN);
118 lv_obj_set_style_radius(this->color_indicator_, 0, LV_PART_MAIN);
119 lv_obj_set_width(this->color_indicator_, lv_pct(90));
120 lv_obj_set_flex_grow(this->color_indicator_, 1);
121
122 if (channels != 0) {
123 auto *rgb_container = create_flex(inner, LV_FLEX_FLOW_ROW, LV_FLEX_ALIGN_SPACE_BETWEEN);
124 lv_obj_set_size(rgb_container, lv_pct(channels_width), lv_pct(100));
125 struct ChannelInfo {
126 SliderIndex index;
127 const char *name;
128 const lv_grad_dsc_t *grad;
129 };
130 const std::array<ChannelInfo, 3> channel_info{{
131 {SLIDER_RED, "R", &red_bar},
132 {SLIDER_GREEN, "G", &green_bar},
133 {SLIDER_BLUE, "B", &blue_bar},
134 }};
135 for (const auto &channel : channel_info) {
136 if (!this->enabled_(channel.index))
137 continue;
138 auto *container = this->create_slider_(rgb_container, channel.index, channel.name, true, channel.grad, 255);
139 lv_obj_set_size(container, lv_pct(100 / channels), lv_pct(100));
140 this->add_value_label_(channel.index, lv_color_black());
141 }
142 }
143
144 if (this->enabled_(SLIDER_SATURATION))
145 this->create_row_slider_(middle, SLIDER_SATURATION, "Saturation", &this->saturation_bar_, lv_color_black());
146
147 this->update_color_();
148
149 for (size_t index = 0; index < SLIDER_COUNT; index++) {
150 if (this->sliders_[index] == nullptr)
151 continue;
152 lv_obj_add_event_cb(this->sliders_[index], index <= SLIDER_BRIGHTNESS ? hsv_changed_cb : rgb_changed_cb,
153 LV_EVENT_VALUE_CHANGED, this);
154 }
155
156 bubble_events(outer);
157 }
158
159 protected:
160 // LVGL keeps only pointers to gradients and styles, so one copy serves every colour picker.
161 static void init_shared() {
162 if (shared_ready)
163 return;
164 shared_ready = true;
165
166 // Clockwise from red at 3 o'clock, matching how lv_color_hsv_to_rgb() reads the angle.
167 std::array<lv_color_t, 7> hues{lv_color_hex(0xFF0000), lv_color_hex(0xFFFF00), lv_color_hex(0x00FF00),
168 lv_color_hex(0x00FFFF), lv_color_hex(0x0000FF), lv_color_hex(0xFF00FF),
169 lv_color_hex(0xFF0000)};
170#ifdef USE_LVGL_COLOR_PICKER_WHEEL
171 lv_grad_conical_init(&wheel_hue_grad, lv_pct(50), lv_pct(50), 0, 360, LV_GRAD_EXTEND_PAD);
172 lv_grad_init_stops(&wheel_hue_grad, hues.data(), nullptr, nullptr, static_cast<int>(hues.size()));
173 // White at the centre fading out at the rim, giving saturation over the hue wheel.
174 const lv_color_t whites[] = {lv_color_white(), lv_color_white()};
175 const lv_opa_t white_opas[] = {LV_OPA_COVER, LV_OPA_TRANSP};
176 lv_grad_radial_init(&wheel_white_grad, lv_pct(50), lv_pct(50), lv_pct(100), lv_pct(50), LV_GRAD_EXTEND_PAD);
177 lv_grad_init_stops(&wheel_white_grad, whites, white_opas, nullptr, 2);
178#endif
179 // A vertical slider has its minimum at the bottom, so the hue bar runs the other way.
180 std::reverse(hues.begin(), hues.end());
181 lv_grad_vertical_init(&hue_bar);
182 lv_grad_init_stops(&hue_bar, hues.data(), nullptr, nullptr, static_cast<int>(hues.size()));
183
184 init_bar(&red_bar, LV_GRAD_DIR_VER, lv_color_hex(0xFF0000), lv_color_white());
185 init_bar(&green_bar, LV_GRAD_DIR_VER, lv_color_hex(0x00FF00), lv_color_white());
186 init_bar(&blue_bar, LV_GRAD_DIR_VER, lv_color_hex(0x0000FF), lv_color_white());
187
188 lv_style_init(&plain_box);
189 lv_style_set_bg_opa(&plain_box, LV_OPA_TRANSP);
190 lv_style_set_border_width(&plain_box, 0);
191 lv_style_set_pad_all(&plain_box, 0);
192 lv_style_set_pad_row(&plain_box, 0);
193 lv_style_set_pad_column(&plain_box, 0);
194
197 }
198
199 static void init_bar(lv_grad_dsc_t *grad, lv_grad_dir_t dir, lv_color_t from, lv_color_t to) {
200 const lv_color_t colors[] = {from, to};
201 grad->dir = dir;
202 lv_grad_init_stops(grad, colors, nullptr, nullptr, 2);
203 }
204
205 static void init_slider_styles(lv_style_t *bar, lv_style_t *knob, bool vertical) {
206 lv_style_init(bar);
207 lv_style_set_align(bar, LV_ALIGN_CENTER);
208 lv_style_set_bg_opa(bar, LV_OPA_COVER);
209 lv_style_set_pad_all(bar, 0);
210 lv_style_set_radius(bar, 0);
211 // A horizontal bar's thickness is set by update_bar_thickness_().
212 if (vertical) {
213 lv_style_set_width(bar, lv_pct(50));
214 lv_style_set_height(bar, lv_pct(90));
215 } else {
216 lv_style_set_width(bar, lv_pct(90));
217 }
218
219 lv_style_init(knob);
220 lv_style_set_bg_color(knob, lv_color_white());
221 lv_style_set_border_width(knob, 1);
222 lv_style_set_outline_color(knob, lv_color_black());
223 lv_style_set_radius(knob, 0);
224 // Both axes are set, or the theme picks a knob size the layout has not left room for.
225 int32_t along = -KNOB_TRIM;
226 int32_t across = KNOB_OVERHANG;
227 lv_style_set_pad_top(knob, vertical ? along : across);
228 lv_style_set_pad_bottom(knob, vertical ? along : across);
229 lv_style_set_pad_left(knob, vertical ? across : along);
230 lv_style_set_pad_right(knob, vertical ? across : along);
231 }
232
233 // A borderless, unpadded container.
234 static lv_obj_t *create_box(lv_obj_t *parent) {
235 auto *obj = lv_obj_create(parent);
236 lv_obj_add_style(obj, &plain_box, LV_PART_MAIN);
237 lv_obj_set_flag(obj, LV_OBJ_FLAG_SCROLLABLE, false);
238 return obj;
239 }
240
241 static lv_obj_t *create_flex(lv_obj_t *parent, lv_flex_flow_t flow, lv_flex_align_t main_place,
242 lv_flex_align_t track_place = LV_FLEX_ALIGN_START) {
243 auto *obj = create_box(parent);
244 lv_obj_set_layout(obj, LV_LAYOUT_FLEX);
245 lv_obj_set_flex_flow(obj, flow);
246 lv_obj_set_flex_align(obj, main_place, LV_FLEX_ALIGN_CENTER, track_place);
247 return obj;
248 }
249
250 // Preferred size, in lines of the text font. The widget is square.
251 static constexpr int SELF_SIZE_FONT_MULTIPLE = 16;
252
253 // At either end of its travel half the knob lies outside the bar. KNOB_TRIM pulls it back
254 // along the bar, and KNOB_OVERHANG makes it stand out across the bar.
255 static constexpr int KNOB_TRIM = 4;
256 static constexpr int KNOB_OVERHANG = 4;
257
258 // Layout proportions, as percentages.
259 static constexpr int32_t HUE_WIDTH_PCT = 15;
260 static constexpr int32_t CHANNEL_WIDTH_PCT = 19;
261 static constexpr int32_t CONTENT_WIDTH_PCT = 99;
262 static constexpr int32_t WHEEL_WIDTH_PCT = 45;
263 static constexpr int32_t WHEEL_HANDLE_SIZE = 14;
264
265 bool enabled_(SliderIndex index) const { return (this->sliders_mask_ & (1 << index)) != 0; }
266 bool has_(SliderIndex index) const { return this->sliders_[index] != nullptr; }
275
276 // A slider the widget was built without keeps the value held internally.
277 int32_t slider_value_(SliderIndex index, int32_t fallback) const {
278 return this->has_(index) ? lv_slider_get_value(this->sliders_[index]) : fallback;
279 }
280
281 void set_slider_value_(SliderIndex index, int32_t value) {
282 if (this->has_(index))
283 lv_slider_set_value(this->sliders_[index], value, LV_ANIM_OFF);
284 }
285
286 // Builds a slider with a name label above it, and returns its container.
287 lv_obj_t *create_slider_(lv_obj_t *parent, SliderIndex index, const char *name, bool vertical,
288 const lv_grad_dsc_t *grad, int32_t max) {
289 // A column keeps the label on its own row however large the font is.
290 auto *container = create_flex(parent, LV_FLEX_FLOW_COLUMN, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER);
291 if (vertical) {
292 // How far the knob reaches past the ends depends on the font, so follow style changes.
293 update_knob_clearance(container);
294 lv_obj_add_event_cb(container, knob_clearance_cb, LV_EVENT_STYLE_CHANGED, nullptr);
295 } else {
296 lv_obj_set_style_pad_bottom(container, KNOB_OVERHANG, LV_PART_MAIN);
297 lv_obj_set_style_pad_row(container, KNOB_OVERHANG, LV_PART_MAIN);
298 }
299 lv_label_set_text(lv_label_create(container), name);
300
301 auto *slider = lv_slider_create(container);
302 this->sliders_[index] = slider;
303 lv_obj_add_style(slider, vertical ? &slider_vert : &slider_horz, LV_PART_MAIN);
304 lv_obj_set_style_bg_grad(slider, grad, LV_PART_MAIN);
305 lv_obj_set_style_bg_opa(slider, LV_OPA_TRANSP, LV_PART_INDICATOR);
306 lv_obj_add_style(slider, vertical ? &slider_vert_knob : &slider_horz_knob, LV_PART_KNOB);
307 if (vertical)
308 lv_obj_set_flex_grow(slider, 1);
309 lv_slider_set_range(slider, 0, max);
310 lv_slider_set_mode(slider, LV_SLIDER_MODE_NORMAL);
311 return container;
312 }
313
314 void create_row_slider_(lv_obj_t *parent, SliderIndex index, const char *name, const lv_grad_dsc_t *grad,
315 lv_color_t text_color) {
316 auto *container = this->create_slider_(parent, index, name, false, grad, 100);
317 lv_obj_set_size(container, lv_pct(100), LV_SIZE_CONTENT);
318 this->add_value_label_(index, text_color);
319 }
320
321#ifdef USE_LVGL_COLOR_PICKER_WHEEL
322 // A full-size circle filled with a gradient.
323 static lv_obj_t *create_disc(lv_obj_t *parent, const lv_grad_dsc_t *grad) {
324 auto *disc = create_box(parent);
325 lv_obj_set_size(disc, lv_pct(100), lv_pct(100));
326 lv_obj_set_style_align(disc, LV_ALIGN_CENTER, LV_PART_MAIN);
327 lv_obj_set_style_radius(disc, LV_RADIUS_CIRCLE, LV_PART_MAIN);
328 lv_obj_set_style_bg_opa(disc, LV_OPA_COVER, LV_PART_MAIN);
329 lv_obj_set_style_bg_grad(disc, grad, LV_PART_MAIN);
330 lv_obj_set_flag(disc, LV_OBJ_FLAG_SCROLLABLE, false);
331 return disc;
332 }
333
334 // Builds the colour wheel that replaces the hue and saturation sliders, and returns its container.
335 lv_obj_t *create_wheel_(lv_obj_t *parent) {
336 auto *container = create_box(parent);
337 this->wheel_ = create_disc(container, &wheel_hue_grad);
338 // Not clickable, so touches fall through to the wheel underneath.
339 auto *overlay = create_disc(this->wheel_, &wheel_white_grad);
340 lv_obj_set_flag(overlay, LV_OBJ_FLAG_CLICKABLE, false);
341
342 this->wheel_handle_ = lv_obj_create(this->wheel_);
343 lv_obj_set_size(this->wheel_handle_, WHEEL_HANDLE_SIZE, WHEEL_HANDLE_SIZE);
344 lv_obj_set_style_align(this->wheel_handle_, LV_ALIGN_CENTER, LV_PART_MAIN);
345 lv_obj_set_style_radius(this->wheel_handle_, LV_RADIUS_CIRCLE, LV_PART_MAIN);
346 lv_obj_set_style_bg_opa(this->wheel_handle_, LV_OPA_TRANSP, LV_PART_MAIN);
347 lv_obj_set_style_border_width(this->wheel_handle_, 2, LV_PART_MAIN);
348 lv_obj_set_style_border_color(this->wheel_handle_, lv_color_white(), LV_PART_MAIN);
349 lv_obj_set_flag(this->wheel_handle_, LV_OBJ_FLAG_CLICKABLE, false);
350 lv_obj_set_flag(this->wheel_handle_, LV_OBJ_FLAG_SCROLLABLE, false);
351
352 // PRESSED as well, so a tap takes effect without the finger moving.
353 lv_obj_add_event_cb(this->wheel_, wheel_touch_cb, LV_EVENT_PRESSED, this);
354 lv_obj_add_event_cb(this->wheel_, wheel_touch_cb, LV_EVENT_PRESSING, this);
355 // The handle can only be placed once the wheel has been laid out.
356 lv_obj_add_event_cb(this->wheel_, wheel_size_cb, LV_EVENT_SIZE_CHANGED, this);
357 return container;
358 }
359#endif
360
361 void add_value_label_(SliderIndex index, lv_color_t color) {
362 auto *slider = this->sliders_[index];
363 lv_obj_set_flag(slider, LV_OBJ_FLAG_OVERFLOW_VISIBLE, true);
364 auto *label = lv_label_create(slider);
365 this->values_[index] = label;
366 lv_obj_set_style_align(label, LV_ALIGN_CENTER, LV_PART_MAIN);
367 lv_obj_set_style_text_color(label, color, LV_PART_MAIN);
368 }
369
370 // Leaves room at the ends of a vertical slider for the overhanging knob: half a line of text.
371 static void update_knob_clearance(lv_obj_t *container) {
372 const auto *font = lv_obj_get_style_text_font(container, LV_PART_MAIN);
373 auto clearance = static_cast<int32_t>(lv_font_get_line_height(font)) / 2;
374 if (clearance == lv_obj_get_style_pad_bottom(container, LV_PART_MAIN))
375 return;
376 lv_obj_set_style_pad_bottom(container, clearance, LV_PART_MAIN);
377 lv_obj_set_style_pad_row(container, clearance, LV_PART_MAIN);
378 }
379
380 static void knob_clearance_cb(lv_event_t *event) { update_knob_clearance(lv_event_get_current_target_obj(event)); }
381
382 static LvColorPickerType *from_event(lv_event_t *event) {
383 return static_cast<LvColorPickerType *>(lv_event_get_user_data(event));
384 }
385 static void hsv_changed_cb(lv_event_t *event) { from_event(event)->update_hsv_(); }
386 static void rgb_changed_cb(lv_event_t *event) { from_event(event)->update_rgb_(); }
387#ifdef USE_LVGL_COLOR_PICKER_WHEEL
388 static void wheel_touch_cb(lv_event_t *event) { from_event(event)->update_from_wheel_touch_(); }
389 static void wheel_size_cb(lv_event_t *event) { from_event(event)->update_wheel_handle_(); }
390#endif
391 static void size_changed_cb(lv_event_t *event) { from_event(event)->update_bar_thickness_(); }
392
393 // Gives the horizontal bars the thickness of the vertical ones, half a hue column, so every
394 // knob is the same size. A knob is sized from the thickness of its bar.
396 auto thickness = lv_obj_get_content_width(this->obj) * HUE_WIDTH_PCT / 200;
397 for (auto index : {SLIDER_SATURATION, SLIDER_BRIGHTNESS}) {
398 if (this->has_(index) && lv_obj_get_style_height(this->sliders_[index], LV_PART_MAIN) != thickness)
399 lv_obj_set_style_height(this->sliders_[index], thickness, LV_PART_MAIN);
400 }
401 }
402
403 // LVGL also counts this size when working out the scroll area, so a widget given a fixed
404 // width must not claim it, or it would scroll whenever it is smaller.
405 static void self_size_cb(lv_event_t *event) {
406 auto *obj = lv_event_get_current_target_obj(event);
407 if (lv_obj_get_style_width(obj, LV_PART_MAIN) != LV_SIZE_CONTENT)
408 return;
409 const auto *font = lv_obj_get_style_text_font(obj, LV_PART_MAIN);
410 auto self_size = static_cast<int32_t>(lv_font_get_line_height(font)) * SELF_SIZE_FONT_MULTIPLE;
411 auto *point = static_cast<lv_point_t *>(lv_event_get_param(event));
412 point->x = self_size;
413 point->y = self_size;
414 }
415
416 void update_text_() const {
417 // "#" + RRGGBB + null
418 char buf[8] = "#";
419 const uint8_t rgb[] = {this->state_.r, this->state_.g, this->state_.b};
420 format_hex_pretty_to(buf + 1, sizeof(buf) - 1, rgb, sizeof(rgb), 0);
421 lv_label_set_text(this->color_text_, buf);
422 }
423
424 void update_hue_bar_(SliderIndex index, lv_grad_dsc_t *grad, lv_color_t color) {
425 if (!this->has_(index))
426 return;
427 grad->stops[1].color = color;
428 lv_obj_invalidate(this->sliders_[index]);
429 }
430
431 void update_values_() const {
432 for (size_t index = 0; index < SLIDER_COUNT; index++) {
433 if (this->values_[index] == nullptr)
434 continue;
435 // up to 3 digits + null
436 char buf[4];
437 snprintf(buf, sizeof(buf), "%d", static_cast<int>(lv_slider_get_value(this->sliders_[index])));
438 lv_label_set_text(this->values_[index], buf);
439 }
440 }
441
442 void tint_knob_(SliderIndex index, lv_color_t color) const {
443 if (this->has_(index))
444 lv_obj_set_style_bg_color(this->sliders_[index], color, LV_PART_KNOB);
445 }
446
447 // Tints each knob with the colour of its bar at the current value. The brightness knob
448 // shows the selected colour instead, since its bar leaves out saturation.
449 void update_knobs_(lv_color_t full_hue) const {
450 if (!this->tint_knobs_)
451 return;
452 this->tint_knob_(SLIDER_HUE, full_hue);
453 this->tint_knob_(SLIDER_SATURATION, lv_color_hsv_to_rgb(this->hue_, this->saturation_, 100));
454 this->tint_knob_(SLIDER_BRIGHTNESS, this->state_);
455 this->tint_knob_(SLIDER_RED, lv_color_make(255, 255 - this->state_.r, 255 - this->state_.r));
456 this->tint_knob_(SLIDER_GREEN, lv_color_make(255 - this->state_.g, 255, 255 - this->state_.g));
457 this->tint_knob_(SLIDER_BLUE, lv_color_make(255 - this->state_.b, 255 - this->state_.b, 255));
458 }
459
460#ifdef USE_LVGL_COLOR_PICKER_WHEEL
461 // Returns the wheel's radius, which is zero until it has been laid out.
462 double wheel_radius_(lv_area_t *area) const {
463 lv_obj_get_coords(this->wheel_, area);
464 return static_cast<double>(area->x2 - area->x1) / 2.0;
465 }
466
468 if (this->wheel_handle_ == nullptr)
469 return;
470 lv_area_t area;
471 auto radius = this->wheel_radius_(&area);
472 if (radius <= 0)
473 return;
474 auto angle_rad = static_cast<double>(this->hue_) * (std::numbers::pi / 180.0);
475 auto distance = (static_cast<double>(this->saturation_) / 100.0) * radius;
476 lv_obj_set_style_translate_x(this->wheel_handle_, static_cast<int32_t>(std::lround(distance * std::cos(angle_rad))),
477 LV_PART_MAIN);
478 lv_obj_set_style_translate_y(this->wheel_handle_, static_cast<int32_t>(std::lround(distance * std::sin(angle_rad))),
479 LV_PART_MAIN);
480 }
481
482 // The angle from the centre gives the hue, the distance the saturation. Brightness is unchanged.
484 lv_indev_t *indev = lv_indev_active();
485 if (indev == nullptr)
486 return;
487 lv_point_t point;
488 lv_indev_get_point(indev, &point);
489 lv_area_t area;
490 auto radius = this->wheel_radius_(&area);
491 if (radius <= 0)
492 return;
493 auto dx = point.x - (area.x1 + area.x2) / 2.0;
494 auto dy = point.y - (area.y1 + area.y2) / 2.0;
495 auto angle = std::atan2(dy, dx) * (180.0 / std::numbers::pi);
496 if (angle < 0)
497 angle += 360.0;
498 this->hue_ = static_cast<uint16_t>(std::lround(angle)) % 360;
499 auto distance = std::sqrt(dx * dx + dy * dy);
500 this->saturation_ = static_cast<uint8_t>(std::lround(std::min(100.0, distance * 100.0 / radius)));
501 this->brightness_ = static_cast<uint8_t>(this->slider_value_(SLIDER_BRIGHTNESS, this->brightness_));
502 this->apply_hsv_();
503 // Only sliders send this themselves; without it on_value would not fire for the wheel.
504 lv_obj_send_event(this->wheel_, LV_EVENT_VALUE_CHANGED, nullptr);
505 }
506#endif
507
508 // Lets events from the internal sliders reach the widget, where the triggers listen. Every
509 // object in between needs the flag; the widget itself does not, so events stop there.
510 static void bubble_events(lv_obj_t *obj) {
511 for (uint32_t i = 0; i < lv_obj_get_child_count(obj); i++) {
512 auto *child = lv_obj_get_child(obj, i);
513 lv_obj_set_flag(child, LV_OBJ_FLAG_EVENT_BUBBLE, true);
514 bubble_events(child);
515 }
516 }
517
518 void refresh_() {
519 lv_obj_set_style_bg_color(this->color_indicator_, this->state_, LV_PART_MAIN);
520 auto full_hue = lv_color_hsv_to_rgb(this->hue_, 100, 100);
521 this->update_hue_bar_(SLIDER_SATURATION, &this->saturation_bar_, full_hue);
522 this->update_hue_bar_(SLIDER_BRIGHTNESS, &this->brightness_bar_, full_hue);
523 this->update_text_();
524 this->update_values_();
525 this->update_knobs_(full_hue);
526#ifdef USE_LVGL_COLOR_PICKER_WHEEL
527 this->update_wheel_handle_();
528#endif
529 }
530
531 // Sets the colour from the hue, saturation and brightness, and moves the RGB sliders to match.
532 void apply_hsv_() {
533 lv_color32_t c32 =
534 lv_color_to_32(lv_color_hsv_to_rgb(this->hue_, this->saturation_, this->brightness_), LV_OPA_COVER);
535 this->state_ = Color(c32.red, c32.green, c32.blue);
536 this->set_slider_value_(SLIDER_RED, c32.red);
537 this->set_slider_value_(SLIDER_GREEN, c32.green);
538 this->set_slider_value_(SLIDER_BLUE, c32.blue);
539 this->refresh_();
540 }
541
542 void update_hsv_() {
543 this->hue_ = static_cast<uint16_t>(this->slider_value_(SLIDER_HUE, this->hue_));
544 this->saturation_ = static_cast<uint8_t>(this->slider_value_(SLIDER_SATURATION, this->saturation_));
545 this->brightness_ = static_cast<uint8_t>(this->slider_value_(SLIDER_BRIGHTNESS, this->brightness_));
546 this->apply_hsv_();
547 }
548
549 void update_rgb_() {
550 this->state_ = Color(static_cast<uint8_t>(this->slider_value_(SLIDER_RED, this->state_.r)),
551 static_cast<uint8_t>(this->slider_value_(SLIDER_GREEN, this->state_.g)),
552 static_cast<uint8_t>(this->slider_value_(SLIDER_BLUE, this->state_.b)));
553 auto hsv = lv_color_rgb_to_hsv(this->state_.r, this->state_.g, this->state_.b);
554 this->hue_ = hsv.h;
555 this->saturation_ = hsv.s;
556 this->brightness_ = hsv.v;
557 // A grey, white or black colour leaves a picker stuck there if nothing it has can move the
558 // saturation, or the brightness, away from zero.
559 bool stuck = false;
560 if (this->saturation_ == 0 && !this->can_change_saturation_()) {
561 this->saturation_ = 100;
562 stuck = true;
563 }
564 if (this->brightness_ == 0 && !this->can_change_brightness_()) {
565 this->brightness_ = 100;
566 stuck = true;
567 }
568 this->set_slider_value_(SLIDER_HUE, this->hue_);
571 if (stuck) {
572 this->apply_hsv_();
573 } else {
574 this->refresh_();
575 }
576 }
577
579 this->set_slider_value_(SLIDER_RED, this->state_.r);
582 this->update_rgb_();
583 lv_obj_invalidate(this->obj);
584 }
585
586 // Shared by every colour picker; set up once by init_shared().
587 inline static bool shared_ready{false};
588 inline static lv_grad_dsc_t hue_bar{};
589 inline static lv_grad_dsc_t red_bar{};
590 inline static lv_grad_dsc_t green_bar{};
591 inline static lv_grad_dsc_t blue_bar{};
592#ifdef USE_LVGL_COLOR_PICKER_WHEEL
593 inline static lv_grad_dsc_t wheel_hue_grad{};
594 inline static lv_grad_dsc_t wheel_white_grad{};
595#endif
596 inline static lv_style_t plain_box{};
597 inline static lv_style_t slider_vert{};
598 inline static lv_style_t slider_vert_knob{};
599 inline static lv_style_t slider_horz{};
600 inline static lv_style_t slider_horz_knob{};
601
602 // Per widget, since their far end follows the chosen hue.
603 lv_grad_dsc_t saturation_bar_{};
604 lv_grad_dsc_t brightness_bar_{};
605 Color state_{0x80, 0x80, 0x80};
606 std::array<lv_obj_t *, SLIDER_COUNT> sliders_{};
607 std::array<lv_obj_t *, SLIDER_COUNT> values_{};
608 lv_obj_t *color_indicator_{};
609 lv_obj_t *color_text_{};
610 // Kept alongside state_ so a slider left out of the widget still has a value.
611 uint16_t hue_{0};
612 uint8_t saturation_{0};
613 uint8_t brightness_{50};
614 const uint8_t sliders_mask_;
615 const bool use_wheel_;
616 bool tint_knobs_{true};
617#ifdef USE_LVGL_COLOR_PICKER_WHEEL
618 // Null unless use_wheel_ is set.
619 lv_obj_t *wheel_{};
620 lv_obj_t *wheel_handle_{};
621#endif
622};
623
624} // namespace esphome::lvgl
625
626#endif // USE_LVGL_COLOR_PICKER
LvColorPickerType(uint8_t sliders, bool use_wheel)
void update_hue_bar_(SliderIndex index, lv_grad_dsc_t *grad, lv_color_t color)
static constexpr int KNOB_TRIM
static void bubble_events(lv_obj_t *obj)
static constexpr int32_t WHEEL_WIDTH_PCT
bool has_(SliderIndex index) const
std::array< lv_obj_t *, SLIDER_COUNT > sliders_
int32_t slider_value_(SliderIndex index, int32_t fallback) const
lv_obj_t * create_wheel_(lv_obj_t *parent)
static constexpr int32_t WHEEL_HANDLE_SIZE
lv_obj_t * create_slider_(lv_obj_t *parent, SliderIndex index, const char *name, bool vertical, const lv_grad_dsc_t *grad, int32_t max)
static lv_obj_t * create_disc(lv_obj_t *parent, const lv_grad_dsc_t *grad)
static lv_grad_dsc_t wheel_hue_grad
static void hsv_changed_cb(lv_event_t *event)
static constexpr int SELF_SIZE_FONT_MULTIPLE
std::array< lv_obj_t *, SLIDER_COUNT > values_
lv_obj_t * get_slider(size_t index) const
static lv_obj_t * create_box(lv_obj_t *parent)
static constexpr int32_t CONTENT_WIDTH_PCT
static void knob_clearance_cb(lv_event_t *event)
double wheel_radius_(lv_area_t *area) const
static lv_obj_t * create_flex(lv_obj_t *parent, lv_flex_flow_t flow, lv_flex_align_t main_place, lv_flex_align_t track_place=LV_FLEX_ALIGN_START)
static constexpr int32_t CHANNEL_WIDTH_PCT
void tint_knob_(SliderIndex index, lv_color_t color) const
static void wheel_touch_cb(lv_event_t *event)
static constexpr int32_t HUE_WIDTH_PCT
bool enabled_(SliderIndex index) const
void set_color(lv_color_t color)
void add_value_label_(SliderIndex index, lv_color_t color)
static void update_knob_clearance(lv_obj_t *container)
void update_knobs_(lv_color_t full_hue) const
static lv_grad_dsc_t wheel_white_grad
void set_tint_knobs(bool tint_knobs)
static lv_grad_dsc_t blue_bar
static void size_changed_cb(lv_event_t *event)
static lv_grad_dsc_t green_bar
static LvColorPickerType * from_event(lv_event_t *event)
void set_slider_value_(SliderIndex index, int32_t value)
static void init_bar(lv_grad_dsc_t *grad, lv_grad_dir_t dir, lv_color_t from, lv_color_t to)
void set_obj(lv_obj_t *outer) override
static void wheel_size_cb(lv_event_t *event)
void create_row_slider_(lv_obj_t *parent, SliderIndex index, const char *name, const lv_grad_dsc_t *grad, lv_color_t text_color)
static void rgb_changed_cb(lv_event_t *event)
static void init_slider_styles(lv_style_t *bar, lv_style_t *knob, bool vertical)
static void self_size_cb(lv_event_t *event)
static constexpr int KNOB_OVERHANG
virtual void set_obj(lv_obj_t *lv_obj)
uint32_t value
Definition hal.cpp:59
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:529
static void uint32_t
uint8_t g
Definition color.h:42
uint8_t b
Definition color.h:46
uint8_t r
Definition color.h:38