From 8bc2d29d45f858bcdc7a073acecaad6e37711eb7 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 16 Nov 2026 00:21:37 -0800 Subject: [PATCH] Recolor Artwork: Save Changes keeps colors added and removed on the Edit tab Save Changes now gives the group the same colors OK would: a color removed with the Remove Color tool removes its swatch, and a color added with the Add Color tool joins the group after its other colors, also when the art holds colors of the group. `swatch.editGroup` takes `null` in `colors` to remove the swatch at that place. After the save, the rows follow the saved group, so a later edit or OK does not remove or add a color a second time. --- crates/engine/src/cmd/swatch.rs | 21 ++- crates/engine/src/tests/swatchcmds.rs | 14 ++ crates/ui-egui/src/dialogs/recolor.rs | 183 ++++++++++++++++++++++++-- docs/mcp.md | 7 +- 4 files changed, 206 insertions(+), 19 deletions(-) diff --git a/crates/engine/src/cmd/swatch.rs b/crates/engine/src/cmd/swatch.rs index 6f2137d6d6..088c7d74c5 100644 --- a/crates/engine/src/cmd/swatch.rs +++ b/crates/engine/src/cmd/swatch.rs @@ -129,7 +129,7 @@ pub fn specs() -> Vec { "Edit Color Group", ["Window", "Swatches"], None, - "{group: colour group, colors: [colour] (hex, key, {c,m,y,k} or {l,a,b}; #rrggbbaa and #rgba are refused; the group's swatches take them in order, extra colours become new swatches, swatches past the end are removed; built-in swatches can't change; a color the swatch already has keeps its model), keepModel?: bool (as paint.setFill), rename?: new group name} rewrite a colour group as one undo step; art linked to its global swatches takes their new colours (and keeps its colour, unlinked, when its swatch goes) → {name, swatches: [names], relinked}", + "{group: colour group, colors: [colour] (hex, key, {c,m,y,k} or {l,a,b}; #rrggbbaa and #rgba are refused; the group's swatches take them in order, extra colours become new swatches, swatches past the end are removed; a null removes the swatch at that place; built-in swatches can't change; a color the swatch already has keeps its model), keepModel?: bool (as paint.setFill), rename?: new group name} rewrite a colour group as one undo step; art linked to its global swatches takes their new colours (and keeps its colour, unlinked, when its swatch goes) → {name, swatches: [names], relinked}", has_doc, swatch_edit_group ), @@ -1317,29 +1317,36 @@ pub(super) fn rewrite_group(d: &mut Document, group: &str, colors: &[Option Result { const C: &str = "swatch.editGroup"; let group = str_param(p, "group").ok_or_else(|| bad(C, "missing `group`"))?; - let colors: Vec = p + // A null removes the swatch at that place. + let colors: Vec> = p .get("colors") .and_then(Value::as_array) .ok_or_else(|| bad(C, "missing `colors`"))? .iter() - .map(|v| color_value(v).ok_or_else(|| bad(C, format!("bad colour {v}")))) + .map(|v| if v.is_null() { Ok(None) } else { color_value(v).map(Some).ok_or_else(|| bad(C, format!("bad colour {v}"))) }) .collect::>()?; super::paint::check_keep_model(p, C)?; // A color its swatch already has keeps its model. The others take the document's. let old: Vec> = s.doc()?.doc.swatch_groups.iter().find(|g| g.name == group).map(|g| g.swatches.iter().map(|w| w.paint.color()).collect()).unwrap_or_default(); - let colors: Vec = - colors.into_iter().enumerate().map(|(i, c)| if old.get(i) == Some(&Some(c)) { c } else { super::paint::new_color(s, p, c) }).collect(); + let mut colors: Vec> = colors + .into_iter() + .enumerate() + .map(|(i, c)| c.map(|c| if old.get(i) == Some(&Some(c)) { c } else { super::paint::new_color(s, p, c) })) + .collect(); + // A null past the group's swatches removes nothing. + while colors.len() > old.len() && colors.last() == Some(&None) { + colors.pop(); + } let rename = str_param(p, "rename"); // The group's own colors, in order, under its own name change nothing: no undo step. if let Some(g) = s.doc()?.doc.swatch_groups.iter().find(|g| g.name == group) && rename.map(str::trim).is_none_or(|n| n.is_empty() || n == group) && old.len() == colors.len() - && old.iter().zip(&colors).all(|(o, c)| *o == Some(*c)) + && old.iter().zip(&colors).all(|(o, c)| c.is_some() && o == c) { return Ok(json!({"name": group, "swatches": g.swatches.iter().map(|w| w.name.clone()).collect::>(), "relinked": 0})); } - let colors: Vec> = colors.into_iter().map(Some).collect(); let e = s.edit("Edit Color Group", |d, _| rewrite_group(d, group, &colors, rename, C))?; e.defaults(s)?; Ok(json!({"name": e.name, "swatches": e.swatches, "relinked": e.relinked})) diff --git a/crates/engine/src/tests/swatchcmds.rs b/crates/engine/src/tests/swatchcmds.rs index 88276a9e31..03ab5fdbe2 100644 --- a/crates/engine/src/tests/swatchcmds.rs +++ b/crates/engine/src/tests/swatchcmds.rs @@ -445,6 +445,20 @@ fn edit_color_group_with_its_own_colors_records_no_undo_step() { assert_eq!(undo_len(&s), undo + 2, "a new name is a step"); } +#[test] +fn edit_color_group_with_a_null_removes_that_swatch() { + let mut s = session(); + run(&mut s, "swatch.newGroup", json!({"name": "Mine", "colors": ["#ff0000", "#00ff00", "#0000ff"], "keepModel": true})); + let undo = undo_len(&s); + let r = run(&mut s, "swatch.editGroup", json!({"group": "Mine", "colors": ["#ff0000", null, "#0000ff"]})); + assert_eq!(r["swatches"], json!(["R=255 G=0 B=0", "R=0 G=0 B=255"])); + assert_eq!(names(&s, Some("Mine")), ["R=255 G=0 B=0", "R=0 G=0 B=255"]); + assert_eq!(undo_len(&s), undo + 1, "a removed swatch is a step"); + run(&mut s, "swatch.editGroup", json!({"group": "Mine", "colors": ["#ff0000", "#0000ff", null]})); + assert_eq!(undo_len(&s), undo + 1, "a null past the swatches removes nothing and records no step"); + assert!(s.execute("swatch.editGroup", &json!({"group": "Mine", "colors": ["#ff0000", "nope"]})).is_err()); +} + #[test] fn unused_leaves_out_linked_colours_gradients_and_patterns_in_use() { let mut s = session(); diff --git a/crates/ui-egui/src/dialogs/recolor.rs b/crates/ui-egui/src/dialogs/recolor.rs index cd3c41206d..42e42eb768 100644 --- a/crates/ui-egui/src/dialogs/recolor.rs +++ b/crates/ui-egui/src/dialogs/recolor.rs @@ -570,16 +570,18 @@ fn group_action(app: &mut VectorcraftApp, d: &mut Dialog, action: &Value) -> Res Some("save") => { let rs = rows(d); let p = apply_params(d); - // With art, the group's colors no row took follow, adjusted and snapped as OK gives them; a - // row made with New Row that holds no current colors gives the group no color. - let filled: Vec<&Value> = rs.iter().filter(|r| !r["from"].as_array().is_some_and(Vec::is_empty)).collect(); + // With art, the group's colors no row took follow, adjusted and snapped as OK gives them, + // then the colors added on the Edit tab; a row made with New Row that holds no current + // colors gives the group no color. + let filled: Vec<&Value> = rs.iter().filter(|r| !no_current(r)).collect(); let rest: Vec = p["groupColors"].as_array().map(|a| a.iter().skip(filled.len()).map(|k| json!({"from": [k], "to": k})).collect()).unwrap_or_default(); - let mut colors = group_colors(app, d, filled.into_iter().chain(&rest)); + let mut colors: Vec = filled.iter().filter_map(|r| saved_color(app, d, r)).chain(group_colors(app, d, rest.iter())).collect(); cancel_preview(app, d); // With art in a color of the group, the group keeps its swatch order: a swatch whose color // a row lists takes that row's new color (its own color when the row is excluded), so the // art linked to it takes the color its row shows, and every other swatch keeps its color. + // The colors added on the Edit tab follow. let own: Vec = doc_groups(app) .into_iter() .find(|(n, _)| group.as_deref() == Some(n.as_str())) @@ -587,12 +589,14 @@ fn group_action(app: &mut VectorcraftApp, d: &mut Dialog, action: &Value) -> Res .unwrap_or_default(); let row_of = |k: &Value| row_listing(&rs, k); if d.bool("__art") && own.iter().any(|k| row_of(k).is_some()) { + let added = rs.iter().filter(|r| no_current(r) && is_added(r) && !excluded(r) && !r["to"].is_null()); colors = own .iter() .map(|k| match row_of(k) { - Some(r) if !excluded(r) => group_colors(app, d, std::iter::once(r)).into_iter().next().unwrap_or_else(|| k.clone()), + Some(r) if !excluded(r) => saved_color(app, d, r).unwrap_or_else(|| k.clone()), _ => k.clone(), }) + .chain(group_colors(app, d, added)) .collect(); } let before = art_colors(app); @@ -601,7 +605,38 @@ fn group_action(app: &mut VectorcraftApp, d: &mut Dialog, action: &Value) -> Res d.fields.insert("group".into(), name.clone()); d.fields.insert(GROUP_LOADED.into(), name.clone()); d.fields.insert("groupName".into(), name); - if d.bool("__art") { + // The rows follow the saved group, so a later edit or OK does not remove or add a color + // again: a removed color's row goes (its art keeps its colors). Without art, a color added + // on the Edit tab becomes a row of its own color, after the rows of the group's other + // colors; with art, its row goes and the color is one of the group's colors no row takes. + let art = d.bool("__art"); + let joins = |r: &Value| no_current(r) && is_added(r) && !excluded(r) && !r["to"].is_null(); + let gone: Vec = + rows(d).iter().filter(|r| color_removed(r)).flat_map(|r| r["from"].as_array().cloned().unwrap_or_default()).collect(); + let (mut joined, mut rs): (Vec, Vec) = rows(d).into_iter().filter(|r| !color_removed(r)).partition(|r| joins(r)); + if art { + joined.clear(); + } + for r in &mut joined { + r["from"] = json!([r["to"].clone()]); + if let Some(o) = r.as_object_mut() { + o.remove("added"); + } + } + // The save added them after the group's other colors, whatever the rows' order. + rs.extend(joined); + if rs != rows(d) { + d.fields.insert("__sel".into(), json!([])); + } + d.fields.insert("rows".into(), Value::Array(rs)); + if art { + // The group's colors no row takes are those of the saved group. + let saved: Vec = doc_groups(app) + .into_iter() + .find(|(n, _)| Some(n.as_str()) == r["name"].as_str()) + .map(|g| g.1.iter().map(key).collect()) + .unwrap_or_default(); + d.fields.insert("__assign".into(), Value::Array(saved)); // The saved group holds the Global Adjust: the rows' new colors take it, and it goes // back to 0%, so neither the preview nor OK applies it again. bake_adjust(app, d); @@ -609,7 +644,7 @@ fn group_action(app: &mut VectorcraftApp, d: &mut Dialog, action: &Value) -> Res if art_colors(app) == before { mark_saved(app, d); } else { - reread_rows(app, d, &rs, &before); + reread_rows(app, d, &rs, &before, &gone); } } Ok(()) @@ -624,14 +659,21 @@ fn group_action(app: &mut VectorcraftApp, d: &mut Dialog, action: &Value) -> Res } } +/// The color Save Changes gives the group's swatch for `row`: `null` when the row's color was +/// removed, which removes the swatch as OK does, otherwise its new color as OK gives it. +fn saved_color(app: &VectorcraftApp, d: &Dialog, row: &Value) -> Option { + if color_removed(row) { Some(Value::Null) } else { group_colors(app, d, std::iter::once(row)).into_iter().next() } +} + /// After Save Changes recolored the art linked to the group's global swatches, read the rows from /// the art again. A row that lists a color the save gave the art (a paint of a selected object /// that differs from `before`, [`art_colors`] before the save) is excluded with its first color, /// so the art keeps it, also when other art has that color: `recolor.apply` recolors every use /// of a color alike, so the row cannot keep that art's earlier new color without undoing the save. Any other row whose first current color one of the `old` rows listed takes /// that row's new color and exclusion (art the save left as it was keeps its recolor); the rest -/// are excluded too. -fn reread_rows(app: &mut VectorcraftApp, d: &mut Dialog, old: &[Value], before: &[Vec]) { +/// are excluded too. A row that lists only `gone` colors (those of swatches the save removed) is +/// left out: its art keeps its colors, and the row gives the group no color. +fn reread_rows(app: &mut VectorcraftApp, d: &mut Dialog, old: &[Value], before: &[Vec], gone: &[Value]) { let given: Vec = art_colors(app).iter().zip(before).flat_map(|(now, was)| now.iter().zip(was).filter(|(n, w)| n != w).map(|(n, _)| n.clone())).collect(); d.fields.insert(THEME_PICKS.into(), json!([])); @@ -652,6 +694,7 @@ fn reread_rows(app: &mut VectorcraftApp, d: &mut Dialog, old: &[Value], before: } } } + rs.retain(|r| r["from"].as_array().is_none_or(|f| f.is_empty() || !f.iter().all(|c| gone.contains(c)))); d.fields.insert("rows".into(), Value::Array(rs)); mark_saved(app, d); } @@ -979,6 +1022,12 @@ fn no_current(row: &Value) -> bool { row["from"].as_array().is_none_or(Vec::is_empty) } +/// Whether `row` holds current colors and its new color was removed (`to: null`) and isn't excluded: +/// its swatch in the color group is removed. +fn color_removed(row: &Value) -> bool { + !excluded(row) && !no_current(row) && row["to"].is_null() +} + /// Whether `row` is a color added on the Edit tab (`added`), which joins the color group. fn is_added(row: &Value) -> bool { row["added"].as_bool().unwrap_or(false) @@ -3506,6 +3555,94 @@ mod tests { assert_eq!(fill_hex(&app, red), "#ff0000", "art linked to a swatch no row lists is left as it is"); } + /// Save Changes with art in colors of the group keeps the Edit tab's changes: a removed color's + /// swatch is removed, and a color added on the Edit tab joins the group after the others. + #[test] + fn saving_a_group_keeps_added_and_removed_colors() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 300, "height": 100})).unwrap(); + let red = square(&mut app, 10.0, "#ff0000"); + let blue = square(&mut app, 40.0, "#0000ff"); + let yellow = square(&mut app, 70.0, "#ffff00"); + app.run("select.set", json!({"ids": [red, blue, yellow]})).unwrap(); + app.run("swatch.newGroup", json!({"name": "Linked", "fromArtwork": true, "toGlobal": true})).unwrap(); + let order = group_hex(&app, "Linked").unwrap(); + app.run("ui.recolorDialog", json!({"group": "Linked"})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("method".into(), json!(Method::Exact.id())); + crate::theme::install_fonts(&ctx); + frame(&mut app, &ctx, 0.0, vec![], 0); + let d = app.ui.dialog.as_mut().unwrap(); + let mut rs = super::rows(d); + for r in &mut rs { + let to = match from_colors(r).first().map(|c| c.to_hex()).as_deref() { + Some("#0000ff") => Value::Null, + _ => r["from"][0].clone(), + }; + r["to"] = to; + } + rs.push(json!({"from": [], "to": "rgb 0 128 0", "added": true})); + d.fields.insert("rows".into(), Value::Array(rs)); + act(&mut app, &ctx, 0.1, "save").unwrap(); + let mut want: Vec = order.into_iter().filter(|h| h != "#0000ff").collect(); + want.push("#008000".into()); + assert_eq!(group_hex(&app, "Linked"), Some(want.clone()), "the removed swatch goes and the added color follows the others"); + assert_eq!(fill_hex(&app, blue), "#0000ff", "art linked to a removed swatch keeps its color"); + let rs = super::rows(app.ui.dialog.as_ref().unwrap()); + assert!(rs.iter().all(|r| !r["to"].is_null() && !is_added(r)), "the rows follow the saved group: {rs:?}"); + let assign = app.ui.dialog.as_ref().unwrap().fields["__assign"].clone(); + let assign: Vec = assign.as_array().unwrap().iter().filter_map(color_value).map(|c| c.to_hex()).collect(); + assert_eq!(Some(assign), group_hex(&app, "Linked"), "a later edit gives the group no removed swatch back"); + crate::dialogs::confirm(&mut app).unwrap(); + assert_eq!(group_hex(&app, "Linked"), Some(want), "OK removes and adds nothing more"); + } + + /// Save Changes that recolors linked art reads the rows from the art again; a color added on the + /// Edit tab stays one of the group's colors and a removed one does not come back, so OK after a + /// further edit keeps them as saved. + #[test] + fn saving_a_group_that_reads_the_rows_again_keeps_an_added_color() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + app.run("file.new", json!({"width": 300, "height": 100})).unwrap(); + let red = square(&mut app, 10.0, "#ff0000"); + let blue = square(&mut app, 40.0, "#0000ff"); + let lime = square(&mut app, 70.0, "#00ff00"); + app.run("select.set", json!({"ids": [red, blue, lime]})).unwrap(); + app.run("swatch.newGroup", json!({"name": "Linked", "fromArtwork": true, "toGlobal": true})).unwrap(); + app.run("ui.recolorDialog", json!({"group": "Linked"})).unwrap(); + app.ui.dialog.as_mut().unwrap().fields.insert("method".into(), json!(Method::Exact.id())); + crate::theme::install_fonts(&ctx); + frame(&mut app, &ctx, 0.0, vec![], 0); + let d = app.ui.dialog.as_mut().unwrap(); + let mut rs = super::rows(d); + for r in &mut rs { + let to = match from_colors(r).first().map(|c| c.to_hex()).as_deref() { + Some("#ff0000") => json!("rgb 0 128 0"), + Some("#00ff00") => Value::Null, + _ => r["from"][0].clone(), + }; + r["to"] = to; + } + rs.push(json!({"from": [], "to": "rgb 255 255 0", "added": true})); + d.fields.insert("rows".into(), Value::Array(rs)); + act(&mut app, &ctx, 0.1, "save").unwrap(); + assert_eq!(fill_hex(&app, red), "#008000", "the save recolored the linked art"); + assert_eq!(fill_hex(&app, lime), "#00ff00", "art linked to the removed swatch keeps its color"); + let group = group_hex(&app, "Linked").unwrap(); + assert!(group.contains(&"#ffff00".to_string()) && !group.contains(&"#00ff00".to_string()), "{group:?}"); + let mut d = app.ui.dialog.clone().unwrap(); + let mut rs = super::rows(&d); + assert!(rs.iter().all(|r| !is_added(r)), "{rs:?}"); + if let Some(r) = rs.last_mut() { + r["to"] = json!("rgb 10 10 10"); + } + d.fields.insert("rows".into(), Value::Array(rs)); + let colors: Vec = apply_params(&d)["groupColors"].as_array().unwrap().iter().filter_map(color_value).map(|c| c.to_hex()).collect(); + assert_eq!(colors.iter().filter(|c| *c == "#ffff00").count(), 1, "OK after a further edit keeps the added color once: {colors:?}"); + assert!(!colors.contains(&"#00ff00".to_string()), "nor brings the removed color back: {colors:?}"); + } + /// Save Changes where the rows list their current colors as "#rrggbb": a row matches the swatch /// of the same color, so the swatches keep their order and the one a row lists takes its new /// color; read again afterwards, a row whose old entry was a hex keeps its new color. @@ -3627,6 +3764,34 @@ mod tests { assert_eq!(group_hex(&app, "Plain").unwrap(), saved, "OK does not darken the group again"); } + /// Without art, Save Changes removes a removed color's swatch and adds a color added on the Edit + /// tab, and OK afterwards gives the group the same colors: it removes and adds nothing more. + #[test] + fn saving_a_group_without_art_with_added_and_removed_colors_keeps_them_at_ok() { + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + let ctx = egui::Context::default(); + crate::theme::install_fonts(&ctx); + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + app.run("swatch.newGroup", json!({"name": "Plain", "colors": ["#ff0000", "#00ff00", "#0000ff"], "keepModel": true})).unwrap(); + app.run("ui.recolorDialog", json!({"group": "Plain"})).unwrap(); + frame(&mut app, &ctx, 0.0, vec![], 0); + let d = app.ui.dialog.as_mut().unwrap(); + let mut rs = super::rows(d); + for r in &mut rs { + if from_colors(r).first().map(|c| c.to_hex()).as_deref() == Some("#00ff00") { + r["to"] = Value::Null; + } + } + // The added color's row was moved ahead of the others. + rs.insert(0, json!({"from": [], "to": "rgb 255 255 0", "added": true})); + d.fields.insert("rows".into(), Value::Array(rs)); + act(&mut app, &ctx, 0.1, "save").unwrap(); + let want: Vec = ["#ff0000", "#0000ff", "#ffff00"].map(String::from).to_vec(); + assert_eq!(group_hex(&app, "Plain"), Some(want.clone()), "the save removes green and adds yellow"); + crate::dialogs::confirm(&mut app).unwrap(); + assert_eq!(group_hex(&app, "Plain"), Some(want), "OK gives the group the same colors"); + } + /// With art: a group picked in the list reduces the art to a row per color of the group, which /// become the rows' new colors, and the art previews them; the list draws beside the tabs and /// hides with `groupStorage`. diff --git a/docs/mcp.md b/docs/mcp.md index 3d884d5c66..754de2aea3 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -4685,8 +4685,8 @@ linked moves and the Color Theme Picker leave its new color as it is, and Save A from colors alone leave its new color out. With `limitTo`, a row's `tint` (a percentage, default 100) gives the art linked to the global or spot swatch its new color is that share of its tint; `tint` must be a number. `recolor.randomize` moves a row's `tint` with its new color and removes it from a color it changes, and moves a row's `model` with its new color. `swatch.editGroup {group, colors, rename?}` rewrites a colour group in one undo -step (art linked to its global swatches follows); given the group's own colors in order and no new name, it records no -undo step. And `recolor.apply` with `group` recolours the art and rewrites the +step (art linked to its global swatches follows); a `null` in `colors` removes the group's swatch at that place, and +given the group's own colors in order and no new name, it records no undo step. And `recolor.apply` with `group` recolours the art and rewrites the group together. A `null` in `groupColors` removes the group's swatch at that place, and the swatches after it keep their colors and names; art linked to a removed global swatch keeps its color and is no longer linked to it. Art linked to one of the group's global swatches stays linked when the swatch is rewritten with its row's new color (the default without `groupColors` when the rows follow the group's order), so a later `swatch.edit` still @@ -4767,7 +4767,8 @@ dialog runs it (OK with it set in the same request runs it first): and `delete` (Delete) runs `swatch.delete` on the selected group. Each is its own undo step, and the art keeps its preview. When the art has colors of the group, `save` keeps the group's swatches in order: a swatch whose color a row lists takes that row's new color (its own color when the row is excluded), so art linked to the swatch takes the color -its row shows, and every other swatch keeps its color. After a `save` with art, the rows' new colors take the Global +its row shows, and every other swatch keeps its color. A row whose color was removed (`to: null`) and is not excluded +removes its swatch, and the colors added on the Edit tab follow the group's other colors, as at OK. After a `save` with art, the rows' new colors take the Global Adjust and it goes back to 0%; when the save recolored linked art, the rows are read from the art again and a row with a color the save gave the art is excluded, also when other art has that color, since a row recolors every use of its colors alike. Until the rows or the Global Adjust change, OK changes the saved group only by the name