diff --git a/docs/HLSD/color-picker.md b/docs/HLSD/color-picker.md index ea69eeb4ca..b0d8bcbc8b 100644 --- a/docs/HLSD/color-picker.md +++ b/docs/HLSD/color-picker.md @@ -32,6 +32,10 @@ A solid selection contains one color; a gradient contains two through represents these as `{"type":"solid|gradient", "colors":["#RRGGBBAA", ...]}`. RGB input is also accepted and becomes opaque. Native validation rejects malformed hex and incorrect cardinality. +`ColorPickerOptions::max_gradient_colors` lets each caller lower the editing limit; +it defaults to seven and is clamped to two through seven. Native selection validation +and the page use the same effective limit. Shared favorites retain up to seven colors +regardless of the current caller's limit. The project filament color action and the official filament picker's More Colors action enable ordered gradients and independent alpha for each color. The preset @@ -65,7 +69,10 @@ writes legacy favorites. AMS material settings retain their fixed/AMS swatches and first-level popup. Only 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. +ordered gradients with at most four colors, matching the AMS manual-write limit. +AMS and preset swatches exceeding four colors are unavailable for selection; +the editor also filters oversized palette entries and favorites without deleting them. +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. diff --git a/resources/web/dialog/ColorPickerDialog/colorpicker.test.js b/resources/web/dialog/ColorPickerDialog/colorpicker.test.js index 2619a84930..6192b013ee 100644 --- a/resources/web/dialog/ColorPickerDialog/colorpicker.test.js +++ b/resources/web/dialog/ColorPickerDialog/colorpicker.test.js @@ -101,3 +101,21 @@ test("a new gradient starts at the solid color and its 75° hue neighbour", () = assert.equal(model.setMode(state, "gradient"), true); assert.deepEqual(model.exportSelection(state).colors, ["#FF000080", "#BFFF0080"]); }); + +test("AMS four-color limits reject oversized palettes and favorites without affecting project editing", () => { + const options = {allow_gradient: true, allow_alpha: true, max_gradient_colors: 4}; + const four = {type: "gradient", colors: ["#FF000001", "#00FF0080", "#0000FFFF", "#FF000001"]}; + const state = model.createState(options, four); + assert.deepEqual(model.exportSelection(state), four); + for (const count of [5, 6, 7]) { + const oversized = {type: "gradient", colors: [...four.colors, ...Array(count - 4).fill("#11223340")]}; + const favorites = model.normalizeFavorites([oversized]); + assert.deepEqual(favorites, [oversized]); + assert.equal(model.normalizeSelection(favorites[0], options, state.maxGradientColors), null); + assert.equal(model.applySelection(state, oversized), false); + assert.deepEqual(model.exportSelection(state), four); + const project = model.createState({...options, max_gradient_colors: 7}, favorites[0]); + assert.deepEqual(model.exportSelection(project), oversized); + assert.deepEqual(favorites, [oversized]); + } +}); diff --git a/src/slic3r/GUI/AMSMaterialsSetting.cpp b/src/slic3r/GUI/AMSMaterialsSetting.cpp index ff87052366..04ac5e19b4 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.cpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.cpp @@ -1092,7 +1092,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)) + if (colors.size() > ColorPickerPopup::MAX_EDITABLE_COLORS || + (colors.size() > 1 && (!obj || !obj->is_support_filament_manual_multi_color))) return; set_ctype(m_color_picker_popup.get_selected_ctype()); @@ -2171,7 +2172,7 @@ std::optional ColorPickerPopup::ColorItem::picker_selection(bool 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) + if (ctype != 0 || !ColorPickerOptions{allow_gradient, true, MAX_EDITABLE_COLORS}.supports_gradient(colors.size())) return std::nullopt; ColorGradient gradient; for (const wxColour& color : colors) @@ -2209,7 +2210,8 @@ void ColorPickerPopup::on_custom_clr_picker(wxMouseEvent& /*event*/) 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, {m_allow_multi_color, true}, !exact && m_def_cols.size() > 1, m_custom_cp); + ColorPickerDialog dialog(GetParent(), initial, {m_allow_multi_color, true, MAX_EDITABLE_COLORS}, + !exact && m_def_cols.size() > 1, m_custom_cp); Dismiss(); // Unchanged fallback seeds must not replace a multi-color or oversized selection. confirmed = dialog.ShowModal() == wxID_OK; @@ -2252,7 +2254,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; + if (item.colors.size() > MAX_EDITABLE_COLORS || (!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()); @@ -2295,7 +2297,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; + if (item.colors.size() > MAX_EDITABLE_COLORS || (!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 ebabe91a71..3fbadcbf44 100644 --- a/src/slic3r/GUI/AMSMaterialsSetting.hpp +++ b/src/slic3r/GUI/AMSMaterialsSetting.hpp @@ -18,6 +18,7 @@ #include "slic3r/Utils/CalibUtils.hpp" #include #include +#include #include #include #include @@ -84,6 +85,9 @@ public: class ColorPickerPopup : public PopupWindow { public: + // Manual AMS color writes support at most four components. + static constexpr std::size_t MAX_EDITABLE_COLORS = 4; + struct ColorItem { std::vector colors; diff --git a/src/slic3r/GUI/ColorPickerDialog.cpp b/src/slic3r/GUI/ColorPickerDialog.cpp index 086eae3a70..f1ac3e12bb 100644 --- a/src/slic3r/GUI/ColorPickerDialog.cpp +++ b/src/slic3r/GUI/ColorPickerDialog.cpp @@ -104,7 +104,7 @@ std::optional normalize_color_selection(const ColorSelection& se return std::nullopt; } else { auto& gradient = std::get(normalized); - if (!options.allow_gradient || gradient.size() < 2 || gradient.size() > COLOR_PICKER_MAX_GRADIENT_COLORS) + if (!options.supports_gradient(gradient.size())) return std::nullopt; for (ColorRGBA& color : gradient) if (!normalize_color(color, options.allow_alpha)) @@ -125,7 +125,7 @@ std::optional color_selection_from_json(const nlohmann::json& va ColorRGBA solid; if (parse_color((*colors)[0], solid)) return normalize_color_selection(solid, options); - } else if (*type == "gradient" && options.allow_gradient && colors->size() >= 2 && colors->size() <= COLOR_PICKER_MAX_GRADIENT_COLORS) { + } else if (*type == "gradient" && options.supports_gradient(colors->size())) { ColorGradient gradient(colors->size()); for (std::size_t i = 0; i < gradient.size(); ++i) if (!parse_color((*colors)[i], gradient[i])) @@ -1062,7 +1062,7 @@ void ColorPickerDialog::send_initial_state() favorites.push_back(color_selection_to_json(selection)); const nlohmann::json payload = {{"command", "init"}, {"page_id", m_page_id}, {"options", {{"allow_gradient", m_options.allow_gradient}, {"allow_alpha", m_options.allow_alpha}, - {"max_gradient_colors", COLOR_PICKER_MAX_GRADIENT_COLORS}}}, + {"max_gradient_colors", m_options.gradient_color_limit()}}}, {"selection", color_selection_to_json(m_initial)}, {"favorites", std::move(favorites)}, {"favorites_writable", m_favorites_writable}, {"preserve_multi_color", m_preserve_multi_color}, {"ui", {{"sidebar_open", m_sidebar_open}, {"palette", m_palette}}}}; diff --git a/src/slic3r/GUI/ColorPickerDialog.hpp b/src/slic3r/GUI/ColorPickerDialog.hpp index 33aa59e087..f34c63625a 100644 --- a/src/slic3r/GUI/ColorPickerDialog.hpp +++ b/src/slic3r/GUI/ColorPickerDialog.hpp @@ -3,6 +3,7 @@ #include "libslic3r/Color.hpp" #include "Widgets/WebViewHostDialog.hpp" +#include #include #include #include @@ -18,9 +19,7 @@ class wxWindow; namespace Slic3r::GUI { -// Most colors an ordered gradient may hold. This is the native switch for multi-color gradients: -// 2 keeps the classic two-color gradients. The page receives it as options.max_gradient_colors -// and never offers more, so the picker, favorites and project colors all share one limit. +// Shared storage limit; individual picker callers may request a lower editing limit. inline constexpr std::size_t COLOR_PICKER_MAX_GRADIENT_COLORS = 7; // Gradient colors retain their order (2..COLOR_PICKER_MAX_GRADIENT_COLORS); a solid has exactly one color. @@ -30,6 +29,17 @@ using ColorSelection = std::variant; struct ColorPickerOptions { bool allow_gradient = false; bool allow_alpha = false; + std::size_t max_gradient_colors = COLOR_PICKER_MAX_GRADIENT_COLORS; + + constexpr std::size_t gradient_color_limit() const + { + return std::clamp(max_gradient_colors, std::size_t{2}, COLOR_PICKER_MAX_GRADIENT_COLORS); + } + + constexpr bool supports_gradient(std::size_t count) const + { + return allow_gradient && count >= 2 && count <= gradient_color_limit(); + } }; // Project storage preserves direction; FilamentColor's set is only for the diff --git a/tests/slic3rutils/test_ams_materials_setting.cpp b/tests/slic3rutils/test_ams_materials_setting.cpp index 9bd06e963d..dd82561180 100644 --- a/tests/slic3rutils/test_ams_materials_setting.cpp +++ b/tests/slic3rutils/test_ams_materials_setting.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -37,7 +38,7 @@ TEST_CASE("AMS custom gradients retain repeated nodes direction and independent 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) { + for (size_t count = 2; count <= std::min(colors.size(), ColorPickerPopup::MAX_EDITABLE_COLORS); ++count) { ColorItem original{{colors.begin(), colors.begin() + count}, 0}; if (reverse) std::reverse(original.colors.begin(), original.colors.end()); @@ -59,10 +60,20 @@ TEST_CASE("AMS custom editing only accepts representable gradients on capable fi 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_FALSE(ColorItem{std::vector(ColorPickerPopup::MAX_EDITABLE_COLORS + 1, colors.front()), 0}.picker_selection(true)); REQUIRE(ColorItem{colors, 0}.picker_selection(true)); } +TEST_CASE("AMS editing rejects five through seven colors while project editing retains them", "[AMSMaterialsSetting]") +{ + const auto count = GENERATE(std::size_t{5}, std::size_t{6}, COLOR_PICKER_MAX_GRADIENT_COLORS); + const ColorItem original{std::vector(count, wxColour(12, 34, 56, 78)), 0}; + REQUIRE_FALSE(original.picker_selection(true)); + const auto project_selection = filament_color_picker_selection({std::vector(count, "#0C22384E"), true}); + REQUIRE(project_selection); + REQUIRE(ColorItem::from_picker_selection(*project_selection).colors == original.colors); +} + 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}; diff --git a/tests/slic3rutils/test_filament_color_picker.cpp b/tests/slic3rutils/test_filament_color_picker.cpp index b3da29a101..1a022a262e 100644 --- a/tests/slic3rutils/test_filament_color_picker.cpp +++ b/tests/slic3rutils/test_filament_color_picker.cpp @@ -14,6 +14,28 @@ using namespace Slic3r; using namespace Slic3r::GUI; +TEST_CASE("Picker callers can lower the gradient limit without changing the shared default", "[FilamentColorPicker]") +{ + const auto limit = GENERATE(std::size_t{2}, std::size_t{4}, COLOR_PICKER_MAX_GRADIENT_COLORS); + const ColorPickerOptions options{true, true, limit}; + REQUIRE(options.gradient_color_limit() == limit); + REQUIRE_FALSE(options.supports_gradient(1)); + REQUIRE(options.supports_gradient(2)); + REQUIRE(options.supports_gradient(limit)); + REQUIRE_FALSE(options.supports_gradient(limit + 1)); + REQUIRE_FALSE(ColorPickerOptions{false, true, limit}.supports_gradient(2)); + REQUIRE(ColorPickerOptions{true, true}.supports_gradient(COLOR_PICKER_MAX_GRADIENT_COLORS)); +} + +TEST_CASE("Picker gradient limits stay within supported storage bounds", "[FilamentColorPicker]") +{ + const auto requested = GENERATE(std::size_t{0}, std::size_t{1}, COLOR_PICKER_MAX_GRADIENT_COLORS + 1); + const ColorPickerOptions options{true, true, requested}; + REQUIRE(options.gradient_color_limit() == (requested < 2 ? 2 : COLOR_PICKER_MAX_GRADIENT_COLORS)); + REQUIRE(options.supports_gradient(2)); + REQUIRE_FALSE(options.supports_gradient(COLOR_PICKER_MAX_GRADIENT_COLORS + 1)); +} + // One color more than an ordered gradient may hold. static std::vector too_many_gradient_colors() {