diff --git a/crates/engine/src/cmd/recolor.rs b/crates/engine/src/cmd/recolor.rs index b1291125c8..bec6767228 100644 --- a/crates/engine/src/cmd/recolor.rs +++ b/crates/engine/src/cmd/recolor.rs @@ -1492,6 +1492,10 @@ struct ColorMap { group: Vec, keys: HashMap, hexes: HashMap, + /// The current colors of the rows whose new color was removed (`to: null`, not excluded), + /// which keep their colors. + removed_keys: std::collections::HashSet, + removed_hexes: std::collections::HashSet, /// Set when a colour took a new colour of another model in its own model. converted: std::cell::Cell, /// The Global Adjust every mapped colour takes after its row's new colour. @@ -1510,11 +1514,23 @@ impl ColorMap { group, keys: HashMap::new(), hexes: HashMap::new(), + removed_keys: Default::default(), + removed_hexes: Default::default(), converted: Default::default(), adjust, }; for (n, r) in rows.iter().enumerate().filter(|(_, r)| !r.from.is_empty()) { - let Some(to) = r.new_color() else { continue }; + let Some(to) = r.new_color() else { + if !r.exclude { + for m in &r.from { + match m { + Match::Key(k) => out.removed_keys.insert(*k), + Match::Hex(h) => out.removed_hexes.insert(h.clone()), + }; + } + } + continue; + }; let i = out.rows.len(); let colors: Vec = r.from.iter().map(Match::color).collect(); let link = links.get(n).cloned().flatten(); @@ -1555,9 +1571,18 @@ impl ColorMap { /// the row gives it the new color in that color's model and the Global Adjust gives it the /// swatch's new color at that tint; /// other rows map the colour, which loses its link when it changes or when the group's rewrite - /// would change it. True when anything changed. + /// would change it; a color of a row whose new color was removed keeps its color and loses its + /// link to a swatch of the group whose rewrite would change it. True when anything changed. fn map_link(&self, c: &mut Color, link: &mut Option, tint: &mut f32) -> bool { - let Some(i) = self.row_of(*c) else { return false }; + let Some(i) = self.row_of(*c) else { + let removed = self.removed_keys.contains(&ColorKey::of(c)) || self.removed_hexes.contains(&c.to_hex()); + let rewritten = link.as_deref().and_then(|n| self.group.iter().find(|g| g.name == n)).is_some_and(|g| g.shown.tinted(*tint) != *c); + if removed && rewritten { + *link = None; + return true; + } + return false; + }; let (Some(row), Some(l)) = (self.rows.get(i), self.links.get(i)) else { return false }; if l.is_none() && let Some(g) = link.as_deref().and_then(|n| self.group.iter().find(|g| g.name == n)) diff --git a/crates/engine/src/tests/recolor.rs b/crates/engine/src/tests/recolor.rs index a85bd921b5..e582f30353 100644 --- a/crates/engine/src/tests/recolor.rs +++ b/crates/engine/src/tests/recolor.rs @@ -804,6 +804,34 @@ fn a_null_in_group_colors_removes_that_swatch_and_the_ones_after_it_keep_their_p assert_eq!(link(&s, green), ("#00ff00".into(), Some(names[2].clone())), "art linked to a swatch that stays keeps its link"); } +/// A row whose new color was removed keeps its art's colors: art in it linked to another swatch of +/// the group, which the rewrite gives another color, loses the link instead of following it. +#[test] +fn art_of_a_row_without_a_new_color_keeps_its_color_when_its_swatch_takes_another() { + let mut s = session(); + run(&mut s, "swatch.newGroup", json!({"name": "Brand", "colors": ["#ff0000", "#0000ff", "#00ff00"]})); + let names: Vec = + s.doc().unwrap().doc.swatch_groups.iter().find(|g| g.name == "Brand").unwrap().swatches.iter().map(|w| w.name.clone()).collect(); + for name in &names { + run(&mut s, "swatch.edit", json!({"name": name, "global": true})); + } + let blue = rect(&mut s, json!("#000000")); + let green = rect(&mut s, json!("#000000")); + run(&mut s, "paint.setFill", json!({"ids": [blue.0], "swatch": names[1]})); + run(&mut s, "paint.setFill", json!({"ids": [green.0], "swatch": names[2]})); + select_all(&mut s); + // The rows are blue, red and green; the blue row's new color is removed, so the group's first + // swatch goes and its second takes the red row's new color. + let map = json!([{"from": ["#0000ff"], "to": null}, {"from": ["#ff0000"], "to": "#ff0000"}, {"from": ["#00ff00"], "to": "#00ff00"}]); + run(&mut s, "recolor.apply", json!({"map": map, "group": "Brand", "groupColors": [null, "#ff0000", "#00ff00"]})); + let link = |s: &Session, id: NodeId| match s.doc().unwrap().doc.node(id).unwrap().appearance.fill_paint() { + vectorcraft_color::Paint::Solid { color, swatch, .. } => (color.to_hex(), swatch), + p => panic!("{p:?}"), + }; + assert_eq!(link(&s, blue), ("#0000ff".into(), None), "the art keeps its color, unlinked"); + assert_eq!(link(&s, green), ("#00ff00".into(), Some(names[2].clone())), "art of another row keeps its link"); +} + #[test] fn recolor_of_everything_does_not_count_locked_art_whose_pattern_the_group_rewrite_recolors() { let mut s = session();