diff --git a/crates/doc/src/swatches.rs b/crates/doc/src/swatches.rs index e4691d46c..56d892271 100644 --- a/crates/doc/src/swatches.rs +++ b/crates/doc/src/swatches.rs @@ -212,27 +212,45 @@ pub fn color_name(c: Color) -> String { #[derive(Clone, Debug)] pub struct FreeSwatchNames { taken: HashSet, - /// For each base already numbered, the number its next numbered name is looked for from: - /// names are only ever added, so the numbers below it stay taken. - next: HashMap, + /// For each base and first number already numbered, the number its next numbered name is + /// looked for from: names are only ever added, so the numbers below it stay taken. + next: HashMap<(String, usize), usize>, } impl FreeSwatchNames { /// `base`, or the first of "base 2", "base 3"… not taken, as [`Document::free_swatch_name`] /// gives it; the name is taken from then on. pub fn take(&mut self, base: &str) -> String { - let name = if self.taken.contains(base) { - let from = self.next.get(base).copied().unwrap_or(2); - let (i, name) = - (from..).map(|i| (i, format!("{base} {i}"))).find(|(_, n)| !self.taken.contains(n)).unwrap_or_else(|| (from, base.to_string())); - self.next.insert(base.to_string(), i + 1); - name - } else { - base.to_string() - }; + let name = if self.taken.contains(base) { self.numbered(base, 2) } else { base.to_string() }; self.taken.insert(name.clone()); name } + /// The name [`Document::new_swatch_name`] gives a new swatch of `paint`; the name is taken + /// from then on. + pub fn take_new(&mut self, paint: &Paint) -> String { + let base = match paint { + Paint::Solid { swatch: Some(n), tint, .. } if *tint < 1.0 => return self.take(&format!("{n} {}%", tint_percent(*tint))), + Paint::Solid { color, .. } => return self.take(&color_name(*color)), + Paint::Gradient(_) => "New Gradient Swatch", + _ => "New Pattern Swatch", + }; + let name = self.numbered(base, 1); + self.taken.insert(name.clone()); + name + } + /// Count `name` as taken (a swatch or colour group added another way). + pub fn insert(&mut self, name: &str) { + self.taken.insert(name.to_string()); + } + /// The first of "base `from`", "base `from + 1`"… not taken. + fn numbered(&mut self, base: &str, from: usize) -> String { + let key = (base.to_string(), from); + let start = self.next.get(&key).copied().unwrap_or(from); + let (i, name) = + (start..).map(|i| (i, format!("{base} {i}"))).find(|(_, n)| !self.taken.contains(n)).unwrap_or_else(|| (start, base.to_string())); + self.next.insert(key, i + 1); + name + } } /// What [`Document::map_solid_paints`] applies to each linkable colour: `(colour, link, tint)`, @@ -470,6 +488,40 @@ mod tests { ); } + #[test] + fn free_swatch_names_match_new_swatch_names_as_swatches_are_added() { + let (mut d, _, _) = doc(); + let red = Color::rgb(1.0, 0.0, 0.0); + d.swatches.push(Swatch { name: "New Gradient Swatch 2".into(), paint: Paint::solid(red), global: false, spot: false }); + let mut names = d.free_swatch_names(); + // A name taken another way (a group made alongside) counts for both. + names.insert("R=255 G=0 B=0 3"); + d.swatch_groups.push(SwatchGroup { name: "R=255 G=0 B=0 3".into(), swatches: vec![] }); + let tint = Paint::Solid { color: red, swatch: Some("Brand".into()), tint: 0.4 }; + let gradient = Paint::Gradient(Box::new(vectorcraft_color::GradientPaint::new(Default::default()))); + let paints = [Paint::solid(red), Paint::solid(red), gradient.clone(), tint.clone(), Paint::solid(red), gradient.clone(), tint, gradient]; + let mut added = vec![]; + for paint in paints { + let want = d.new_swatch_name(&paint); + assert_eq!(names.take_new(&paint), want); + d.swatches.push(Swatch { name: want.clone(), paint, global: false, spot: false }); + added.push(want); + } + assert_eq!( + added, + [ + "R=255 G=0 B=0", + "R=255 G=0 B=0 2", + "New Gradient Swatch 1", + "Brand 40%", + "R=255 G=0 B=0 4", + "New Gradient Swatch 3", + "Brand 40% 2", + "New Gradient Swatch 4" + ] + ); + } + #[test] fn mapping_copies_only_changed_nodes() { let (mut d, a, b) = doc(); diff --git a/crates/engine/src/cmd/swatch.rs b/crates/engine/src/cmd/swatch.rs index 59bfa528f..a3276a30d 100644 --- a/crates/engine/src/cmd/swatch.rs +++ b/crates/engine/src/cmd/swatch.rs @@ -9,10 +9,7 @@ use vectorcraft_doc::swatches::{LinkFn, color_name, map_node_solid_paints, node_ use vectorcraft_doc::{Document, NodeId}; use super::paint::paint_from; -use super::{ - CommandSpec, Result, Session, bad, bool_or, check_numbers, check_switches, cmd, color_value, has_doc, index, ok, str_param, switch_or, - unique_name, -}; +use super::{CommandSpec, Result, Session, bad, bool_or, check_numbers, check_switches, cmd, color_value, has_doc, index, ok, str_param, switch_or}; use crate::EngineError; pub fn specs() -> Vec { @@ -197,10 +194,11 @@ fn swatch_new(s: &mut Session, p: &Value) -> Result { let requested = name_param(p, "name").filter(|_| paints.len() == 1); let names = s.edit("New Swatch", |d, _| { let mut names = vec![]; + let mut free = d.free_swatch_names(); for paint in paints { let name = match &requested { - Some(n) => d.free_swatch_name(n), - None => d.new_swatch_name(&paint), + Some(n) => free.take(n), + None => free.take_new(&paint), }; // A tint swatch's kind is its base's. let tint = matches!(paint, Paint::Solid { swatch: Some(_), .. }); @@ -417,8 +415,11 @@ fn swatch_new_group(s: &mut Session, p: &Value) -> Result { } // The global swatches made from the art's colours, which its unlinked paints link to. let mut links: Vec<(Color, String)> = vec![]; + let mut free = d.free_swatch_names(); + free.insert(&name); + group.swatches.iter().for_each(|sw| free.insert(&sw.name)); for (c, global) in new { - let nm = unique_name(&color_name(c), |n| n == name || d.swatch_name_taken(n) || group.swatches.iter().any(|sw| sw.name == n)); + let nm = free.take(&color_name(c)); if global { links.push((c, nm.clone())); } @@ -1096,8 +1097,11 @@ pub(super) fn rewrite_group(d: &mut Document, group: &str, colors: &[Color], ren } } } + let mut free = d.free_swatch_names(); + free.insert(&name); + swatches.iter().for_each(|w| free.insert(&w.name)); for c in colors.iter().skip(swatches.len()) { - let nm = unique_name(&color_name(*c), |n| n == name || d.swatch_name_taken(n) || swatches.iter().any(|w| w.name == n)); + let nm = free.take(&color_name(*c)); swatches.push(Swatch { name: nm, paint: Paint::solid(*c), global: false, spot: false }); } let g = &mut d.swatch_groups[gi]; diff --git a/crates/engine/src/tests/swatchcmds.rs b/crates/engine/src/tests/swatchcmds.rs index 0e7e60da3..614d6400b 100644 --- a/crates/engine/src/tests/swatchcmds.rs +++ b/crates/engine/src/tests/swatchcmds.rs @@ -243,6 +243,44 @@ fn unused_matches_links_tints_colors_gradients_and_patterns_exactly() { } } +#[test] +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 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"]; + assert_eq!(r, json!({"name": want[0], "names": want})); + assert_eq!(undo_len(&s), undo + 1); + 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"]); +} + +#[test] +fn new_color_group_numbers_its_colors_after_the_group_and_its_moved_swatches() { + let mut s = session(); + run(&mut s, "swatch.new", json!({"name": "R=255 G=0 B=0 2", "color": "#0000ff"})); + let r = run( + &mut s, + "swatch.newGroup", + json!({"name": "R=255 G=0 B=0", "swatches": ["R=255 G=0 B=0 2"], "colors": ["#ff0000", "#ff0000", "#00ff00", "#ff0000"]}), + ); + let want = ["R=255 G=0 B=0 2", "R=255 G=0 B=0 3", "R=255 G=0 B=0 4", "R=0 G=255 B=0", "R=255 G=0 B=0 5"]; + assert_eq!(r, json!({"name": "R=255 G=0 B=0", "swatches": want, "linked": 0})); +} + +#[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 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"]; + assert_eq!(r, json!({"name": "R=255 G=0 B=0 2", "swatches": want, "relinked": 0})); +} + #[test] fn unused_leaves_out_linked_colours_gradients_and_patterns_in_use() { let mut s = session(); diff --git a/docs/target-app-parity.md b/docs/target-app-parity.md index 6cdc72656..3dfe31202 100644 --- a/docs/target-app-parity.md +++ b/docs/target-app-parity.md @@ -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 and tints up in hash sets, so their time grows in step with the number of colors in a document rather than with its square. The swatches Add Used Colors adds 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` and `unused_matches_links_tints_colors_gradients_and_patterns_exactly` in `crates/engine/src/tests/swatchcmds.rs`, `free_swatch_names_match_naming_each_swatch_as_it_is_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, 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`. - **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.