From 99ca7732c7bddcee8b7a2121fcc2293dcb29e241 Mon Sep 17 00:00:00 2001 From: Noisyfox Date: Thu, 15 Oct 2026 23:35:35 +0800 Subject: [PATCH] Remove filament swatch rendering fallbacks --- docs/HLSD/color-picker.md | 2 + src/slic3r/GUI/AMSMaterialsSetting.cpp | 19 ++--- src/slic3r/GUI/FilamentBitmapUtils.cpp | 21 +++--- src/slic3r/GUI/FilamentBitmapUtils.hpp | 6 +- src/slic3r/GUI/Widgets/AMSItem.cpp | 18 ++--- .../test_filament_bitmap_utils.cpp | 69 +++++++++++++++++-- 6 files changed, 86 insertions(+), 49 deletions(-) diff --git a/docs/HLSD/color-picker.md b/docs/HLSD/color-picker.md index 08c23a8c27..5d51b1a1fa 100644 --- a/docs/HLSD/color-picker.md +++ b/docs/HLSD/color-picker.md @@ -99,6 +99,8 @@ settings and palette previews and the device panel's gradient cards also use thi renderer, including AMS Lite, external spools, remaining-material regions and four-slot thumbnails. Each keeps its existing shape and material bounds. Opaque swatches retain their existing background. +Rounded gradient and segmented swatches use the same graphics-context renderer. +Alpha rendering requires a graphics-capable DC, and callers invoke this renderer directly. 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 1791a8d3e1..da3de55a0b 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.cpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.cpp @@ -101,10 +101,7 @@ private: dc.Clear(); const wxRect rect = GetClientRect(); const double corner_radius = std::max(0.0, radius - border_width); - if (!draw_rounded_filament_swatch(dc, rect, m_colors, m_ctype == 0, corner_radius)) { - StaticBox::doRender(dc); - return; - } + draw_rounded_filament_swatch(dc, rect, m_colors, m_ctype == 0, corner_radius); dc.SetPen(wxPen(border_color.colorForStates(state_handler.states()), border_width, border_style)); dc.SetBrush(*wxTRANSPARENT_BRUSH); dc.DrawRoundedRectangle(rect, corner_radius); @@ -1886,11 +1883,9 @@ void ColorPicker::render(wxDC& dc) dc.DrawBitmap(bmp, 0, 0); #else if (m_show_full && m_cols.size() > 1) { - if (auto* context = wxGraphicsContext::CreateFromUnknownDC(dc)) { - wxGCDC graphics(context); - doRender(graphics); - return; - } + wxGCDC graphics(wxGraphicsContext::CreateFromUnknownDC(dc)); + doRender(graphics); + return; } doRender(dc); #endif @@ -1906,11 +1901,7 @@ void ColorPicker::doRender(wxDC& dc) if (m_show_full && m_cols.size() > 1) { if (ctype == 0) { const wxRect rect(size.x / 2 - radius, size.y / 2 - radius, 2 * radius, 2 * radius); - if (!draw_rounded_filament_swatch(dc, rect, m_cols, true, radius)) { - dc.SetPen(*wxTRANSPARENT_PEN); - dc.SetBrush(wxBrush(m_colour)); - dc.DrawCircle(size.x / 2, size.y / 2, radius); - } + draw_rounded_filament_swatch(dc, rect, m_cols, true, radius); } else { float ev_angle = 360.0 / m_cols.size(); diff --git a/src/slic3r/GUI/FilamentBitmapUtils.cpp b/src/slic3r/GUI/FilamentBitmapUtils.cpp index 2bb5b23319..0e6370e50c 100644 --- a/src/slic3r/GUI/FilamentBitmapUtils.cpp +++ b/src/slic3r/GUI/FilamentBitmapUtils.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -112,11 +113,10 @@ void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const std::vector& colors, +void draw_rounded_filament_swatch(wxDC& dc, const wxRect& rect, const std::vector& colors, bool gradient, double radius) { if (colors.empty() || rect.width <= 0 || rect.height <= 0) - return false; + return; std::unique_ptr owned_gc; wxGraphicsContext* gc = dc.GetGraphicsContext(); if (!gc) { owned_gc.reset(wxGraphicsContext::CreateFromUnknownDC(dc)); gc = owned_gc.get(); } - if (!gc) - return false; + wxCHECK_RET(gc, "Rounded filament swatches require a graphics context"); wxGraphicsGradientStops stops(colors.front(), colors.back()); const size_t count = colors.size(); @@ -178,7 +177,6 @@ bool draw_rounded_filament_swatch(wxDC& dc, const wxRect& rect, const std::vecto 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(); - return true; } static std::string to_hex(const wxColour& c) @@ -302,8 +300,7 @@ struct BitmapDC { // Don't set white background - let the color patterns fill the entire area dc.SetPen(*wxTRANSPARENT_PEN); if (alpha) { - if (auto* context = wxGraphicsContext::CreateFromUnknownDC(dc)) - graphics = std::make_unique(context); + graphics = std::make_unique(wxGraphicsContext::CreateFromUnknownDC(dc)); wxDC& paint = paint_dc(); paint.SetPen(*wxTRANSPARENT_PEN); paint.SetBrush(checkerboard_brush(std::max(1, size.GetHeight() / 5))); diff --git a/src/slic3r/GUI/FilamentBitmapUtils.hpp b/src/slic3r/GUI/FilamentBitmapUtils.hpp index 1986cb6606..9db9da73bc 100644 --- a/src/slic3r/GUI/FilamentBitmapUtils.hpp +++ b/src/slic3r/GUI/FilamentBitmapUtils.hpp @@ -34,9 +34,9 @@ void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const wxColour& from, // Ordered, evenly spaced RGBA stops, including when the rect is narrower than the stop count. void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const std::vector& colors); -// 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, +// Draw ordered RGBA colors through the graphics backend, with a checkerboard beneath alpha. +// The DC must support graphics-context rendering. +void draw_rounded_filament_swatch(wxDC& dc, const wxRect& rect, const std::vector& colors, bool gradient, double radius); enum class FilamentRenderMode { diff --git a/src/slic3r/GUI/Widgets/AMSItem.cpp b/src/slic3r/GUI/Widgets/AMSItem.cpp index ba8526c9d9..97217e48bd 100644 --- a/src/slic3r/GUI/Widgets/AMSItem.cpp +++ b/src/slic3r/GUI/Widgets/AMSItem.cpp @@ -1413,8 +1413,7 @@ void AMSLib::render_lite_lib(wxDC& dc) if (m_info.material_cols.size() > 1) { wxRect color_rect(FromDIP(10), FromDIP(10), libsize.x - FromDIP(18), libsize.y - FromDIP(18)); if (m_info.ctype == 0) { - if (!draw_rounded_filament_swatch(dc, color_rect, m_info.material_cols, true, 0)) - fill_gradient_rect_east(dc, color_rect, m_info.material_cols); + draw_rounded_filament_swatch(dc, color_rect, m_info.material_cols, true, 0); } else { int cols_size = m_info.material_cols.size(); @@ -1518,8 +1517,7 @@ void AMSLib::render_generic_lib(wxDC &dc) const bool multi_color = (m_info.ctype == 0 || m_info.ctype == 1) && m_info.material_cols.size() > 1; if (multi_color) { if (m_info.ctype == 0) { - if (!draw_rounded_filament_swatch(dc, color_rect, m_info.material_cols, true, 0)) - fill_gradient_rect_east(dc, color_rect, m_info.material_cols); + draw_rounded_filament_swatch(dc, color_rect, m_info.material_cols, true, 0); } else { int cols_size = static_cast(m_info.material_cols.size()); @@ -1624,8 +1622,7 @@ void AMSLib::render_generic_lib(wxDC &dc) //gradient if (m_info.ctype == 0) { const wxRect color_rect(FromDIP(2), height - curr_height, size.x - FromDIP(4), curr_height); - if (!draw_rounded_filament_swatch(dc, color_rect, m_info.material_cols, true, 0)) - fill_gradient_rect_east(dc, color_rect, m_info.material_cols); + draw_rounded_filament_swatch(dc, color_rect, m_info.material_cols, true, 0); } else { //multicolour @@ -2787,8 +2784,7 @@ void AMSPreview::doRender(wxDC &dc) if (iter->material_cols.size() > 1) { if (iter->ctype == 0) { const wxRect rect(left, (size.y - AMS_ITEM_CUBE_SIZE.y) / 2, AMS_ITEM_CUBE_SIZE.x, AMS_ITEM_CUBE_SIZE.y); - if (!draw_rounded_filament_swatch(dc, rect, iter->material_cols, true, 0)) - fill_gradient_rect_east(dc, rect, iter->material_cols); + draw_rounded_filament_swatch(dc, rect, iter->material_cols, true, 0); } else { int cols_size = iter->material_cols.size(); @@ -2849,11 +2845,7 @@ void AMSPreview::doRender(wxDC &dc) const wxSize swatch_size = m_ams_item_type == AMSModel::N3S_AMS ? AMS_ITEM_CUBE_SIZE : AMS_ITEM_CUBE_SIZE2; const wxRect rect((size.x - swatch_size.x) / 2, (size.y - swatch_size.y) / 2, swatch_size.x, swatch_size.y); const int radius = m_ams_item_type == AMSModel::N3S_AMS ? 0 : FromDIP(3); - if (!draw_rounded_filament_swatch(dc, rect, iter.material_cols, iter.ctype == 0, radius)) { - dc.SetPen(*wxTRANSPARENT_PEN); - dc.SetBrush(iter.material_colour); - dc.DrawRoundedRectangle(rect, radius); - } + draw_rounded_filament_swatch(dc, rect, iter.material_cols, iter.ctype == 0, radius); } else { dc.SetPen(wxPen(*wxTRANSPARENT_PEN)); diff --git a/tests/slic3rutils/test_filament_bitmap_utils.cpp b/tests/slic3rutils/test_filament_bitmap_utils.cpp index 58dc563085..0c0e182ef8 100644 --- a/tests/slic3rutils/test_filament_bitmap_utils.cpp +++ b/tests/slic3rutils/test_filament_bitmap_utils.cpp @@ -32,7 +32,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -296,7 +298,7 @@ TEST_CASE("Rounded custom swatches display every gradient stop in order", "[Fila wxMemoryDC dc(bitmap); dc.SetBackground(wxBrush(background)); dc.Clear(); - REQUIRE(Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, colors, true, 7)); + Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, colors, true, 7); } const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); @@ -321,7 +323,7 @@ TEST_CASE("Rounded custom swatches retain visible stops after a transparent firs wxMemoryDC dc(bitmap); dc.SetBackground(wxBrush(background)); dc.Clear(); - REQUIRE(Slic3r::GUI::draw_rounded_filament_swatch(dc, wxRect(2, 2, 120, 25), colors, true, 7)); + Slic3r::GUI::draw_rounded_filament_swatch(dc, wxRect(2, 2, 120, 25), colors, true, 7); } const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); @@ -344,7 +346,7 @@ TEST_CASE("Rounded single colors composite alpha over checkerboard cells without 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)); + Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, {wxColour(0, 0, 255, alpha)}, false, 7 * scale); } const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); @@ -372,7 +374,7 @@ TEST_CASE("Nonopaque interior colors reveal checkerboard beneath opaque endpoint 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)); + Slic3r::GUI::draw_rounded_filament_swatch(dc, wxRect(0, 0, 120, 25), colors, gradient, 7); } const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); @@ -393,7 +395,7 @@ TEST_CASE("Rounded custom swatches keep segmented colors distinct from gradients wxMemoryDC dc(bitmap); dc.SetBackground(*wxWHITE_BRUSH); dc.Clear(); - REQUIRE(Slic3r::GUI::draw_rounded_filament_swatch(dc, wxRect(0, 0, 120, 25), colors, false, 7)); + Slic3r::GUI::draw_rounded_filament_swatch(dc, wxRect(0, 0, 120, 25), colors, false, 7); } const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); @@ -418,7 +420,7 @@ TEST_CASE("Circular AMS gradients reveal interior alpha and leave their surround wxMemoryDC dc(bitmap); dc.SetBackground(wxBrush(background)); dc.Clear(); - REQUIRE(Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, colors, true, radius)); + Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, colors, true, radius); } const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); @@ -449,7 +451,7 @@ TEST_CASE("AMS card gradient checkerboards stay inside the remaining material re wxMemoryDC dc(bitmap); dc.SetBackground(wxBrush(background)); dc.Clear(); - REQUIRE(Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, colors, true, 0)); + Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, colors, true, 0); } const wxImage image = bitmap.ConvertToImage(); REQUIRE(image.IsOk()); @@ -664,6 +666,59 @@ 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("Rounded swatches match the main AMS renderer for opaque gradients and bands", "[FilamentBitmapUtils][AMSItem]") +{ + const int count = GENERATE(2, 3, 7); + const bool gradient = GENERATE(false, true); + const bool graphics_dc = GENERATE(false, true); + const int radius = GENERATE(0, 7, 12); + const std::vector all{wxColour(255, 0, 0), wxColour(255, 128, 0), wxColour(255, 255, 0), + wxColour(0, 255, 0), wxColour(0, 255, 255), wxColour(0, 0, 255), wxColour(255, 0, 255)}; + const std::vector colors(all.begin(), all.begin() + count); + const wxRect rect(2, 2, 96, 24); + wxBitmap actual(100, 28, 24), reference(100, 28, 24); + { + wxMemoryDC dc(actual); + dc.SetBackground(*wxBLACK_BRUSH); + dc.Clear(); + if (graphics_dc) { + wxGCDC graphics(dc); + Slic3r::GUI::draw_rounded_filament_swatch(graphics, rect, colors, gradient, radius); + } else { + Slic3r::GUI::draw_rounded_filament_swatch(dc, rect, colors, gradient, radius); + } + } + { + wxMemoryDC memory(reference); + memory.SetBackground(*wxBLACK_BRUSH); + memory.Clear(); + wxGCDC dc(memory); + wxGraphicsContext* gc = dc.GetGraphicsContext(); + REQUIRE(gc); + // Baseline: AMSPreview::doRender's primary drawing path from main at d4fabbf7c8. + wxGraphicsGradientStops stops(colors.front(), colors.back()); + if (gradient) { + for (size_t i = 1; i + 1 < colors.size(); ++i) + stops.Add(colors[i], static_cast(i) / (colors.size() - 1)); + } else { + for (size_t i = 1; i < colors.size(); ++i) { + const float position = static_cast(i) / colors.size(); + stops.Add(colors[i - 1], position); + stops.Add(colors[i], position); + } + } + gc->PushState(); + gc->SetPen(*wxTRANSPARENT_PEN); + 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(); + } + const wxImage image = actual.ConvertToImage(), expected = reference.ConvertToImage(); + REQUIRE(image.IsOk()); + REQUIRE(expected.IsOk()); + REQUIRE(std::equal(image.GetData(), image.GetData() + image.GetWidth() * image.GetHeight() * 3, expected.GetData())); +} + TEST_CASE("Windows menu gradient labels retain opaque pixels after native text drawing", "[FilamentBitmapUtils][Regression]") { const int alpha = GENERATE(0, 128, 255);