From 4efd8bfde1dab14a8344910796bd1f7f89f40165 Mon Sep 17 00:00:00 2001 From: Noisyfox Date: Thu, 15 Oct 2026 22:49:22 +0800 Subject: [PATCH] Show checkerboard under translucent AMS custom colors --- docs/HLSD/color-picker.md | 6 ++- src/slic3r/GUI/AMSMaterialsSetting.cpp | 4 +- src/slic3r/GUI/FilamentBitmapUtils.cpp | 19 ++++++- src/slic3r/GUI/FilamentBitmapUtils.hpp | 4 +- .../test_filament_bitmap_utils.cpp | 54 ++++++++++++++++++- 5 files changed, 79 insertions(+), 8 deletions(-) diff --git a/docs/HLSD/color-picker.md b/docs/HLSD/color-picker.md index b0d8bcbc8b..e1ef517248 100644 --- a/docs/HLSD/color-picker.md +++ b/docs/HLSD/color-picker.md @@ -81,8 +81,10 @@ the existing packed RGBA event carries the representative first color. Device permission/status checks remain at the first-level entry. Saving uses `RRGGBBAA` for `tray_color` and, on capable firmware, `cols`, with `ctype=0` for gradients and `ctype=2` for single colors. The edited tray updates locally while device status -is pending. AMS swatches interpolate every gradient stop and composite its alpha -over their existing background. +is pending. AMS custom swatches interpolate every gradient stop. Single colors, +gradients and segmented swatches with any nonopaque component composite over a +rounded checkerboard, including the shared AMS preview renderer. Opaque swatches +retain their existing background. Texture import's color-mapping Add Material action opens a solid, opaque editor anchored to the mapping row. The existing filament limit is checked before opening. diff --git a/src/slic3r/GUI/AMSMaterialsSetting.cpp b/src/slic3r/GUI/AMSMaterialsSetting.cpp index 04ac5e19b4..42808c9ac6 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.cpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.cpp @@ -92,11 +92,11 @@ public: private: void doRender(wxDC& dc) override { - if (m_colors.size() <= 1) { + if (m_colors.empty() || (m_colors.size() == 1 && m_colors.front().Alpha() == 255)) { StaticBox::doRender(dc); return; } - // Transparent stops reveal the popup background, never a previous paint or the first color. + // Clear the corners and previous paint before drawing colors over their transparency pattern. dc.SetBackground(wxBrush(GetBackgroundColour())); dc.Clear(); const wxRect rect = GetClientRect(); diff --git a/src/slic3r/GUI/FilamentBitmapUtils.cpp b/src/slic3r/GUI/FilamentBitmapUtils.cpp index 3770a854b8..6126da5998 100644 --- a/src/slic3r/GUI/FilamentBitmapUtils.cpp +++ b/src/slic3r/GUI/FilamentBitmapUtils.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -149,6 +150,22 @@ bool draw_rounded_filament_swatch(wxDC& dc, const wxRect& rect, const std::vecto } gc->PushState(); gc->SetPen(*wxTRANSPARENT_PEN); + if (std::any_of(colors.begin(), colors.end(), [](const wxColour& color) { return color.Alpha() < 255; })) { + // A bitmap brush clips the checkerboard to the same rounded outline as the colors. + const int square_size = std::max(1, rect.height / 5); + wxBitmap checkerboard(2 * square_size, 2 * square_size, 24); + { + wxMemoryDC checker_dc(checkerboard); + checker_dc.SetBackground(*wxWHITE_BRUSH); + checker_dc.Clear(); + checker_dc.SetPen(*wxTRANSPARENT_PEN); + checker_dc.SetBrush(wxBrush(wxColour(217, 217, 217))); + checker_dc.DrawRectangle(square_size, 0, square_size, square_size); + checker_dc.DrawRectangle(0, square_size, square_size, square_size); + } + gc->SetBrush(wxBrush(checkerboard)); + gc->DrawRoundedRectangle(rect.x, rect.y, rect.width, rect.height, radius); + } gc->SetBrush(gc->CreateLinearGradientBrush(rect.x, rect.y, rect.x + rect.width, rect.y, stops)); gc->DrawRoundedRectangle(rect.x, rect.y, rect.width, rect.height, radius); gc->PopState(); @@ -926,4 +943,4 @@ void draw_mixed_triangle_labels(wxDC& dc, const wxSize& size, const std::array& colors); -// Draw ordered RGBA colors as a rounded gradient or segmented swatch on the existing background. +// Draw ordered RGBA colors as a rounded gradient or segmented swatch, with a checkerboard beneath alpha. // Returns false if a graphics context is unavailable, leaving fallback drawing to the caller. bool draw_rounded_filament_swatch(wxDC& dc, const wxRect& rect, const std::vector& colors, bool gradient, double radius); @@ -167,4 +167,4 @@ void draw_mixed_triangle_labels(wxDC& dc, const wxSize& size, const std::array{3, background}, {62, wxColour(20, 148, 20)}, {121, wxColour(0, 0, 255)}}) { + for (const auto& sample : {std::pair{3, wxColour(255, 255, 255)}, {62, wxColour(127, 255, 127)}, {121, wxColour(0, 0, 255)}}) { REQUIRE(std::abs(int(image.GetRed(sample.first, 14)) - sample.second.Red()) <= 8); REQUIRE(std::abs(int(image.GetGreen(sample.first, 14)) - sample.second.Green()) <= 8); REQUIRE(std::abs(int(image.GetBlue(sample.first, 14)) - sample.second.Blue()) <= 8); @@ -330,6 +330,58 @@ TEST_CASE("Rounded custom swatches retain visible stops after a transparent firs require_same_rgb(wxColour(image.GetRed(2, 2), image.GetGreen(2, 2), image.GetBlue(2, 2)), background); } +TEST_CASE("Rounded single colors composite alpha over checkerboard cells without painting the corners", "[FilamentBitmapUtils][AMSMaterialsSetting]") +{ + const auto alpha = GENERATE(0, 128, 254, 255); + const int scale = GENERATE(1, 2); + const wxColour background(40, 40, 40); + const wxRect rect(0, 0, 120 * scale, 25 * scale); + wxBitmap bitmap(rect.width, rect.height, 24); + { + wxMemoryDC dc(bitmap); + dc.SetBackground(wxBrush(background)); + dc.Clear(); + REQUIRE(Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, {wxColour(0, 0, 255, alpha)}, false, 7 * scale)); + } + const wxImage image = bitmap.ConvertToImage(); + REQUIRE(image.IsOk()); + const int y = 12 * scale; + const int first = image.GetRed(22 * scale, y); + const int second = image.GetRed(27 * scale, y); + const int dark = 217 * (255 - alpha) / 255; + const int light = 255 - alpha; + REQUIRE(std::abs(std::min(first, second) - dark) <= 1); + REQUIRE(std::abs(std::max(first, second) - light) <= 1); + REQUIRE(image.GetGreen(22 * scale, y) == first); + REQUIRE(image.GetGreen(27 * scale, y) == second); + REQUIRE(std::abs(int(image.GetBlue(22 * scale, y)) - (first + alpha)) <= 1); + REQUIRE(std::abs(int(image.GetBlue(27 * scale, y)) - (second + alpha)) <= 1); + require_same_rgb(wxColour(image.GetRed(0, 0), image.GetGreen(0, 0), image.GetBlue(0, 0)), background); +} + +TEST_CASE("Nonopaque interior colors reveal checkerboard beneath opaque endpoints", "[FilamentBitmapUtils][AMSMaterialsSetting]") +{ + const auto alpha = GENERATE(0, 128); + const bool gradient = GENERATE(false, true); + const std::vector colors{wxColour(255, 0, 0), wxColour(0, 0, 255, alpha), wxColour(0, 255, 0)}; + wxBitmap bitmap(120, 25, 24); + { + wxMemoryDC dc(bitmap); + dc.SetBackground(*wxBLACK_BRUSH); + dc.Clear(); + REQUIRE(Slic3r::GUI::draw_rounded_filament_swatch(dc, wxRect(0, 0, 120, 25), colors, gradient, 7)); + } + const wxImage image = bitmap.ConvertToImage(); + REQUIRE(image.IsOk()); + const int first = image.GetRed(60, 12); + const int second = image.GetRed(60, 17); + REQUIRE(std::abs(std::min(first, second) - 217 * (255 - alpha) / 255) <= 8); + REQUIRE(std::abs(std::max(first, second) - (255 - alpha)) <= 8); + REQUIRE(std::abs(first - second) >= (alpha == 0 ? 30 : 12)); + REQUIRE(image.GetRed(1, 12) >= 247); + REQUIRE(image.GetGreen(119, 12) >= 247); +} + TEST_CASE("Rounded custom swatches keep segmented colors distinct from gradients", "[FilamentBitmapUtils][AMSMaterialsSetting]") { const std::vector colors{wxColour(255, 0, 0), wxColour(0, 255, 0), wxColour(0, 0, 255)};