mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 11:05:03 +00:00
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.
This commit is contained in:
@@ -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<NodeId>>) -> Vec<Used> {
|
||||
fn read<'a>(n: &'a Node, only: Option<&BTreeSet<NodeId>>, 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<NodeId>>) ->
|
||||
}
|
||||
}
|
||||
};
|
||||
// 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<NodeId>>) ->
|
||||
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()
|
||||
|
||||
@@ -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::<Vec<_>>(), [(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};
|
||||
|
||||
Reference in New Issue
Block a user