From 1e31f9b94ccec4dd64c78dd94914cf62d2607daf Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 29 Nov 2026 10:45:42 -0800 Subject: [PATCH] Document Tools: Flood Fill Artboards refuses infinite colors and keeps the fill on Cancel - A color component too large for f32 (c: 1e308, [1e39, 0, 0], gray or Lab out of range) read as infinite and reached the rectangles; the command now refuses it with "bad color". - The dialog's Color option opened on the current fill in display sRGB and sent it as working RGB, so a document whose working RGB space is not sRGB got a shifted color. The dialog now opens on the fill's display color and converts the picked color back to working RGB. - With New Art Has Basic Appearance off and nothing selected, a previewed color gave the new rectangles' fill to the new-art paints, and Cancel or a replaced preview kept it. Both now put back the paints the interaction began with. Docs: docs/mcp.md. --- crates/engine/src/cmd/addons/document.rs | 2 +- .../src/cmd/addons/document/flood_fill.rs | 13 ++++- .../engine/src/cmd/addons/document/tests.rs | 42 ++++++++++++++ crates/engine/src/session/edit.rs | 9 ++- crates/ui-egui/src/addons/document.rs | 18 +++--- crates/ui-egui/src/addons/document/tests.rs | 56 ++++++++++++++----- docs/mcp.md | 2 +- 7 files changed, 113 insertions(+), 29 deletions(-) diff --git a/crates/engine/src/cmd/addons/document.rs b/crates/engine/src/cmd/addons/document.rs index dd20305a2d..3dcde0fd38 100644 --- a/crates/engine/src/cmd/addons/document.rs +++ b/crates/engine/src/cmd/addons/document.rs @@ -67,7 +67,7 @@ pub fn specs() -> Vec { "Flood Fill Artboards", ["Object", "Plug-ins", "Document Tools"], None, - "{artboards?: [index…] (default: the active artboard), all?: bool (true: every artboard; not with artboards), color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused; default: the current fill, in a preview the one the preview began with, an error when that is None), layer?: name (default: \"Flood Fill\"; at most 255 characters)} add an editable rectangle exactly the size of each artboard, painted with the color and no stroke, on top of the top-level layer of that name, made at the bottom of the layers when there is none. A layer that already exists keeps its lock and visibility; the rectangles are selected unless that layer is locked or hidden, and the current layer stays as it was. One undo step. Every value given is checked: null and values of the wrong type are errors. Refused while an opacity mask is edited → {ids: [rectangle id…], layer: layer id, created: bool (a new layer was made)}", + "{artboards?: [index…] (default: the active artboard), all?: bool (true: every artboard; not with artboards), color?: color (\"#rrggbb\", {c,m,y,k}, … as the paint commands take, #rrggbbaa and #rgba refused, as is a component too large to be finite; default: the current fill, in a preview the one the preview began with, an error when that is None), layer?: name (default: \"Flood Fill\"; at most 255 characters)} add an editable rectangle exactly the size of each artboard, painted with the color and no stroke, on top of the top-level layer of that name, made at the bottom of the layers when there is none. A layer that already exists keeps its lock and visibility; the rectangles are selected unless that layer is locked or hidden, and the current layer stays as it was. One undo step. Every value given is checked: null and values of the wrong type are errors. Refused while an opacity mask is edited → {ids: [rectangle id…], layer: layer id, created: bool (a new layer was made)}", super::super::maskedit::outside_mask_editing, flood_fill::flood_fill_artboards ), diff --git a/crates/engine/src/cmd/addons/document/flood_fill.rs b/crates/engine/src/cmd/addons/document/flood_fill.rs index a8214572da..b4b63cbc19 100644 --- a/crates/engine/src/cmd/addons/document/flood_fill.rs +++ b/crates/engine/src/cmd/addons/document/flood_fill.rs @@ -4,7 +4,7 @@ //! (`artboard.setProps {color}`), which is a property of the artboard. use serde_json::{Value, json}; -use vectorcraft_color::Paint; +use vectorcraft_color::{Color, Paint}; use vectorcraft_doc::{Appearance, LiveShape, Node, NodeKind}; use vectorcraft_geom::{Affine, FillRule}; @@ -41,7 +41,7 @@ pub(super) fn flood_fill_artboards(s: &mut Session, p: &Value) -> Result // rectangles may have given their color to the new-art paints since. let current = st.interaction.as_ref().map_or(&s.paint.fill, |it| &it.paint.fill); let fill = match p.get("color") { - Some(v) => Paint::solid(color_value(v).ok_or_else(|| bad(COMMAND, format!("bad color {v}")))?), + Some(v) => Paint::solid(color_value(v).filter(finite).ok_or_else(|| bad(COMMAND, format!("bad color {v}")))?), None => match current { Paint::None => return Err(bad(COMMAND, "the current fill is None: pick a fill or give `color`")), paint => paint.clone(), @@ -98,6 +98,15 @@ pub(super) fn flood_fill_artboards(s: &mut Session, p: &Value) -> Result Ok(json!({ "ids": ids.iter().map(|i| i.0).collect::>(), "layer": layer.0, "created": created })) } +/// Whether every component of `c` is finite: a component too large for `f32` reads as infinite. +fn finite(c: &Color) -> bool { + match *c { + Color::Rgb { r, g, b } | Color::Lab { l: r, a: g, b } => [r, g, b].iter().all(|v| v.is_finite()), + Color::Cmyk { c, m, y, k } => [c, m, y, k].iter().all(|v| v.is_finite()), + Color::Gray { k } => k.is_finite(), + } +} + /// The index of the view's active artboard ([`crate::DocState::active_artboard`]): when the /// document no longer has it, the artboard the document opened on, else the first. fn active_artboard(st: &crate::DocState) -> usize { diff --git a/crates/engine/src/cmd/addons/document/tests.rs b/crates/engine/src/cmd/addons/document/tests.rs index 1642a81196..f50b1f26e5 100644 --- a/crates/engine/src/cmd/addons/document/tests.rs +++ b/crates/engine/src/cmd/addons/document/tests.rs @@ -303,6 +303,14 @@ fn flood_fill_refuses_bad_params_and_a_none_fill() { assert!(s.execute(FLOOD, &p).is_err(), "{p}"); assert!(*s.doc().unwrap().doc == *before.0 && s.doc().unwrap().history.undo.len() == before.1, "{p} changed nothing"); } + // Components too large for f32 become infinite once read. + for color in [json!({"c": 1e308, "m": 0, "y": 0, "k": 0}), json!([1e39, 0, 0]), json!({"gray": 1e41}), json!({"l": 1e39, "a": 0, "b": 0})] { + let p = json!({ "color": color }); + let before = (s.doc().unwrap().doc.clone(), s.doc().unwrap().history.undo.len()); + let e = s.execute(FLOOD, &p).unwrap_err().to_string(); + assert!(e.contains("bad color"), "{p}: {e}"); + assert!(*s.doc().unwrap().doc == *before.0 && s.doc().unwrap().history.undo.len() == before.1, "{p} changed nothing"); + } s.execute("paint.setFill", &json!({"none": true})).unwrap(); let e = s.execute(FLOOD, &json!({})).unwrap_err().to_string(); assert!(e.contains("the current fill is None"), "{e}"); @@ -326,6 +334,40 @@ fn flood_fill_previews_without_color_use_the_fill_the_interaction_began_with() { assert_eq!(fill, red, "the fill the interaction began with, not the previewed color"); } +/// With New Art Has Basic Appearance off and nothing selected, a canceled preview in a color puts +/// back the new-art fill that its selected rectangles had given their color to. +#[test] +fn flood_fill_cancel_from_an_empty_selection_keeps_the_new_art_fill() { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + assert!(s.doc().unwrap().selection.is_empty()); + let red = s.paint.fill.clone(); + s.begin_interaction("Flood Fill Artboards").unwrap(); + s.preview(FLOOD, &json!({"color": "#0000ff"})).unwrap(); + assert_ne!(s.paint.fill, red, "the previewed rectangles gave their color to the new-art fill"); + s.cancel_interaction().unwrap(); + assert_eq!(s.paint.fill, red, "the cancel puts the new-art fill back"); + assert!(s.doc().unwrap().selection.is_empty()); +} + +#[test] +fn flood_fill_cancel_after_a_rejected_preview_keeps_the_new_art_fill() { + let mut s = boards(); + s.prefs.new_art_basic = false; + s.execute("select.none", &json!({})).unwrap(); + assert!(s.doc().unwrap().selection.is_empty()); + let red = s.paint.fill.clone(); + s.begin_interaction("Flood Fill Artboards").unwrap(); + s.preview(FLOOD, &json!({"color": "#0000ff"})).unwrap(); + assert_ne!(s.paint.fill, red); + assert!(s.preview(FLOOD, &json!({"color": [1e39, 0, 0]})).is_err()); + assert_eq!(s.paint.fill, red); + s.cancel_interaction().unwrap(); + assert_eq!(s.paint.fill, red); + assert!(s.doc().unwrap().selection.is_empty()); +} + /// #4920: Export All Open Documents writes an SVG of each open document; a folder that can't be /// written lists every document in `failed`, and either way the active document stays active. #[test] diff --git a/crates/engine/src/session/edit.rs b/crates/engine/src/session/edit.rs index d5d62e607c..61e62d0dd8 100644 --- a/crates/engine/src/session/edit.rs +++ b/crates/engine/src/session/edit.rs @@ -577,6 +577,7 @@ impl Session { // preview starts from the colors the interaction began with; so does a preview with // `defaultsOnly: true`, and one after it. let tool_paints = self.tool_paints(false) || self.tool_paints(true); + let inherits = !self.prefs.new_art_basic; let (paint, restore_paint) = { let menu = self.menu.clone(); let st = self.doc_mut()?; @@ -597,7 +598,8 @@ impl Session { || cmd == "command.batch" || it.used_defaults_only || it.preview.as_ref().is_some_and(|(id, p)| id == "command.batch" || cmd::asks_defaults_only(p)) - || it.color_mode_paints.is_some(); + || it.color_mode_paints.is_some() + || (inherits && it.preview.is_some() && it.selection.is_empty()); let paint = it.paint.clone(); it.preview_paint = None; it.color_mode_paints = None; @@ -767,6 +769,10 @@ impl Session { // it does a color previewed with `defaultsOnly: true`. let tool_paints = self.tool_paints(false) || self.tool_paints(true); let used_defaults_only = self.active().and_then(|st| st.interaction.as_ref()).is_some_and(|it| it.used_defaults_only); + // With New Art Has Basic Appearance off, a preview that selected new art from an empty + // selection gave that art's appearance to the new-art paints; after the undo nothing is + // selected to take them from, so the cancel puts back the paints the interaction began with. + let inherits = !self.prefs.new_art_basic; if let Some(paint) = self .active() .and_then(|st| st.interaction.as_ref()) @@ -776,6 +782,7 @@ impl Session { || it.used_defaults_only || it.preview.as_ref().is_some_and(|(id, p)| id == "command.batch" || cmd::asks_defaults_only(p)) || it.color_mode_paints.is_some() + || (inherits && it.preview.is_some() && it.selection.is_empty()) }) .map(|it| it.paint.clone()) { diff --git a/crates/ui-egui/src/addons/document.rs b/crates/ui-egui/src/addons/document.rs index 37be0c3cc8..bab56e3edb 100644 --- a/crates/ui-egui/src/addons/document.rs +++ b/crates/ui-egui/src/addons/document.rs @@ -66,21 +66,22 @@ fn flood_rows() -> Vec { ] } -/// `#rrggbb` of an sRGB color with components from 0 to 1. -fn hex([r, g, b]: [f32; 3]) -> String { - let byte = |v: f32| (v.clamp(0.0, 1.0) * 255.0).round() as u8; - format!("#{:02x}{:02x}{:02x}", byte(r), byte(g), byte(b)) +/// The working-RGB components `[r, g, b]` of the dialog's display sRGB color, as the engine reads a +/// color array. +fn working_rgb(srgb: [f32; 3]) -> Value { + let [r, g, b] = vectorcraft_color::cms::active().srgb_to_rgb(srgb.map(|v| v.clamp(0.0, 1.0))); + json!([r, g, b]) } /// Flood Fill Artboards' params from the dialog: every artboard or the one the dialog opened on (`board`, kept -/// out of sight), the color only when Color is picked, and the layer named. +/// out of sight), the color only when Color is picked (in the document's working RGB), and the layer named. fn flood_params(fields: &DialogFields) -> Value { let mut p = match fields.text("artboards") { Some("all") => json!({ "all": true }), _ => json!({ "artboards": [fields.int("board").unwrap_or(0)] }), }; if fields.text("fill") == Some("color") { - p["color"] = json!(hex(fields.color("color").unwrap_or(GRAY))); + p["color"] = working_rgb(fields.color("color").unwrap_or(GRAY)); } p["layer"] = json!(fields.text("layer").unwrap_or(FLOOD_LAYER)); p @@ -91,10 +92,7 @@ fn flood_params(fields: &DialogFields) -> Value { pub(crate) fn open_flood_fill(app: &mut VectorcraftApp) -> Result { let board = app.view().map_or(0, |v| v.artboard); let fill = &app.session.paint.fill; - let color = fill.color().map_or(GRAY, |c| { - let [r, g, b, _] = c.to_rgba8(1.0); - [r, g, b].map(|v| f32::from(v) / 255.0) - }); + let color = fill.color().map_or(GRAY, |c| c.to_rgb().map(|v| v.clamp(0.0, 1.0))); let fields = DialogFields::from_pairs([ ("artboards", FieldValue::Text("active".into())), ("fill", FieldValue::Text(if fill.is_none() { "color" } else { "current" }.into())), diff --git a/crates/ui-egui/src/addons/document/tests.rs b/crates/ui-egui/src/addons/document/tests.rs index 40a9985c38..88abf7931b 100644 --- a/crates/ui-egui/src/addons/document/tests.rs +++ b/crates/ui-egui/src/addons/document/tests.rs @@ -315,20 +315,48 @@ fn flood_fill_cancel_rolls_the_preview_back() { #[test] fn flood_fill_all_artboards_in_a_chosen_color() { - let mut app = boards(); - click(&mut app, "Flood Fill Artboards…"); - let id = host::dialog_id(&app); - host::set(&app, id, "artboards", json!("all")); - assert_eq!(host::read(&app, id, |f| f.color("color")), Some([1.0, 0.0, 0.0]), "Color opens on the current fill's color"); - host::set(&app, id, "fill", json!("color")); - host::set(&app, id, "color", json!([0.0, 0.0, 1.0])); - let params = host::read(&app, id, super::flood_params); - assert_eq!(params, json!({"all": true, "color": "#0000ff", "layer": "Flood Fill"})); - assert!(host::confirm(&app, id)); - host::frame(&mut app); - let blue = vectorcraft_color::Paint::solid(vectorcraft_color::Color::from_hex("#0000ff").unwrap()); - let artboards: Vec<_> = app.session.doc().unwrap().doc.artboards.iter().map(|a| (a.rect, blue.clone())).collect(); - assert_eq!(bottom_art(&app), artboards, "one rectangle per artboard, in the dialog's color"); + let srgb = std::sync::Arc::new(vectorcraft_color::cms::Cms::new(&Default::default()).unwrap()); + vectorcraft_color::cms::with_active(srgb, || { + let mut app = boards(); + click(&mut app, "Flood Fill Artboards…"); + let id = host::dialog_id(&app); + host::set(&app, id, "artboards", json!("all")); + assert_eq!(host::read(&app, id, |f| f.color("color")), Some([1.0, 0.0, 0.0]), "Color opens on the current fill's color"); + host::set(&app, id, "fill", json!("color")); + host::set(&app, id, "color", json!([0.0, 0.0, 1.0])); + let params = host::read(&app, id, super::flood_params); + assert_eq!(params, json!({"all": true, "color": [0.0, 0.0, 1.0], "layer": "Flood Fill"})); + assert!(host::confirm(&app, id)); + host::frame(&mut app); + let blue = vectorcraft_color::Paint::solid(vectorcraft_color::Color::from_hex("#0000ff").unwrap()); + let artboards: Vec<_> = app.session.doc().unwrap().doc.artboards.iter().map(|a| (a.rect, blue.clone())).collect(); + assert_eq!(bottom_art(&app), artboards, "one rectangle per artboard, in the dialog's color"); + }); +} + +/// In a working RGB space other than sRGB, switching from Current Fill to Color without editing the +/// color fills the artboards in the current fill's color. +#[test] +fn flood_fill_color_keeps_the_current_fill_in_a_non_srgb_working_space() { + use vectorcraft_color::{Color, cms}; + let settings = cms::ColorSettings { rgb: cms::WIDE_GAMUT_RGB.into(), ..Default::default() }; + let wide = std::sync::Arc::new(cms::Cms::new(&settings).unwrap()); + cms::with_active(wide, || { + assert!(!cms::active().rgb_is_srgb()); + let mut app = boards(); + app.run("paint.setFill", json!({"color": [0.6, 0.5, 0.4]})).unwrap(); + click(&mut app, "Flood Fill Artboards…"); + let id = host::dialog_id(&app); + host::set(&app, id, "fill", json!("color")); + assert!(host::confirm(&app, id)); + host::frame(&mut app); + let art = bottom_art(&app); + assert_eq!(art.len(), 1); + let Some(Color::Rgb { r, g, b }) = art[0].1.color() else { panic!("an RGB fill: {:?}", art[0].1) }; + for (got, want) in [(r, 0.6), (g, 0.5), (b, 0.4)] { + assert!((got - want).abs() < 1e-3, "{:?} is the current fill [0.6, 0.5, 0.4]", [r, g, b]); + } + }); } #[test] diff --git a/docs/mcp.md b/docs/mcp.md index 287e858648..c4224bdcdc 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -17514,7 +17514,7 @@ there is no step. → `{symbols: [name…], ids: [instance id…], skipped: [{la `addon.document.floodFillArtboards {artboards?, all?, color?, layer?}` adds an editable rectangle exactly the size of each artboard, painted with `color` and no stroke, as one undo step. `artboards` lists artboard indices (the active artboard by default), and `all: true` takes every artboard instead; the two can't be given together. `color` takes the -colors the paint commands take (`#rrggbbaa` and `#rgba` are refused); without it the rectangles take the current fill +colors the paint commands take (`#rrggbbaa` and `#rgba` are refused, as is a component too large to be finite); without it the rectangles take the current fill (in a preview, the fill the preview began with), and a current fill of None is an error. The rectangles go on top of the top-level layer named `layer` (`Flood Fill` by default, at most 255 characters), which is made at the bottom of the layers when the document has none. A layer that already exists keeps its lock and visibility. The rectangles are selected unless that layer is locked or hidden, and