From 206552674bf0a0a19faed7ee759cc87e435151e4 Mon Sep 17 00:00:00 2001 From: Noisyfox Date: Thu, 15 Oct 2026 23:25:23 +0800 Subject: [PATCH] Flatten filament preview bitmaps to opaque RGB for native labels --- docs/HLSD/color-picker.md | 5 ++ src/slic3r/GUI/AMSMaterialsSetting.cpp | 2 +- src/slic3r/GUI/FilamentBitmapUtils.cpp | 8 +-- src/slic3r/GUI/FilamentBitmapUtils.hpp | 3 +- src/slic3r/GUI/Widgets/AMSItem.cpp | 4 +- src/slic3r/GUI/wxExtensions.cpp | 5 +- .../test_filament_bitmap_utils.cpp | 53 +++++++++++++++++++ 7 files changed, 69 insertions(+), 11 deletions(-) diff --git a/docs/HLSD/color-picker.md b/docs/HLSD/color-picker.md index 359694378f..08c23a8c27 100644 --- a/docs/HLSD/color-picker.md +++ b/docs/HLSD/color-picker.md @@ -50,6 +50,11 @@ single colors, gradients and multicolor layouts composite over the same checkerb as AMS custom colors. Two- and three-color stripes and four-color quadrants retain their layouts. Icon caches distinguish gradient and segmented modes and include every RGBA component; label contrast uses the composited color for single colors. +All composited filament preview bitmaps use opaque RGB storage, including single, +gradient, segmented and vertical-ramp swatches. The color's alpha affects its +appearance over the checkerboard; the finished preview carries no alpha channel. +On Windows this also prevents GDI label drawing from leaving alpha bytes that +native menu conversion treats as transparent text. Opaque colors use `#RRGGBB`; nonopaque colors use `#RRGGBBAA`, including fully transparent alpha `00`. Initial selections, alpha-only edits and cancellation retain stored alpha. Ordered diff --git a/src/slic3r/GUI/AMSMaterialsSetting.cpp b/src/slic3r/GUI/AMSMaterialsSetting.cpp index d41bc37fcf..1791a8d3e1 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.cpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.cpp @@ -1873,7 +1873,7 @@ void ColorPicker::render(wxDC& dc) #ifdef __WXMSW__ wxSize size = GetSize(); wxMemoryDC memdc; - wxBitmap bmp(size.x, size.y); + wxBitmap bmp(size.x, size.y, 24); memdc.SelectObject(bmp); memdc.Blit({ 0, 0 }, size, &dc, { 0, 0 }); diff --git a/src/slic3r/GUI/FilamentBitmapUtils.cpp b/src/slic3r/GUI/FilamentBitmapUtils.cpp index 4551133a6d..2bb5b23319 100644 --- a/src/slic3r/GUI/FilamentBitmapUtils.cpp +++ b/src/slic3r/GUI/FilamentBitmapUtils.cpp @@ -294,10 +294,10 @@ struct BitmapDC { wxMemoryDC dc; std::unique_ptr graphics; - BitmapDC(const wxSize& size, bool alpha) : bitmap(size){ -#ifdef __WXOSX__ - bitmap.UseAlpha(); -#endif + // All previews are opaque after checkerboard compositing. RGB storage also + // prevents Windows GDI labels from leaving invalid alpha bytes for menus. + BitmapDC(const wxSize& size, bool alpha) : bitmap(size, 24) + { dc.SelectObject(bitmap); // Don't set white background - let the color patterns fill the entire area dc.SetPen(*wxTRANSPARENT_PEN); diff --git a/src/slic3r/GUI/FilamentBitmapUtils.hpp b/src/slic3r/GUI/FilamentBitmapUtils.hpp index 966459bde9..1986cb6606 100644 --- a/src/slic3r/GUI/FilamentBitmapUtils.hpp +++ b/src/slic3r/GUI/FilamentBitmapUtils.hpp @@ -49,6 +49,7 @@ enum class FilamentRenderMode { // Create a colour swatch bitmap. The render mode is chosen automatically from the // number of colours unless force_gradient is true. +// Composites RGBA over a checkerboard and returns an opaque RGB bitmap. wxBitmap create_filament_bitmap(const std::vector& colors, const wxSize& size, bool force_gradient = false); @@ -81,7 +82,7 @@ Slic3r::GradientCurve mixed_gradient_curve(const Slic3r::DynamicPrintConfig& cfg // Fill rect with a ramp, ramp.front() along the bottom edge. void fill_gradient_ramp_rect(wxDC& dc, const wxRect& rect, const std::vector& ramp); -// Swatch bitmap for a gradient mixed filament, drawn bottom to top from the ramp. +// Opaque RGB swatch for a gradient mixed filament, drawn bottom to top over a checkerboard. wxBitmap create_gradient_ramp_bitmap(const std::vector& ramp, const wxSize& size); // Recompute blended representative colors for mixed (virtual) filament slots. diff --git a/src/slic3r/GUI/Widgets/AMSItem.cpp b/src/slic3r/GUI/Widgets/AMSItem.cpp index 4d18396f07..ba8526c9d9 100644 --- a/src/slic3r/GUI/Widgets/AMSItem.cpp +++ b/src/slic3r/GUI/Widgets/AMSItem.cpp @@ -1082,7 +1082,7 @@ void AMSLib::render(wxDC &dc) #ifdef __WXMSW__ wxSize size = GetSize(); wxMemoryDC memdc; - wxBitmap bmp(size.x, size.y); + wxBitmap bmp(size.x, size.y, 24); memdc.SelectObject(bmp); memdc.Blit({0, 0}, size, &dc, {0, 0}); @@ -2731,7 +2731,7 @@ void AMSPreview::render(wxDC &dc) #ifdef __WXMSW__ wxSize size = GetSize(); wxMemoryDC memdc; - wxBitmap bmp(size.x, size.y); + wxBitmap bmp(size.x, size.y, 24); memdc.SelectObject(bmp); memdc.Blit({0, 0}, size, &dc, {0, 0}); diff --git a/src/slic3r/GUI/wxExtensions.cpp b/src/slic3r/GUI/wxExtensions.cpp index 446c98b53a..ec7ff5ae70 100644 --- a/src/slic3r/GUI/wxExtensions.cpp +++ b/src/slic3r/GUI/wxExtensions.cpp @@ -712,7 +712,7 @@ wxBitmap *get_extruder_color_icon(std::vector colors, bool is_gradi int text_y = (icon_height - size.y) / 2; // Draw very thin border with lighter color and fewer directions - dc.SetTextForeground(wxColor("262E30")); // Semi-transparent dark gray + dc.SetTextForeground(wxColour("#262E30")); // Dark outline dc.DrawText(label, text_x - 1, text_y); // Left dc.DrawText(label, text_x + 1, text_y); // Right dc.DrawText(label, text_x, text_y - 1); // Up @@ -753,10 +753,9 @@ wxBitmap *get_extruder_color_icon(std::string color, std::string label, int icon wxColor clr(color); const bool alpha = clr.Alpha() < 255; const wxBitmap base = alpha ? Slic3r::GUI::create_filament_bitmap({clr}, wxSize(icon_width, icon_height)) - : wxBitmap(icon_width, icon_height); + : wxBitmap(icon_width, icon_height, 24); bitmap = bmp_cache.insert(bitmap_key, base); #ifdef __WXOSX__ - bitmap->UseAlpha(); wxMemoryDC dc(*bitmap); #elif defined(__WXMSW__) wxClientDC cdc(win); diff --git a/tests/slic3rutils/test_filament_bitmap_utils.cpp b/tests/slic3rutils/test_filament_bitmap_utils.cpp index 7c18368655..58dc563085 100644 --- a/tests/slic3rutils/test_filament_bitmap_utils.cpp +++ b/tests/slic3rutils/test_filament_bitmap_utils.cpp @@ -30,7 +30,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -583,8 +585,10 @@ TEST_CASE("Filament single color icons composite every RGBA channel over both ch REQUIRE(int(color.Alpha()) == alpha); const wxBitmap bitmap = Slic3r::GUI::create_filament_bitmap({color}, wxSize(width * scale, 20 * scale)); REQUIRE(bitmap.IsOk()); + REQUIRE_FALSE(bitmap.HasAlpha()); const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); + REQUIRE_FALSE(image.HasAlpha()); const int y = 10 * scale; for (const auto& channels : {std::vector{32, image.GetRed(6 * scale, y), image.GetRed(10 * scale, y)}, std::vector{64, image.GetGreen(6 * scale, y), image.GetGreen(10 * scale, y)}, @@ -604,8 +608,10 @@ TEST_CASE("Filament gradient icons reveal interior alpha while retaining opaque std::reverse(colors.begin(), colors.end()); const wxBitmap bitmap = Slic3r::GUI::create_filament_bitmap(colors, wxSize(121, 20), true); REQUIRE(bitmap.IsOk()); + REQUIRE_FALSE(bitmap.HasAlpha()); const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); + REQUIRE_FALSE(image.HasAlpha()); const int first = image.GetRed(60, 6), second = image.GetRed(60, 10); REQUIRE(std::abs(std::min(first, second) - 217 * (255 - alpha) / 255) <= 1); REQUIRE(std::abs(std::max(first, second) - (255 - alpha)) <= 1); @@ -623,8 +629,10 @@ TEST_CASE("Filament segmented icons preserve stripes and quadrants over a contin const std::vector colors(all.begin(), all.begin() + count); const wxBitmap bitmap = Slic3r::GUI::create_filament_bitmap(colors, wxSize(44 * scale, 20 * scale), false); REQUIRE(bitmap.IsOk()); + REQUIRE_FALSE(bitmap.HasAlpha()); const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); + REQUIRE_FALSE(image.HasAlpha()); for (int i = 0; i < count; ++i) { const int x = (count == 4 ? 22 * (i % 2) + 5 : (44 / count) * i + 5) * scale; const int y = (count == 4 ? 10 * (i / 2) + 5 : 10) * scale; @@ -645,8 +653,10 @@ TEST_CASE("Vertical filament ramps composite alpha over the same checkerboard as const std::vector ramp{wxColour(255, 0, 0), wxColour(0, 0, 255, 128), wxColour(0, 255, 0, 0)}; const wxBitmap bitmap = Slic3r::GUI::create_gradient_ramp_bitmap(ramp, wxSize(44, 21)); REQUIRE(bitmap.IsOk()); + REQUIRE_FALSE(bitmap.HasAlpha()); const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); + REQUIRE_FALSE(image.HasAlpha()); REQUIRE(std::min(image.GetRed(6, 0), image.GetRed(10, 0)) == 217); REQUIRE(std::max(image.GetRed(6, 0), image.GetRed(10, 0)) == 255); REQUIRE(std::abs(int(std::min(image.GetRed(6, 10), image.GetRed(10, 10))) - 108) <= 1); @@ -654,4 +664,47 @@ TEST_CASE("Vertical filament ramps composite alpha over the same checkerboard as require_same_rgb(wxColour(image.GetRed(6, 20), image.GetGreen(6, 20), image.GetBlue(6, 20)), ramp.front()); } +TEST_CASE("Windows menu gradient labels retain opaque pixels after native text drawing", "[FilamentBitmapUtils][Regression]") +{ + const int alpha = GENERATE(0, 128, 255); + const double scale = GENERATE(1.0, 1.5, 2.0); + const wxString label = GENERATE("3", "10"); + const int size = static_cast(20 * scale); + wxBitmap bitmap = Slic3r::GUI::create_filament_bitmap({wxColour(0, 0, 255, alpha), wxColour(255, 0, 0, alpha)}, + wxSize(size, size), true); + REQUIRE(bitmap.IsOk()); + bitmap.SetScaleFactor(scale); + { + wxMemoryDC dc(bitmap); + dc.SetFont(wxFont(wxFontInfo(wxSize(0, static_cast(14 * scale))).Family(wxFONTFAMILY_SWISS))); + dc.SetBackgroundMode(wxTRANSPARENT); + const wxSize text = dc.GetTextExtent(label); + const int x = (size - text.x) / 2, y = (size - text.y) / 2; + dc.SetTextForeground(wxColour("#262E30")); + dc.DrawText(label, x - 1, y); + dc.DrawText(label, x + 1, y); + dc.DrawText(label, x, y - 1); + dc.DrawText(label, x, y + 1); + dc.SetTextForeground(*wxWHITE); + dc.DrawText(label, x, y); + } + wxMenuItem item(nullptr, wxID_ANY, "Filament"); + item.SetBitmap(bitmap); + const wxImage image = item.GetBitmap().ConvertToImage(); + REQUIRE(image.IsOk()); + REQUIRE(image.HasAlpha()); + std::size_t nonopaque = 0, foreground = 0; + for (int y = 0; y < image.GetHeight(); ++y) { + for (int x = 0; x < image.GetWidth(); ++x) { + if (image.GetAlpha(x, y) != 255) + ++nonopaque; + if (image.GetRed(x, y) > 230 && image.GetGreen(x, y) > 230 && image.GetBlue(x, y) > 230) + ++foreground; + } + } + CAPTURE(alpha, scale, label, bitmap.GetDepth(), bitmap.HasAlpha()); + REQUIRE(nonopaque == 0); + REQUIRE(foreground > 0); +} + #endif // __WXMSW__