Swatches: linked paints find their swatch in one name map

Add Used Colors, New Color Group from artwork and Select All Unused
looked up each linked paint's swatch by searching every swatch. They now
build one map of swatch names first, with the built-in Registration
swatch, and the first swatch of each name as before.

New tests and test cases: New Color Group from artwork with repeated,
near-equal and -0.0 colors, a Registration link and names that need
numbering, and New Swatch and Edit Color Group with near-equal and
-0.0 colors; each checks the names, their order and one undo step.
This commit is contained in:
dotCooCoo
2026-11-08 13:20:41 +07:00
committed by bflatastic
parent 346f80dcc7
commit cd95a8a6d1
4 changed files with 117 additions and 8 deletions
+1 -1
View File
@@ -238,7 +238,7 @@ impl FreeSwatchNames {
self.taken.insert(name.clone());
name
}
/// Count `name` as taken (a swatch or colour group added another way).
/// Count `name` as taken (a swatch or color group added another way).
pub fn insert(&mut self, name: &str) {
self.taken.insert(name.to_string());
}
+14 -2
View File
@@ -271,11 +271,12 @@ fn swatch_delete(s: &mut Session, p: &Value) -> Result<Value> {
/// each). A tint (a linked colour that differs from its swatch's) brings its swatch, and with
/// `tints` its own colour too. Links to missing swatches count as unlinked.
fn artwork_colors(d: &Document, ids: &[NodeId], tints: bool, swatches: &mut Vec<String>, colors: &mut Vec<Color>) {
let by_name = swatches_by_name(d);
let mut seen_swatches: HashSet<String> = swatches.iter().cloned().collect();
let mut seen_colors: HashSet<ColorKey> = colors.iter().filter_map(color_key).collect();
for n in ids.iter().filter_map(|id| d.node(*id)) {
node_colors(n, &mut |c, link| {
let sw = link.and_then(|l| d.swatch(l)).filter(|w| w.paint.color().is_some());
let sw = link.and_then(|l| by_name.get(l).copied()).filter(|w| w.paint.color().is_some());
if let Some(w) = sw
&& !seen_swatches.contains(&w.name)
{
@@ -291,6 +292,16 @@ fn artwork_colors(d: &Document, ids: &[NodeId], tints: bool, swatches: &mut Vec<
}
}
/// Every swatch by name, as [`Document::swatch`] finds it: the first swatch of each name, and the
/// built-in Registration swatch when no swatch has its name.
fn swatches_by_name(d: &Document) -> HashMap<&str, &Swatch> {
let mut by_name = HashMap::new();
for w in d.swatches_iter().chain([vectorcraft_color::swatch::registration()]) {
by_name.entry(w.name.as_str()).or_insert(w);
}
by_name
}
/// A color's model and the bits of its components, with -0.0 read as 0.0: two keys are equal
/// exactly when the colors are equal (`==`). `None` for a color with a NaN component, which
/// equals no color.
@@ -887,10 +898,11 @@ fn swatch_add_used(s: &mut Session, p: &Value) -> Result<Value> {
fn swatch_unused(s: &mut Session, _: &Value) -> Result<Value> {
let d = &s.doc()?.doc;
let by_name = swatches_by_name(d);
// Links with their tints, so a tint swatch is used where its tint of its base is.
let (mut links, mut colors, mut gradients, mut patterns, mut gradient_links) = (vec![], vec![], vec![], vec![], vec![]);
let mut color = |c: &Color, link: Option<&String>, tint: f32| match link {
Some(l) if d.swatch(l).is_some() => links.push((l.clone(), tint)),
Some(l) if by_name.contains_key(l.as_str()) => links.push((l.clone(), tint)),
_ => colors.push(*c),
};
let mut paint = |p: &Paint| match p {
+101 -4
View File
@@ -248,11 +248,40 @@ fn new_swatches_from_colors_are_numbered_after_names_already_taken() {
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "R=255 G=0 B=0 2", "color": "#0000ff"}));
run(&mut s, "swatch.newGroup", json!({"name": "Mine"}));
let before = doc(&s).clone();
let undo = undo_len(&s);
let r = run(&mut s, "swatch.new", json!({"colors": ["#ff0000", "#ff0000", "#ff0000", "#00ff00", "#00ff00"]}));
let want = ["R=255 G=0 B=0", "R=255 G=0 B=0 3", "R=255 G=0 B=0 4", "R=0 G=255 B=0", "R=0 G=255 B=0 2"];
// Near-equal colors with the same name, and a Lab color and its twin with a -0.0 component.
let colors = json!([
"#ff0000",
"#ff0000",
"#ff0000",
"#00ff00",
"#00ff00",
[0.2, 0.4, 0.6],
[0.2, 0.4, 0.6001],
{"l": 50, "a": 0.0, "b": 0},
{"l": 50, "a": -0.0, "b": 0}
]);
let r = run(&mut s, "swatch.new", json!({ "colors": colors }));
let want = [
"R=255 G=0 B=0",
"R=255 G=0 B=0 3",
"R=255 G=0 B=0 4",
"R=0 G=255 B=0",
"R=0 G=255 B=0 2",
"R=51 G=102 B=153",
"R=51 G=102 B=153 2",
"L=50 a=0 b=0",
"L=50 a=0 b=0 2",
];
assert_eq!(r, json!({"name": want[0], "names": want}));
assert_eq!(names(&s, None)[names(&s, None).len() - want.len()..], want);
assert!(matches!(doc(&s).swatch("L=50 a=0 b=0 2").unwrap().paint.color(), Some(Color::Lab { a, .. }) if a.is_sign_negative()));
assert_eq!(undo_len(&s), undo + 1);
run(&mut s, "edit.undo", json!({}));
assert_eq!(*doc(&s), before);
let r = run(&mut s, "swatch.new", json!({"colors": ["#ff0000", "#ff0000", "#ff0000"]}));
assert_eq!(r["names"], json!(["R=255 G=0 B=0", "R=255 G=0 B=0 3", "R=255 G=0 B=0 4"]));
let r = run(&mut s, "swatch.new", json!({"colors": ["#ff0000", "#ff0000"], "group": "Mine"}));
assert_eq!(r["names"], json!(["R=255 G=0 B=0 5", "R=255 G=0 B=0 6"]));
assert_eq!(names(&s, Some("Mine")), ["R=255 G=0 B=0 5", "R=255 G=0 B=0 6"]);
@@ -271,14 +300,82 @@ fn new_color_group_numbers_its_colors_after_the_group_and_its_moved_swatches() {
assert_eq!(r, json!({"name": "R=255 G=0 B=0", "swatches": want, "linked": 0}));
}
/// New Color Group from artwork lists each color once in paint order: a repeat and a twin with a
/// -0.0 component are the same color, near-equal colors with the same name are numbered after the
/// names already taken, and a paint linked to Registration brings no color. One undo step.
#[test]
fn new_color_group_from_artwork_lists_each_color_once_with_free_names() {
let mut s = session();
run(&mut s, "swatch.new", json!({"name": "R=0 G=102 B=153", "color": "#00ff00"}));
let colors = [
Color::rgb(0.2, 0.4, 0.6),
Color::rgb(0.0, 0.4, 0.6),
Color::rgb(0.2, 0.4, 0.6001),
Color::rgb(-0.0, 0.4, 0.6),
Color::rgb(0.2, 0.4, 0.6),
Color::lab(50.0, 0.0, 0.0),
Color::lab(50.0, -0.0, 0.0),
Color::lab(50.0, 0.0001, 0.0),
];
let mut ids: Vec<u64> = vec![];
for c in colors {
let id = rect_filled(&mut s, json!({"color": "#000000"}));
set_fill(&mut s, id, Paint::solid(c));
ids.push(id.0);
}
let registration = rect_filled(&mut s, json!({"color": "#000000"}));
let reg_paint = Paint::Solid { color: Color::cmyk(1.0, 1.0, 1.0, 1.0), swatch: Some(vectorcraft_color::swatch::REGISTRATION.into()), tint: 1.0 };
set_fill(&mut s, registration, reg_paint);
ids.push(registration.0);
run(&mut s, "select.set", json!({ "ids": ids }));
let before = doc(&s).clone();
let undo = undo_len(&s);
let r = run(&mut s, "swatch.newGroup", json!({"name": "R=51 G=102 B=153 2", "fromArtwork": true}));
let want = ["R=51 G=102 B=153", "R=0 G=102 B=153 2", "R=51 G=102 B=153 3", "L=50 a=0 b=0", "L=50 a=0 b=0 2"];
assert_eq!(r, json!({"name": "R=51 G=102 B=153 2", "swatches": want, "linked": 8}));
assert_eq!(names(&s, Some("R=51 G=102 B=153 2")), want);
assert_eq!(undo_len(&s), undo + 1);
run(&mut s, "edit.undo", json!({}));
assert_eq!(*doc(&s), before);
}
#[test]
fn edit_color_group_numbers_extra_colors_after_the_names_it_keeps() {
let mut s = session();
run(&mut s, "swatch.newGroup", json!({"name": "Mine", "colors": ["#ff0000", "#00ff00", "#0000ff"]}));
let colors = ["#0000ff", "#ff0000", "#ff0000", "#ff0000", "#ff0000", "#00ff00"];
let before = doc(&s).clone();
let undo = undo_len(&s);
// Near-equal colors with the same name, and a Lab color and its twin with a -0.0 component.
let colors = json!([
"#0000ff",
"#ff0000",
"#ff0000",
"#ff0000",
"#ff0000",
"#00ff00",
[0.2, 0.4, 0.6],
[0.2, 0.4, 0.6001],
{"l": 50, "a": 0.0, "b": 0},
{"l": 50, "a": -0.0, "b": 0}
]);
let r = run(&mut s, "swatch.editGroup", json!({"group": "Mine", "colors": colors, "rename": "R=255 G=0 B=0 2"}));
let want = ["R=255 G=0 B=0", "R=0 G=255 B=0", "R=0 G=0 B=255", "R=255 G=0 B=0 3", "R=255 G=0 B=0 4", "R=0 G=255 B=0 2"];
let want = [
"R=255 G=0 B=0",
"R=0 G=255 B=0",
"R=0 G=0 B=255",
"R=255 G=0 B=0 3",
"R=255 G=0 B=0 4",
"R=0 G=255 B=0 2",
"R=51 G=102 B=153",
"R=51 G=102 B=153 2",
"L=50 a=0 b=0",
"L=50 a=0 b=0 2",
];
assert_eq!(r, json!({"name": "R=255 G=0 B=0 2", "swatches": want, "relinked": 0}));
assert_eq!(names(&s, Some("R=255 G=0 B=0 2")), want);
assert_eq!(undo_len(&s), undo + 1);
run(&mut s, "edit.undo", json!({}));
assert_eq!(*doc(&s), before);
}
#[test]
+1 -1
View File
@@ -1119,7 +1119,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
- **CSS swatch libraries:** fractional linear and radial gradient stop positions are preserved, so nearby stops such as 12.5% and 12.890625% remain distinct instead of collapsing to 13%. Reference: [CSS Images 3 color stops](https://www.w3.org/TR/css-images-3/#color-stop-syntax). The original synthetic regression is `palette_io::tests::css_preserves_fractional_gradient_stop_positions` in `crates/color/src/palette_io.rs`. Scores and hours are unchanged.
- **Swatches:** names follow the colour model and are unique; Swatch Options (process/spot, Global, Gray/RGB/HSB/CMYK/Web, live preview); editing a global or spot swatch recolours every linked fill, stroke and text run in one undo step; multi-select and delete with confirmation; New Swatch and New Color Group dialogs (a group from the selected artwork's colours); swatches in colour groups convert, separate and count like any other. The Swatches panel selects ranges and whole groups, has a find field, and drags swatches to reorder, regroup or paint art; Add Used Colors, Select All Unused, Merge, Ungroup and Sort by Kind; a CMYK document keeps applied colours in CMYK. Swatch libraries: nine generated colour libraries, five gradient libraries and a pattern library (Geometric Patterns, whose swatches bring their patterns) in a library panel, saved as `.vcswatches`, `.gpl`, swatch exchange (`.ase`) or CSS, and loaded from `.vcswatches`, `.gpl` and `.ase` files (`.ase` both ways: RGB, CMYK, Lab and Gray; global, spot and process; groups) and from color books (`.acb`, read only: the RGB, CMYK or Lab colors of a book such as a Pantone book a user owns, named with its prefix and suffix, spot unless it says process, #831); `swatch.library.copyToUser` copies a loaded library into the User Defined folder without replacing a file, and a file with the same extension and bytes as a User Defined library opens as that library. Tints of global and spot colours ("Name 40%") stay linked and separate as a percentage of their plate. Spot colours can be defined in Lab (Spot Colors options). A built-in [Registration] swatch prints on every plate.
Editing, renaming, deleting or merging a global swatch also changes the colors linked to it in graph designs, the art of brushes and the tile that pattern editing's Cancel puts back, in the same undo step as the rest of the art; Default Swatches with replace unlinks them there too.
Add Used Colors (`swatch.addUsedColors`) and Select All Unused (`swatch.unused`) look colors, swatch names, links, tints and gradients up in hash sets instead of searching every swatch and color for each color, and New Swatch with several colors (`swatch.new`), New Color Group (`swatch.newGroup`) and Edit Color Group (`swatch.editGroup`) name their new swatches from one set of the names already taken. The new swatches keep their names, order and links, and Select All Unused lists the same swatches. Evidence: `add_used_colors_adds_each_new_color_once_with_free_names`, `add_used_colors_global_links_every_paint_of_a_new_color`, `unused_matches_links_tints_colors_gradients_and_patterns_exactly`, `new_swatches_from_colors_are_numbered_after_names_already_taken`, `new_color_group_numbers_its_colors_after_the_group_and_its_moved_swatches` and `edit_color_group_numbers_extra_colors_after_the_names_it_keeps` in `crates/engine/src/tests/swatchcmds.rs`; `free_swatch_names_match_naming_each_swatch_as_it_is_added` and `free_swatch_names_match_new_swatch_names_as_swatches_are_added` in `crates/doc/src/swatches.rs`.
Add Used Colors (`swatch.addUsedColors`) and Select All Unused (`swatch.unused`) look colors, swatch names, links, tints and gradients up in hash sets instead of searching every swatch and color for each color or linked paint (New Color Group from artwork too), and New Swatch with several colors (`swatch.new`), New Color Group (`swatch.newGroup`) and Edit Color Group (`swatch.editGroup`) name their new swatches from one set of the names already taken. The new swatches keep their names, order and links, and Select All Unused lists the same swatches. Evidence: `add_used_colors_adds_each_new_color_once_with_free_names`, `add_used_colors_global_links_every_paint_of_a_new_color`, `unused_matches_links_tints_colors_gradients_and_patterns_exactly`, `new_swatches_from_colors_are_numbered_after_names_already_taken`, `new_color_group_numbers_its_colors_after_the_group_and_its_moved_swatches`, `new_color_group_from_artwork_lists_each_color_once_with_free_names` and `edit_color_group_numbers_extra_colors_after_the_names_it_keeps` in `crates/engine/src/tests/swatchcmds.rs`; `free_swatch_names_match_naming_each_swatch_as_it_is_added` and `free_swatch_names_match_new_swatch_names_as_swatches_are_added` in `crates/doc/src/swatches.rs`.
- **Colorized images (#1466):** a fill color applied to a selected grayscale or 1-bit image (`paint.setFill`) colorizes it with black, white, a process color or a spot color, and refuses a process color when the image has an alpha channel ([Convert color modes](https://web.archive.org/web/20251220223850/https://helpx.adobe.com/illustrator/desktop/manage-colors/select-and-adjust-colors/convert-color-modes.html), "Colorize Grayscale or 1-bit images"); black and white are accepted there too. The canvas draws each pixel in the color as far as the pixel is dark, with white pixels transparent; PDF export writes the image in the color (a spot color as its ink, Registration in every ink) with the image as its soft mask; Separations Preview and printed separations show it on its color's plates; native files keep the color. SVG writes the fill color through a coverage mask (or a colorized PNG in Tiny 1.2); EPS writes binary coverage as an image mask in the color's ink, and a grayscale image as ink samples scaled by coverage, with white pixels left out at Level 3 and gray midtones written over white.
The prompt for deleting several swatches and groups uses the interface language's plural form for the count. Evidence: `deleting_several_swatches_uses_each_count_form` in `crates/ui-egui/src/panels/swatches/tests.rs`.
- **Colorized images (#1466):** a fill color applied to a selected grayscale or 1-bit image (`paint.setFill`) colorizes it with black, white, a process color or a spot color, and refuses a process color when the image has an alpha channel ([Convert color modes](https://web.archive.org/web/20251220223850/https://helpx.adobe.com/illustrator/desktop/manage-colors/select-and-adjust-colors/convert-color-modes.html), "Colorize Grayscale or 1-bit images"); black and white are accepted there too. The canvas draws each pixel in the color as far as the pixel is dark, with white pixels transparent; PDF export writes the image in the color (a spot color as its ink, Registration in every ink) with the image as its soft mask; Separations Preview and printed separations show it on its color's plates; native files keep the color. EPS and SVG write the image's own pixels.