diff --git a/docs/HLSD/color-picker.md b/docs/HLSD/color-picker.md index b753477b3b..ea69eeb4ca 100644 --- a/docs/HLSD/color-picker.md +++ b/docs/HLSD/color-picker.md @@ -27,13 +27,14 @@ parent-centering constraint and reapplies its native position after showing, bypassing wxGTK's requested-position cache. Absolute top-level placement remains subject to the window manager; Wayland compositors control it. The official filament palette's existing placement is independent of this custom panel. -A solid selection contains one color; a gradient contains exactly two ordered -endpoints. The bridge represents these as `{"type":"solid|gradient", +A solid selection contains one color; a gradient contains two through +`COLOR_PICKER_MAX_GRADIENT_COLORS` ordered colors (currently seven). The bridge +represents these as `{"type":"solid|gradient", "colors":["#RRGGBBAA", ...]}`. RGB input is also accepted and becomes opaque. Native validation rejects malformed hex and incorrect cardinality. The project filament color action and the official filament picker's More Colors -action enable two-endpoint gradients and independent endpoint alpha. The preset +action enable ordered gradients and independent alpha for each color. The preset combo box reads project colors in their stored vector order locally and applies changes through `sync_colour_config`, retaining the existing RGB/RGBA profile/project formats and dirty marking. @@ -47,7 +48,7 @@ official swatch retains its existing palette behavior. The Linux sidebar's Change extruder color menu uses this same custom editor and the filament swatch anchor, including alpha, ordered gradients and configuration syncing. -Striped colors and gradients with more than two endpoints cannot be represented +Striped colors and gradients exceeding the configured color limit cannot be represented by this editor. A notice explains that the current selection is retained; the editor starts from the first color, and unchanged confirmation is a no-op. Cancellation is also a no-op. If the WebView backend cannot initialize, a local @@ -62,11 +63,19 @@ changes. Programmatic assignments remain silent, and reading a field never writes legacy favorites. AMS material settings retain their fixed/AMS swatches and first-level popup. Only -its custom-color action opens the solid, opaque editor, anchored to that custom -control. The transient popup releases its grab while the modal is open and returns -to the same selection on cancellation. Confirmation uses the existing default-color -and packed RGBA event path. Device permission/status checks remain at the first-level -entry. +its custom-color action opens the editor with alpha enabled, anchored to that custom +control. Firmware advertising manual multi-color support (`fun2[23]`) also enables +ordered gradients. Other printers only offer single-color swatches and favorites. +The transient popup releases its grab while the modal is open and returns +to the same selection on cancellation. Unchanged confirmation is a no-op. Existing striped +or oversized selections start from their first color with the preservation notice. +Confirmed edits retain the complete ordered RGBA list and color type in the popup; +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. 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 f723ef13d3..3ccd72da76 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.cpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -1016,6 +1017,8 @@ void AMSMaterialsSetting::set_empty_color(wxColour color) { m_clr_picker->is_empty(true); m_clr_picker->set_color(color); + m_clr_picker->set_colors({}); + m_clr_picker->ctype = 2; m_clr_name->SetLabelText(wxEmptyString); } @@ -1049,6 +1052,8 @@ void AMSMaterialsSetting::on_picker_color(wxCommandEvent& event) unsigned int color_num = event.GetInt(); colors.emplace_back(color_num >> 24 & 0xFF, color_num >> 16 & 0xFF, color_num >> 8 & 0xFF, color_num & 0xFF); } + if (colors.size() > 1 && (!obj || !obj->is_support_filament_manual_multi_color)) + return; set_ctype(m_color_picker_popup.get_selected_ctype()); set_color(colors.front()); @@ -1067,6 +1072,7 @@ void AMSMaterialsSetting::on_clr_picker(wxMouseEvent &event) } } + m_color_picker_popup.set_allow_multi_color(obj->is_support_filament_manual_multi_color); m_color_picker_popup.set_ams_colours(collect_ams_color_items(obj->GetFilaSystem().get())); m_color_picker_popup.set_preset_colours(collect_preset_color_items(ams_filament_id)); m_color_picker_popup.set_def_colour(m_clr_picker->m_colour, m_clr_picker->m_cols, m_clr_picker->ctype); @@ -1850,6 +1856,13 @@ void ColorPicker::render(wxDC& dc) memdc.SelectObject(wxNullBitmap); 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; + } + } doRender(dc); #endif } @@ -2110,32 +2123,70 @@ ColorPickerPopup::ColorPickerPopup(wxWindow* parent) wxGetApp().UpdateDarkUIWin(this); } +std::optional ColorPickerPopup::ColorItem::picker_selection(bool allow_gradient) const +{ + if (colors.empty()) + return std::nullopt; + if (colors.size() == 1) { + const wxColour& color = colors.front(); + return ColorRGBA(color.Red(), color.Green(), color.Blue(), color.Alpha()); + } + if (!allow_gradient || ctype != 0 || colors.size() > COLOR_PICKER_MAX_GRADIENT_COLORS) + return std::nullopt; + ColorGradient gradient; + for (const wxColour& color : colors) + gradient.emplace_back(color.Red(), color.Green(), color.Blue(), color.Alpha()); + return gradient; +} + +ColorPickerPopup::ColorItem ColorPickerPopup::ColorItem::from_picker_selection(const ColorSelection& selection) +{ + ColorItem item; + auto append = [&item](const ColorRGBA& color) { + item.colors.emplace_back(static_cast(std::lround(color.r() * 255.f)), + static_cast(std::lround(color.g() * 255.f)), + static_cast(std::lround(color.b() * 255.f)), + static_cast(std::lround(color.a() * 255.f))); + }; + if (const auto* solid = std::get_if(&selection)) { + append(*solid); + } else { + for (const ColorRGBA& color : std::get(selection)) + append(color); + item.ctype = 0; + } + return item; +} + void ColorPickerPopup::on_custom_clr_picker(wxMouseEvent& /*event*/) { const bool was_shown = IsShown(); - std::optional picker_color; + bool confirmed = false; + std::optional picker_color; { - const ColorRGBA initial(m_def_col.Red(), m_def_col.Green(), m_def_col.Blue(), static_cast(255)); + const ColorItem current{m_def_cols, m_def_ctype}; + const auto exact = current.picker_selection(m_allow_multi_color); + const wxColour& first = m_def_cols.empty() ? m_def_col : m_def_cols.front(); + const ColorSelection initial = exact ? *exact : ColorSelection{ColorRGBA(first.Red(), first.Green(), first.Blue(), first.Alpha())}; // Snapshot the custom control's anchor before releasing the transient popup grab. - ColorPickerDialog dialog(GetParent(), initial, {}, false, m_custom_cp); + ColorPickerDialog dialog(GetParent(), initial, {m_allow_multi_color, true}, !exact && m_def_cols.size() > 1, m_custom_cp); Dismiss(); - if (dialog.ShowModal() == wxID_OK && dialog.selection()) { - const auto& result = std::get(*dialog.selection()); - picker_color = wxColour(static_cast(std::lround(result.r() * 255.f)), - static_cast(std::lround(result.g() * 255.f)), - static_cast(std::lround(result.b() * 255.f))); - } + // Unchanged fallback seeds must not replace a multi-color or oversized selection. + confirmed = dialog.ShowModal() == wxID_OK; + if (confirmed && filament_color_picker_changed(initial, dialog.selection())) + picker_color = ColorItem::from_picker_selection(*dialog.selection()); } // Destroy the modal before restoring the first-level popup and its focus/grab. if (!picker_color) { - if (was_shown) Popup(); + if (was_shown && !confirmed) Popup(); return; } - set_def_colour(*picker_color); + const wxColour& primary = picker_color->colors.front(); + set_def_colour(primary, picker_color->colors, picker_color->ctype); wxCommandEvent evt(EVT_SELECTED_COLOR); - const std::uint32_t packed = (std::uint32_t(picker_color->Red()) << 24) | - (std::uint32_t(picker_color->Green()) << 16) | - (std::uint32_t(picker_color->Blue()) << 8) | 255u; + const std::uint32_t packed = (std::uint32_t(primary.Red()) << 24) | + (std::uint32_t(primary.Green()) << 16) | + (std::uint32_t(primary.Blue()) << 8) | std::uint32_t(primary.Alpha()); evt.SetInt(static_cast(packed)); wxPostEvent(GetParent(), evt); } @@ -2161,6 +2212,7 @@ void ColorPickerPopup::set_ams_colours(const std::vector& ams) m_ams_color_items = ams; for (const ColorItem& item : m_ams_color_items) { if (item.colors.empty()) continue; + if (!m_allow_multi_color && item.colors.size() > 1) continue; auto cp = new ColorPicker(m_def_color_box, wxID_ANY, wxDefaultPosition, wxDefaultSize); cp->set_color(item.colors.front()); @@ -2203,6 +2255,7 @@ void ColorPickerPopup::set_preset_colours(const std::vector& preset_c for (const ColorItem& item : preset_colors) { if (item.colors.empty()) continue; + if (!m_allow_multi_color && item.colors.size() > 1) continue; auto cp = new ColorPicker(m_def_color_box, wxID_ANY, wxDefaultPosition, wxDefaultSize); cp->set_color(item.colors.front()); diff --git a/src/slic3r/GUI/AMSMaterialsSetting.hpp b/src/slic3r/GUI/AMSMaterialsSetting.hpp index 799b5d800b..ebabe91a71 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.hpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.hpp @@ -2,6 +2,7 @@ #define slic3r_AMSMaterialsSetting_hpp_ #include "libslic3r/Preset.hpp" +#include "ColorPickerDialog.hpp" #include "wxExtensions.hpp" #include "GUI_Utils.hpp" #include "DeviceManager.hpp" @@ -32,6 +33,7 @@ #include #include #include +#include #define AMS_MATERIALS_SETTING_DEF_COLOUR wxColour(255, 255, 255) #define AMS_MATERIALS_SETTING_GREY900 wxColour(38, 46, 48) @@ -60,7 +62,7 @@ public: bool m_selected{false}; bool m_show_full{false}; bool m_is_empty{false}; - int ctype; + int ctype = 2; bool transparent_changed{false}; @@ -87,6 +89,9 @@ public: std::vector colors; int ctype = 2; wxString name; + + std::optional picker_selection(bool allow_gradient) const; + static ColorItem from_picker_selection(const ColorSelection& selection); }; ScalableBitmap m_ts_bitmap_custom; @@ -105,6 +110,7 @@ public: ColorPickerPopup(wxWindow* parent); ~ColorPickerPopup() {}; void on_custom_clr_picker(wxMouseEvent& event); + void set_allow_multi_color(bool allow) { m_allow_multi_color = allow; } void set_ams_colours(const std::vector& ams); void set_preset_colours(const std::vector& preset_colors); void set_def_colour(wxColour col, std::vector cols = {}, int ctype = 2); @@ -119,6 +125,7 @@ private: std::vector m_preset_color_pickers; std::vector m_def_cols; int m_def_ctype = 2; + bool m_allow_multi_color = false; }; diff --git a/src/slic3r/GUI/FilamentBitmapUtils.cpp b/src/slic3r/GUI/FilamentBitmapUtils.cpp index 02a7b9fb46..815a8f7183 100644 --- a/src/slic3r/GUI/FilamentBitmapUtils.cpp +++ b/src/slic3r/GUI/FilamentBitmapUtils.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include #include "libslic3r/Config.hpp" @@ -73,9 +75,9 @@ TriPoint tri_clamp(TriPoint p, TriPoint v0, TriPoint v1, TriPoint v2) w0 * v0.y + w1 * v1.y + w2 * v2.y}; } -void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const wxColour& from, const wxColour& to) +static void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const wxColour* colors, size_t count) { - if (rect.width <= 0 || rect.height <= 0) return; + if (rect.width <= 0 || rect.height <= 0 || count == 0) return; auto mix_channel = [](unsigned char a, unsigned char b, double t) { return static_cast(a + (b - a) * t + 0.5); @@ -83,7 +85,11 @@ void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const wxColour& from, dc.SetPen(*wxTRANSPARENT_PEN); for (int x = 0; x < rect.width; ++x) { - const double t = rect.width > 1 ? static_cast(x) / (rect.width - 1) : 0.0; + const double position = rect.width > 1 ? static_cast(x) * (count - 1) / (rect.width - 1) : 0.0; + const size_t index = std::min(static_cast(position), count - 1); + const wxColour& from = colors[index]; + const wxColour& to = colors[std::min(index + 1, count - 1)]; + const double t = position - index; const wxColour col(mix_channel(from.Red(), to.Red(), t), mix_channel(from.Green(), to.Green(), t), mix_channel(from.Blue(), to.Blue(), t), @@ -93,6 +99,25 @@ void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const wxColour& from, } } +void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const wxColour& from, const wxColour& to) +{ + const std::array colors{from, to}; + fill_gradient_rect_east(dc, rect, colors.data(), colors.size()); +} + +void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const std::vector& colors) +{ + // Plain GTK DCs can ignore brush alpha; use the graphics backend for transparent stops. + if (!dc.GetGraphicsContext() && std::any_of(colors.begin(), colors.end(), [](const wxColour& color) { return color.Alpha() != 255; })) { + if (auto* context = wxGraphicsContext::CreateFromUnknownDC(dc)) { + wxGCDC graphics(context); + fill_gradient_rect_east(graphics, rect, colors.data(), colors.size()); + return; + } + } + fill_gradient_rect_east(dc, rect, colors.data(), colors.size()); +} + static std::string to_hex(const wxColour& c) { return wxString::Format("#%02X%02X%02X", c.Red(), c.Green(), c.Blue()).ToStdString(); diff --git a/src/slic3r/GUI/FilamentBitmapUtils.hpp b/src/slic3r/GUI/FilamentBitmapUtils.hpp index b95a61a1b6..1dc0cc036b 100644 --- a/src/slic3r/GUI/FilamentBitmapUtils.hpp +++ b/src/slic3r/GUI/FilamentBitmapUtils.hpp @@ -31,6 +31,8 @@ TriPoint tri_clamp(TriPoint p, TriPoint v0, TriPoint v1, TriPoint v2); // Use instead of wxDC::GradientFillLinear, whose CoreGraphics (CGShading) backend // fails to render on some macOS builds; solid fills are unaffected. void fill_gradient_rect_east(wxDC& dc, const wxRect& rect, const wxColour& from, const wxColour& to); +// 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); enum class FilamentRenderMode { Single, diff --git a/src/slic3r/GUI/Widgets/AMSItem.cpp b/src/slic3r/GUI/Widgets/AMSItem.cpp index c1938d06dc..c5fa718fb6 100644 --- a/src/slic3r/GUI/Widgets/AMSItem.cpp +++ b/src/slic3r/GUI/Widgets/AMSItem.cpp @@ -1393,7 +1393,7 @@ void AMSLib::render_lite_lib(wxDC& dc) temp_bitmap_brand = m_bitmap_readonly; } - if (tmp_lib_colour.Alpha() == 0) { + if (tmp_lib_colour.Alpha() == 0 && m_info.material_cols.size() <= 1) { temp_bitmap_third = m_bitmap_editable; temp_bitmap_brand = m_bitmap_readonly; @@ -1413,7 +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) { - fill_gradient_rect_east(dc, color_rect, m_info.material_cols.front(), m_info.material_cols.back()); + fill_gradient_rect_east(dc, color_rect, m_info.material_cols); } else { int cols_size = m_info.material_cols.size(); @@ -1514,9 +1514,10 @@ void AMSLib::render_generic_lib(wxDC &dc) if (m_ams_model == EXT_AMS){ wxRect color_rect(FromDIP(1), FromDIP(1), size.x - FromDIP(2), size.y - FromDIP(1)); - if ((m_info.ctype == 0 || m_info.ctype == 1) && m_info.material_cols.size() > 1 && alpha != 0) { + 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) { - fill_gradient_rect_east(dc, color_rect, m_info.material_cols.front(), m_info.material_cols.back()); + fill_gradient_rect_east(dc, color_rect, m_info.material_cols); } else { int cols_size = static_cast(m_info.material_cols.size()); @@ -1534,10 +1535,10 @@ void AMSLib::render_generic_lib(wxDC &dc) else { dc.DrawRoundedRectangle(color_rect.x, color_rect.y, color_rect.width, color_rect.height, m_radius - 1); } - if (alpha == 0) { + if (!multi_color && alpha == 0) { dc.DrawBitmap(m_bitmap_transparent_def.bmp(), FromDIP(2), FromDIP(2)); } - else if (alpha != 255 && alpha != 254) { + else if (!multi_color && alpha != 255 && alpha != 254) { if (transparent_changed) { transparent_changed = false; } @@ -1595,10 +1596,10 @@ void AMSLib::render_generic_lib(wxDC &dc) if (curr_height >= FromDIP(1)) { //transparent - if (alpha == 0) { + if (m_info.material_cols.size() <= 1 && alpha == 0) { dc.DrawBitmap(m_bitmap_transparent_def.bmp(), FromDIP(2), FromDIP(2)); } - else if (alpha != 255 && alpha != 254) { + else if (m_info.material_cols.size() <= 1 && alpha != 255 && alpha != 254) { if (transparent_changed) { std::string rgb = (tmp_lib_colour.GetAsString(wxC2S_HTML_SYNTAX)).ToStdString(); if (rgb.size() == 9) { @@ -1618,25 +1619,14 @@ void AMSLib::render_generic_lib(wxDC &dc) } //gradient if (m_info.material_cols.size() > 1) { - auto left = FromDIP(2); - float total_width = size.x - FromDIP(4); - int gwidth = std::round(total_width / (m_info.material_cols.size() - 1)); //gradient if (m_info.ctype == 0) { - for (int i = 0; i < m_info.material_cols.size() - 1; i++) { - - if ((left + gwidth) > (size.x)) { - gwidth = (size.x) - left; - } - - auto rect = wxRect(left, height - curr_height, gwidth, curr_height); - fill_gradient_rect_east(dc, rect, m_info.material_cols[i], m_info.material_cols[i + 1]); - left += gwidth; - } + fill_gradient_rect_east(dc, wxRect(FromDIP(2), height - curr_height, size.x - FromDIP(4), curr_height), m_info.material_cols); } else { //multicolour - gwidth = std::round(total_width / m_info.material_cols.size()); + auto left = FromDIP(2); + int gwidth = std::round(float(size.x - FromDIP(4)) / m_info.material_cols.size()); for (int i = 0; i < m_info.material_cols.size(); i++) { dc.SetPen(wxPen(*wxTRANSPARENT_PEN)); dc.SetBrush(wxBrush(m_info.material_cols[i])); @@ -2791,19 +2781,8 @@ void AMSPreview::doRender(wxDC &dc) dc.SetBrush(AMS_CONTROL_DISABLE_COLOUR); } if (iter->material_cols.size() > 1) { - int fleft = left; - - int gwidth = (AMS_ITEM_CUBE_SIZE.x / (iter->material_cols.size() - 1)); if (iter->ctype == 0) { - for (int i = 0; i < iter->material_cols.size() - 1; i++) { - if (fleft + gwidth > (AMS_ITEM_CUBE_SIZE.x)) { - gwidth = fleft + AMS_ITEM_CUBE_SIZE.x - fleft; - } - - auto rect = wxRect(fleft, (size.y - AMS_ITEM_CUBE_SIZE.y) / 2, gwidth, AMS_ITEM_CUBE_SIZE.y); - fill_gradient_rect_east(dc, rect, iter->material_cols[i], iter->material_cols[i + 1]); - fleft += gwidth; - } + fill_gradient_rect_east(dc, wxRect(left, (size.y - AMS_ITEM_CUBE_SIZE.y) / 2, AMS_ITEM_CUBE_SIZE.x, AMS_ITEM_CUBE_SIZE.y), iter->material_cols); } else { int cols_size = iter->material_cols.size(); diff --git a/tests/slic3rutils/CMakeLists.txt b/tests/slic3rutils/CMakeLists.txt index 36e88cb37d..b9c280b427 100644 --- a/tests/slic3rutils/CMakeLists.txt +++ b/tests/slic3rutils/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(${_TEST_NAME}_tests test_filament_bitmap_utils.cpp test_colour_picker_field.cpp test_filament_color_picker.cpp + test_ams_materials_setting.cpp test_device_progress.cpp test_device_manager_integration.cpp test_web_media_controller.cpp diff --git a/tests/slic3rutils/test_ams_materials_setting.cpp b/tests/slic3rutils/test_ams_materials_setting.cpp new file mode 100644 index 0000000000..9bd06e963d --- /dev/null +++ b/tests/slic3rutils/test_ams_materials_setting.cpp @@ -0,0 +1,83 @@ +#include +#include + +#include "slic3r/GUI/AMSMaterialsSetting.hpp" +#include "slic3r/GUI/ColorPickerDialog.hpp" +#include "libslic3r/Color.hpp" + +#include +#include +#include +#include +#include +#include + +using namespace Slic3r; +using namespace Slic3r::GUI; +using ColorItem = ColorPickerPopup::ColorItem; + +TEST_CASE("AMS custom solid colors preserve every RGBA byte with either firmware capability", "[AMSMaterialsSetting]") +{ + const bool allow_gradient = GENERATE(false, true); + for (int byte = 0; byte < 256; ++byte) { + const ColorItem original{{wxColour(byte, 255 - byte, byte ^ 0x55, byte)}, 2}; + const auto selection = original.picker_selection(allow_gradient); + REQUIRE(selection); + REQUIRE(std::holds_alternative(*selection)); + const ColorItem result = ColorItem::from_picker_selection(*selection); + REQUIRE(result.ctype == 2); + REQUIRE(result.colors == original.colors); + } +} + +TEST_CASE("AMS custom gradients retain repeated nodes direction and independent alpha", "[AMSMaterialsSetting]") +{ + const std::vector colors{wxColour(255, 0, 0, 0), wxColour(0, 255, 0, 1), + wxColour(0, 0, 255, 128), wxColour(255, 0, 0, 0), + wxColour(255, 255, 0, 254), wxColour(0, 255, 255, 255), + wxColour(128, 64, 32, 80)}; + const bool reverse = GENERATE(false, true); + for (size_t count = 2; count <= std::min(colors.size(), COLOR_PICKER_MAX_GRADIENT_COLORS); ++count) { + ColorItem original{{colors.begin(), colors.begin() + count}, 0}; + if (reverse) + std::reverse(original.colors.begin(), original.colors.end()); + const auto selection = original.picker_selection(true); + REQUIRE(selection); + REQUIRE(std::holds_alternative(*selection)); + const ColorItem result = ColorItem::from_picker_selection(*selection); + REQUIRE(result.ctype == 0); + REQUIRE(result.colors == original.colors); + REQUIRE_FALSE(filament_color_picker_changed(*selection, result.picker_selection(true))); + REQUIRE_FALSE(filament_color_picker_changed(*selection, std::nullopt)); + } +} + +TEST_CASE("AMS custom editing only accepts representable gradients on capable firmware", "[AMSMaterialsSetting]") +{ + const std::vector colors{wxColour(255, 0, 0, 0), wxColour(0, 0, 255, 128)}; + REQUIRE_FALSE(ColorItem{}.picker_selection(true)); + REQUIRE_FALSE(ColorItem{colors, 0}.picker_selection(false)); + REQUIRE_FALSE(ColorItem{colors, 1}.picker_selection(true)); + REQUIRE_FALSE(ColorItem{colors, 2}.picker_selection(true)); + REQUIRE_FALSE(ColorItem{std::vector(COLOR_PICKER_MAX_GRADIENT_COLORS + 1, colors.front()), 0}.picker_selection(true)); + REQUIRE(ColorItem{colors, 0}.picker_selection(true)); +} + +TEST_CASE("AMS alpha only edits and switching a gradient to solid replace the complete selection", "[AMSMaterialsSetting]") +{ + const ColorItem original{{wxColour(255, 0, 0, 0), wxColour(0, 0, 255, 128)}, 0}; + const auto initial = original.picker_selection(true); + REQUIRE(initial); + const ColorItem edited{{wxColour(255, 0, 0, 1), wxColour(0, 0, 255, 129)}, 0}; + const auto selection = edited.picker_selection(true); + REQUIRE(selection); + REQUIRE(filament_color_picker_changed(*initial, selection)); + REQUIRE(ColorItem::from_picker_selection(*selection).colors == edited.colors); + + const ColorSelection solid = ColorRGBA(static_cast(255), static_cast(0), + static_cast(0), static_cast(0)); + REQUIRE(filament_color_picker_changed(*initial, solid)); + const ColorItem result = ColorItem::from_picker_selection(solid); + REQUIRE(result.ctype == 2); + REQUIRE(result.colors == std::vector{original.colors.front()}); +} diff --git a/tests/slic3rutils/test_device_manager_integration.cpp b/tests/slic3rutils/test_device_manager_integration.cpp index dc3c492803..91bb3a3663 100644 --- a/tests/slic3rutils/test_device_manager_integration.cpp +++ b/tests/slic3rutils/test_device_manager_integration.cpp @@ -208,7 +208,7 @@ TEST_CASE("AMS filament settings preserve ordered gradient and multicolor RGBA c MachineObject object(&manager, &network, "Test printer", "integration-device", "192.0.2.10"); object.dev_connection_type = GENERATE("lan", "cloud"); const int color_type = GENERATE(0, 1); - const std::vector colors{"FF000080", "0000FF40", "00FF00FF"}; + const std::vector colors{"FF000000", "0000FF40", "00FF00FF", "FF000000", "12345601", "FEDCBAFE", "ABCDEF80"}; REQUIRE(object.command_ams_filament_settings(1, 2, "filament-id", "setting-id", colors.front(), "PLA", 190, 220, colors, color_type) == 0); diff --git a/tests/slic3rutils/test_filament_bitmap_utils.cpp b/tests/slic3rutils/test_filament_bitmap_utils.cpp index 600e8a23c8..26d630d417 100644 --- a/tests/slic3rutils/test_filament_bitmap_utils.cpp +++ b/tests/slic3rutils/test_filament_bitmap_utils.cpp @@ -20,11 +20,17 @@ #include #include +#include #include #ifdef __WXMSW__ #include +#include #include +#include +#include +#include +#include #include #include #include @@ -271,6 +277,52 @@ TEST_CASE("sample_gradient_ramp returns nothing without a usable curve or step c // The headless GTK/macOS suite does not initialize GUI display/font stock objects. // Exercise native bitmap pixels on Windows without adding that dependency elsewhere. #ifdef __WXMSW__ +TEST_CASE("Ordered gradient fills preserve middle nodes and cover narrow swatches", "[FilamentBitmapUtils][AMSMaterialsSetting]") +{ + const int width = GENERATE(1, 2, 3, 7, 13, 17); + const bool reverse = GENERATE(false, true); + std::vector colors{wxColour(255, 0, 0), wxColour(0, 255, 0), wxColour(0, 0, 255)}; + if (reverse) + std::reverse(colors.begin(), colors.end()); + wxBitmap bitmap(width + 4, 5, 24); + { + wxMemoryDC dc(bitmap); + Slic3r::GUI::fill_gradient_rect_east(dc, wxRect(2, 1, width, 3), colors); + } + const wxImage image = bitmap.ConvertToImage(); + REQUIRE(image.IsOk()); + auto color_at = [&image](int x) { return wxColour(image.GetRed(x, 2), image.GetGreen(x, 2), image.GetBlue(x, 2)); }; + require_same_rgb(color_at(2), colors.front()); + require_same_rgb(color_at(width + 1), width == 1 ? colors.front() : colors.back()); + if (width >= 3) + require_same_rgb(color_at(2 + width / 2), colors[1]); +} + +TEST_CASE("Ordered gradients composite independent node alpha over the existing background", "[FilamentBitmapUtils][AMSMaterialsSetting]") +{ + const bool graphics_dc = GENERATE(false, true); + const std::vector colors{wxColour(255, 0, 0, 0), wxColour(0, 255, 0, 128), wxColour(0, 0, 255, 255)}; + wxBitmap bitmap(9, 3, 24); + { + wxMemoryDC memory(bitmap); + memory.SetBackground(*wxWHITE_BRUSH); + memory.Clear(); + if (graphics_dc) { + wxGCDC dc(memory); + Slic3r::GUI::fill_gradient_rect_east(dc, wxRect(0, 0, 9, 3), colors); + } else { + Slic3r::GUI::fill_gradient_rect_east(memory, wxRect(0, 0, 9, 3), colors); + } + } + const wxImage image = bitmap.ConvertToImage(); + REQUIRE(image.IsOk()); + require_same_rgb(wxColour(image.GetRed(0, 1), image.GetGreen(0, 1), image.GetBlue(0, 1)), wxColour(255, 255, 255)); + REQUIRE(std::abs(int(image.GetRed(4, 1)) - 127) <= 1); + REQUIRE(image.GetGreen(4, 1) == 255); + REQUIRE(std::abs(int(image.GetBlue(4, 1)) - 127) <= 1); + require_same_rgb(wxColour(image.GetRed(8, 1), image.GetGreen(8, 1), image.GetBlue(8, 1)), wxColour(0, 0, 255)); +} + TEST_CASE("Filament gradient pixels follow both endpoint directions without changing input", "[FilamentBitmapUtils]") { const bool reverse = GENERATE(false, true);