From 0662c5e0fb3e760fe488b80ab4cc312e6bd1011c Mon Sep 17 00:00:00 2001 From: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> Date: Fri, 26 Apr 2024 07:00:01 +1000 Subject: [PATCH] Fix for #6614- use background_color, improve anti-aliasing (#6618) --- esphome/components/font/font.cpp | 17 ++++++++++------- .../graphical_display_menu.cpp | 15 ++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/esphome/components/font/font.cpp b/esphome/components/font/font.cpp index 5a18429789..3b62b8ca66 100644 --- a/esphome/components/font/font.cpp +++ b/esphome/components/font/font.cpp @@ -129,7 +129,13 @@ void Font::print(int x_start, int y_start, display::Display *display, Color colo uint8_t bitmask = 0; uint8_t pixel_data = 0; - float bpp_max = (1 << this->bpp_) - 1; + uint8_t bpp_max = (1 << this->bpp_) - 1; + auto diff_r = (float) color.r - (float) background.r; + auto diff_g = (float) color.g - (float) background.g; + auto diff_b = (float) color.b - (float) background.b; + auto b_r = (float) background.r; + auto b_g = (float) background.g; + auto b_b = (float) background.g; for (int glyph_y = y_start + scan_y1; glyph_y != max_y; glyph_y++) { for (int glyph_x = x_at + scan_x1; glyph_x != max_x; glyph_x++) { uint8_t pixel = 0; @@ -146,12 +152,9 @@ void Font::print(int x_start, int y_start, display::Display *display, Color colo if (pixel == bpp_max) { display->draw_pixel_at(glyph_x, glyph_y, color); } else if (pixel != 0) { - float on = (float) pixel / bpp_max; - float off = 1.0 - on; - Color blended; - blended.r = color.r * on + background.r * off; - blended.g = color.r * on + background.g * off; - blended.b = color.r * on + background.b * off; + auto on = (float) pixel / (float) bpp_max; + auto blended = + Color((uint8_t) (diff_r * on + b_r), (uint8_t) (diff_g * on + b_g), (uint8_t) (diff_b * on + b_b)); display->draw_pixel_at(glyph_x, glyph_y, blended); } } diff --git a/esphome/components/graphical_display_menu/graphical_display_menu.cpp b/esphome/components/graphical_display_menu/graphical_display_menu.cpp index fcbad41388..4a4e519009 100644 --- a/esphome/components/graphical_display_menu/graphical_display_menu.cpp +++ b/esphome/components/graphical_display_menu/graphical_display_menu.cpp @@ -104,7 +104,8 @@ void GraphicalDisplayMenu::draw(display::Display *display, const display::Rect * } void GraphicalDisplayMenu::draw_menu_internal_(display::Display *display, const display::Rect *bounds) { - int total_height = 0; + int16_t total_height = 0; + int16_t max_width = 0; int y_padding = 2; bool scroll_menu_items = false; std::vector menu_dimensions; @@ -118,6 +119,7 @@ void GraphicalDisplayMenu::draw_menu_internal_(display::Display *display, const menu_dimensions.push_back(item_dimensions); total_height += item_dimensions.h + (i == 0 ? 0 : y_padding); + max_width = std::max(max_width, item_dimensions.w); if (total_height <= bounds->h) { number_items_fit_to_screen++; @@ -166,7 +168,8 @@ void GraphicalDisplayMenu::draw_menu_internal_(display::Display *display, const // Render the items into the view port display->start_clipping(*bounds); - int y_offset = bounds->y; + display->filled_rectangle(bounds->x, bounds->y, max_width, total_height, this->background_color_); + auto y_offset = bounds->y; for (size_t i = first_item_index; i <= last_item_index; i++) { const auto *item = this->displayed_item_->get_item(i); const bool selected = i == this->cursor_index_; @@ -176,7 +179,7 @@ void GraphicalDisplayMenu::draw_menu_internal_(display::Display *display, const dimensions.x = bounds->x; this->draw_item(display, item, &dimensions, selected); - y_offset = dimensions.y + dimensions.h + y_padding; + y_offset += dimensions.h + y_padding; } display->end_clipping(); @@ -219,9 +222,7 @@ inline void GraphicalDisplayMenu::draw_item(display::Display *display, const dis // int background_width = std::max(bounds->width, available_width); int background_width = bounds->w; - if (selected) { - display->filled_rectangle(bounds->x, bounds->y, background_width, bounds->h, background_color); - } + display->filled_rectangle(bounds->x, bounds->y, background_width, bounds->h, background_color); std::string label = item->get_text(); if (item->has_value()) { @@ -230,7 +231,7 @@ inline void GraphicalDisplayMenu::draw_item(display::Display *display, const dis } display->print(bounds->x, bounds->y, this->font_, foreground_color, display::TextAlign::TOP_LEFT, label.c_str(), - ~foreground_color); + background_color); } void GraphicalDisplayMenu::draw_item(const display_menu_base::MenuItem *item, const uint8_t row, const bool selected) {