From a0862097c7956ed8da56756183d4a429156beb76 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 12:01:58 -0800 Subject: [PATCH] Color Wheel: color.used reads the tiles of patterns used inside pattern tiles A pattern whose tile is painted with another pattern now adds that pattern's tile colors too. Each pattern is read once, so patterns that use each other are read once each. --- crates/engine/src/cmd/recolor.rs | 15 ++++++++++----- crates/engine/src/tests/recolor.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/recolor.rs b/crates/engine/src/cmd/recolor.rs index 26680d39fc..b2c8a021a7 100644 --- a/crates/engine/src/cmd/recolor.rs +++ b/crates/engine/src/cmd/recolor.rs @@ -965,8 +965,8 @@ struct Used { } /// The unique colours of the objects `ids` and the objects inside them, most used first (ties by -/// key): fills, strokes, gradient stops, text, mesh points and the tiles of the patterns used as -/// fills or strokes. With `only`, an object not in it is left out with everything it holds. +/// key): fills, strokes, gradient stops, text, mesh points, the tiles of the patterns used as +/// fills or strokes, and the tiles of the patterns those tiles use. With `only`, an object not in it is left out with everything it holds. fn used_colors(d: &Document, ids: &[NodeId], only: Option<&BTreeSet>) -> Vec { fn read<'a>(n: &'a Node, only: Option<&BTreeSet>, out: &mut Vec<&'a Node>) { if only.is_some_and(|o| !o.contains(&n.id)) { @@ -999,7 +999,8 @@ fn used_colors(d: &Document, ids: &[NodeId], only: Option<&BTreeSet>) -> } } }; - // The patterns used as fills or strokes: their tiles count once each. + // The patterns used as fills or strokes, and the patterns their tiles use: each tile counts + // once, and a pattern whose tile uses itself, directly or through other patterns, counts once. let mut patterns: Vec<&str> = vec![]; let mut nodes = vec![]; for n in ids.iter().filter_map(|id| d.node(*id)) { @@ -1008,8 +1009,12 @@ fn used_colors(d: &Document, ids: &[NodeId], only: Option<&BTreeSet>) -> for m in nodes { paints(m, &mut count_tint, &mut patterns); } - for def in patterns.iter().filter_map(|p| d.pattern(p)) { - def.art.iter().for_each(|n| n.walk(&mut |m| paints(m, &mut count_tint, &mut vec![]))); + let mut next = 0; + while let Some(&name) = patterns.get(next) { + next += 1; + for a in d.pattern(name).map(|def| def.art.as_slice()).unwrap_or_default() { + a.walk(&mut |m| paints(m, &mut count_tint, &mut patterns)); + } } used.sort_by(|a, b| b.1.count.cmp(&a.1.count).then(a.0.cmp(&b.0))); used.into_iter().map(|(_, u)| u).collect() diff --git a/crates/engine/src/tests/recolor.rs b/crates/engine/src/tests/recolor.rs index a6e8c56b1b..0e9bf78f3d 100644 --- a/crates/engine/src/tests/recolor.rs +++ b/crates/engine/src/tests/recolor.rs @@ -204,6 +204,32 @@ fn used_colors_name_the_swatch_and_tint_a_color_is_linked_to() { assert_eq!((c["tint"].as_f64().unwrap() / 100.0) as f32, tint, "{c}"); } +#[test] +fn used_colors_read_the_tiles_of_patterns_used_inside_pattern_tiles() { + let mut s = session(); + let red = rect(&mut s, json!("#ff0000")); + run(&mut s, "select.set", json!({"ids": [red.0]})); + run(&mut s, "object.pattern.make", json!({"name": "Inner", "width": 20, "height": 20})); + run(&mut s, "object.pattern.done", json!({})); + let middle = rect(&mut s, json!("#000000")); + run(&mut s, "paint.setFill", json!({"ids": [middle.0], "swatch": "Inner"})); + run(&mut s, "select.set", json!({"ids": [middle.0]})); + run(&mut s, "object.pattern.make", json!({"name": "Outer", "width": 20, "height": 20})); + run(&mut s, "object.pattern.done", json!({})); + let square = rect(&mut s, json!("#000000")); + run(&mut s, "paint.setFill", json!({"ids": [square.0], "swatch": "Outer"})); + // The inner tile's square is stroked with the outer pattern, so the two patterns use each other. + let outer = s.doc().unwrap().doc.node(square).unwrap().appearance.fill_paint().clone(); + let st = s.doc_mut().unwrap(); + let doc = std::sync::Arc::make_mut(&mut st.doc); + let tile = doc.pattern_mut("Inner").unwrap().art.first_mut().unwrap(); + std::sync::Arc::make_mut(tile).appearance.set_stroke(outer); + run(&mut s, "select.set", json!({"ids": [square.0]})); + let r = run(&mut s, "color.used", json!({})); + let colors = r["colors"].as_array().unwrap(); + assert_eq!(colors.iter().map(|c| (c["hex"].as_str(), c["count"].as_u64())).collect::>(), [(Some("#ff0000"), Some(1))], "{r}"); +} + #[test] fn used_colors_give_no_swatch_for_a_stop_that_mixes_its_ink_with_process_inks() { use vectorcraft_color::{Gradient, GradientKind, GradientPaint, GradientStop, Paint};