Swatches: Add to Swatches brings a library color group with its gradient and pattern swatches

swatch.library.add no longer refuses a library color group that holds a
gradient or pattern swatch. main now adds a .ai library's pattern swatch
in its color group (#8081), and Recolor Artwork keeps a group's gradient
and pattern swatches at their places (#8556), so the group comes in
whole, each swatch at its place.
This commit is contained in:
dotCooCoo committed 2026-11-18 14:01:22 -08:00
1 parent f34f9e7bb8
commit fa87b41da1
3 files changed
+16 -32

No files matched your search

+2 -13
View File
@@ -49,7 +49,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Add to Swatches",
["Window", "Swatch Libraries"],
None,
"{library: id or name, names?: [swatch or colour group names in the library] (default: all of it; a group comes as a colour group, a single swatch ungrouped), apply?: \"fill\"|\"stroke\" (also apply the first one to the selection and the defaults, as paint.setFill / paint.setStroke), focus?: true (with apply: false keeps the active proxy)} copy library swatches into the document as one undo step. A gradient or pattern swatch in a library color group is refused (color groups hold solid colors only), and the document is left unchanged. A swatch the document already has (same name and paint) isn't added again; a name taken by another swatch gets a number (\"Clay 1 2\"). A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, and a name another pattern or swatch has gets a number; a global or spot color its art links to whose name the document gives another color comes in numbered, and the pattern links to it → {library, added: [names in the document], existing: [names already there], applied?: name}",
"{library: id or name, names?: [swatch or colour group names in the library] (default: all of it; a group comes as a colour group, a single swatch ungrouped), apply?: \"fill\"|\"stroke\" (also apply the first one to the selection and the defaults, as paint.setFill / paint.setStroke), focus?: true (with apply: false keeps the active proxy)} copy library swatches into the document as one undo step. A swatch the document already has (same name and paint) isn't added again; a name taken by another swatch gets a number (\"Clay 1 2\"). A pattern swatch brings its pattern, as pasting art painted with it does: the same pattern the document has is used, and a name another pattern or swatch has gets a number; a global or spot color its art links to whose name the document gives another color comes in numbered, and the pattern links to it → {library, added: [names in the document], existing: [names already there], applied?: name}",
has_doc,
add
),
@@ -860,14 +860,7 @@ fn copy_to_user(s: &mut Session, p: &Value) -> Result<Value> {
/// The library swatches `names` stand for, each once, with the colour group it goes into (`None`:
/// ungrouped): a group's name brings the group, a swatch's name the swatch alone; no names, all.
/// A picked group that holds a gradient or pattern swatch is refused.
fn picked<'a>(lib: &'a SwatchLibrary, names: &[String], cmd: &str) -> Result<Vec<(Option<&'a str>, &'a Swatch)>> {
// Color groups hold solid colors only, as swatch.new, swatch.newGroup and swatch.move keep them.
// Each picked group is checked whole, so a member also named on its own is not skipped.
let solid = |g: &SwatchGroup| match g.swatches.iter().find(|w| w.paint.color().is_none()) {
Some(w) => Err(bad(cmd, format!("`{}`: colour groups hold solid colours only", w.name))),
None => Ok(()),
};
let mut out: Vec<(Option<&str>, &Swatch)> = vec![];
let mut seen: HashSet<&str> = HashSet::new();
let mut push = |g: Option<&'a str>, w: &'a Swatch| {
@@ -876,16 +869,12 @@ fn picked<'a>(lib: &'a SwatchLibrary, names: &[String], cmd: &str) -> Result<Vec
}
};
if names.is_empty() {
lib.groups.iter().try_for_each(solid)?;
lib.swatches.iter().for_each(|w| push(None, w));
lib.groups.iter().for_each(|g| g.swatches.iter().for_each(|w| push(Some(&g.name), w)));
}
for n in names {
match (lib.group(n), lib.swatch(n)) {
(Some(g), _) => {
solid(g)?;
g.swatches.iter().for_each(|w| push(Some(&g.name), w));
}
(Some(g), _) => g.swatches.iter().for_each(|w| push(Some(&g.name), w)),
(None, Some(w)) => push(None, w),
(None, None) => return Err(bad(cmd, format!("no swatch or colour group `{n}` in `{}`", lib.name))),
}
+13 -17
View File
@@ -160,10 +160,10 @@ fn save_and_load_round_trip_keeps_cmyk_spot_and_groups() {
assert!(s.execute("swatch.library.save", &json!({"user": true})).is_err(), "no user folder in a headless session");
}
/// A library color group that holds a gradient is not copied into the document: color groups
/// hold solid colors only. The document keeps its swatches and no undo step is recorded.
/// A library color group that holds a gradient comes into the document whole, the gradient at its
/// place in the group, as a `.ai` library's pattern swatch comes in its color group.
#[test]
fn add_refuses_a_library_color_group_holding_a_gradient() {
fn add_brings_a_library_color_group_holding_a_gradient_whole() {
let mut s = session();
run(&mut s, "swatch.newGroup", json!({"name": "Brand", "colors": ["#123456", "#abcdef"]}));
let r = run(&mut s, "swatch.library.save", json!({"names": ["Brand", "Sunset"], "name": "Mixed"}));
@@ -178,21 +178,17 @@ fn add_refuses_a_library_color_group_holding_a_gradient() {
let (_, loaded) = cmd::swatchlib::library(&s, &id).unwrap();
assert!(matches!(loaded.groups[0].swatches[1].paint, Paint::Gradient(_)), "the reader keeps the gradient in the group");
run(&mut s, "swatch.delete", json!({"names": ["Brand", "Sunset"]}));
let (before, swatches, undo) = (doc(&s).swatch_groups.clone(), doc(&s).swatches.len(), undo_len(&s));
// A group is refused whole even when its gradient was also named on its own before it.
let cases = [json!({"library": id}), json!({"library": id, "names": ["Brand"]}), json!({"library": id, "names": ["Sunset", "Brand"]})];
for p in cases {
let e = s.execute("swatch.library.add", &p).unwrap_err().to_string();
assert!(e.contains("`Sunset`: colour groups hold solid colours only"), "{p}: {e}");
assert_eq!(doc(&s).swatch_groups, before, "{p}");
assert_eq!(doc(&s).swatches.len(), swatches, "{p}");
assert_eq!(undo_len(&s), undo, "{p}");
for p in [json!({"library": id}), json!({"library": id, "names": ["Brand"]})] {
let undo = undo_len(&s);
let r = run(&mut s, "swatch.library.add", p.clone());
assert!(r["added"].as_array().unwrap().iter().any(|n| n == "Sunset"), "{p}: {r}");
let brand = doc(&s).swatch_groups.iter().find(|g| g.name == "Brand").map(|g| g.swatches.clone()).unwrap_or_default();
let kinds: Vec<(&str, bool)> = brand.iter().map(|w| (w.name.as_str(), matches!(w.paint, Paint::Gradient(_)))).collect();
assert_eq!(kinds.len(), 3, "{p}: {kinds:?}");
assert_eq!(kinds[1], ("Sunset", true), "{p}: the gradient keeps its place in the group");
assert_eq!(undo_len(&s), undo + 1, "{p}: one undo step");
run(&mut s, "edit.undo", json!({}));
}
// The gradient on its own comes in ungrouped.
let r = run(&mut s, "swatch.library.add", json!({"library": id, "names": ["Sunset"]}));
assert_eq!(r["added"], json!(["Sunset"]), "{r}");
assert!(doc(&s).swatches.iter().any(|w| w.name == "Sunset" && matches!(w.paint, Paint::Gradient(_))));
assert_eq!(doc(&s).swatch_groups, before);
}
/// The swatch library commands, and the graphic style library save and load that share their file
+1 -2
View File
@@ -5355,8 +5355,7 @@ them (`id`, `name`, `category`, `count`) and `swatch.library.get {library}` retu
in the `swatch.list` shape (`library` is an id or a name). `swatch.library.add {library, names?}` copies swatches
into the document as one undo step: a colour group's name brings the whole group, a swatch's name the swatch alone,
no names the whole library; swatches the document already has (same name and paint) are reported under `existing`
and not added again. Color groups hold solid colors only: an add that would bring a gradient or pattern swatch
into a color group is refused, and the document is left unchanged. `apply: "fill"|"stroke"` also applies the first one, in the same undo step (what clicking a
and not added again. `apply: "fill"|"stroke"` also applies the first one, in the same undo step (what clicking a
swatch in the library panel does). The pattern libraries Geometric Patterns (`geometric-patterns`), Grit Textures
(`grit-textures`: seamless black grain, grit, sand, speckle, stipple, scratch and fleck tiles) and Optical Effects
(`optical-effects`: VectorCraft's own black wave, ring, warped-check, pinched-stripe, chevron and lens tiles),