Recolor Artwork: an excluded row keeps its color group swatch

OK on a color group sent an excluded row's new color in groupColors, so
the group's swatch at that place changed (or was removed after Remove
Color) while the row's art kept its colors. The dialog now sends "keep"
for an excluded row with current colors. recolor.apply takes "keep" in
groupColors: the swatch at that place stays as it is, without Global
Adjust, Limit to Library or the document's model, and past the group's
last swatch it adds none.
This commit is contained in:
dotCooCoo
2026-11-15 13:58:25 -08:00
parent 0fb2c95c51
commit a075bfbffd
5 changed files with 73 additions and 8 deletions
+17 -3
View File
@@ -34,7 +34,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Recolor Artwork",
["Edit", "Edit Colors"],
None,
"{map: rows [{from: [colour keys (recolor.colors) or \"#rrggbb\" (every colour shown as that hex); may be empty: an empty row recolours nothing, adds no library swatch and gives `group` no colour unless `added`], to: colour (hex, key, {c,m,y,k} or {l,a,b}) | null (in from, to and groupColors, #rrggbbaa and #rgba are refused), exclude?: false (the row's colours are kept), tint?: 100 (percent; with limitTo, art linked to the global or spot swatch the new color is takes this share of its tint), added?: false (an empty row's new color is a color added to `group`)}] | {\"key or #rrggbb\": colour, …} (one row per colour), method?: \"exact\" (default)|\"preserveTints\"|\"scaleTints\"|\"tintsShades\"|\"hueShift\" (how a row's colours take its new colour, relative to the row's darkest, average or most saturated colour), limitTo?: swatch library id or name; or \"document\" (the document's swatches): new colours snap to its nearest colour, group?: colour group to rewrite, groupColors?: [colour | null] (what `group` is rewritten with, a null removing the group's swatch at its place; default: the new colors of the rows with current colors, in order, then those of the empty rows with `added`, excluded rows left out), rename?: the group's new name, includeImages?: true, includePatterns?: true, globalAdjust?: {saturation?: 0, brightness?: 0, temperature?: 0, luminosity?: 0} (Global Adjust: percent from -100 to 100, 0 changes nothing; every colour a row that isn't excluded recolours, and the group's new colours, take the same change after the row's new colour: saturation scales HSB saturation by 1 + saturation/100, brightness moves HSB brightness that share of the way to full or to black, temperature adds 0.2 × temperature/100 to red and takes it from blue, luminosity moves Lab lightness that share of the way to 100 or 0 keeping a and b; with limitTo each row's new colour takes the change before it snaps to the library)} recolour the selection (gradients, text, meshes, image pixels and pattern tiles included; each new colour keeps the model of the one it replaces (model?: \"keep\" default | \"new\" (the new colour's own model, so a CMYK colour replacing RGB art gives CMYK art) | \"document\" (the document's colour mode)), except a library swatch's with limitTo: art takes the swatch itself, linked to a global or spot colour at its tints, and the swatch joins the document's swatches; art linked to a global swatch of `group` that is rewritten with its row's new color stays linked to it, at its tint, unless the row converts the color into the art's model; other recolored art, and art in a row linked to a global swatch of `group` that is rewritten with another color, keeps its row's new color and loses its swatch link) and the group, as one undo step (none when nothing changes). On a path directly in a graph series, the graph also stores the new paint of the topmost visible fill and stroke, and on a graph label the new paint of its characters and rows for every label of its kind (docs/mcp.md) → {changed, swatches: [names added to the document's swatches], relinked?: with group, the other paints linked to its global swatches that took their new colors, note?: set when a colour kept a model other than its new colour's (see model), notChanged: {locked, hidden} (as edit.colors.invert), warning?: set when notChanged counts any object}",
"{map: rows [{from: [colour keys (recolor.colors) or \"#rrggbb\" (every colour shown as that hex); may be empty: an empty row recolours nothing, adds no library swatch and gives `group` no colour unless `added`], to: colour (hex, key, {c,m,y,k} or {l,a,b}) | null (in from, to and groupColors, #rrggbbaa and #rgba are refused), exclude?: false (the row's colours are kept), tint?: 100 (percent; with limitTo, art linked to the global or spot swatch the new color is takes this share of its tint), added?: false (an empty row's new color is a color added to `group`)}] | {\"key or #rrggbb\": colour, …} (one row per colour), method?: \"exact\" (default)|\"preserveTints\"|\"scaleTints\"|\"tintsShades\"|\"hueShift\" (how a row's colours take its new colour, relative to the row's darkest, average or most saturated colour), limitTo?: swatch library id or name; or \"document\" (the document's swatches): new colours snap to its nearest colour, group?: colour group to rewrite, groupColors?: [colour | null | \"keep\"] (what `group` is rewritten with, a null removing the group's swatch at its place and \"keep\" leaving it as it is, without globalAdjust, limitTo or model, and adding none past the group's swatches; default: the new colors of the rows with current colors, in order, then those of the empty rows with `added`, excluded rows left out), rename?: the group's new name, includeImages?: true, includePatterns?: true, globalAdjust?: {saturation?: 0, brightness?: 0, temperature?: 0, luminosity?: 0} (Global Adjust: percent from -100 to 100, 0 changes nothing; every colour a row that isn't excluded recolours, and the group's new colours, take the same change after the row's new colour: saturation scales HSB saturation by 1 + saturation/100, brightness moves HSB brightness that share of the way to full or to black, temperature adds 0.2 × temperature/100 to red and takes it from blue, luminosity moves Lab lightness that share of the way to 100 or 0 keeping a and b; with limitTo each row's new colour takes the change before it snaps to the library)} recolour the selection (gradients, text, meshes, image pixels and pattern tiles included; each new colour keeps the model of the one it replaces (model?: \"keep\" default | \"new\" (the new colour's own model, so a CMYK colour replacing RGB art gives CMYK art) | \"document\" (the document's colour mode)), except a library swatch's with limitTo: art takes the swatch itself, linked to a global or spot colour at its tints, and the swatch joins the document's swatches; art linked to a global swatch of `group` that is rewritten with its row's new color stays linked to it, at its tint, unless the row converts the color into the art's model; other recolored art, and art in a row linked to a global swatch of `group` that is rewritten with another color, keeps its row's new color and loses its swatch link) and the group, as one undo step (none when nothing changes). On a path directly in a graph series, the graph also stores the new paint of the topmost visible fill and stroke, and on a graph label the new paint of its characters and rows for every label of its kind (docs/mcp.md) → {changed, swatches: [names added to the document's swatches], relinked?: with group, the other paints linked to its global swatches that took their new colors, note?: set when a colour kept a model other than its new colour's (see model), notChanged: {locked, hidden} (as edit.colors.invert), warning?: set when notChanged counts any object}",
has_doc,
apply
),
@@ -1655,12 +1655,26 @@ fn apply(s: &mut Session, p: &Value) -> Result<Value> {
Some(Value::Array(cs)) => Some(cs),
Some(_) => return Err(bad(C, "`groupColors` must be an array")),
};
// A null in groupColors removes the group's swatch at its place.
// The colors of the group's swatches, which "keep" leaves as they are.
let swatches: Vec<Option<Color>> = match group {
Some(g) => {
s.doc()?.doc.swatch_groups.iter().find(|x| x.name == g).map(|x| x.swatches.iter().map(|w| w.paint.color()).collect()).unwrap_or_default()
}
None => vec![],
};
// A null in groupColors removes the group's swatch at its place; "keep" leaves it as it is,
// without Global Adjust, Limit to Library or the document's model, and past the swatches adds none.
let new: Vec<Option<Color>> = match group_colors {
Some(cs) => cs
.iter()
.map(|v| match v {
.enumerate()
.map(|(i, v)| match v {
Value::Null => Ok(None),
Value::String(k) if k == "keep" => match swatches.get(i) {
Some(Some(c)) => Ok(Some(*c)),
Some(None) => Err(bad(C, format!("the swatch at place {} of `group` has no colour to keep", i + 1))),
None => Ok(None),
},
v => color_value(v)
.map(|c| adjust.apply(c))
.map(|c| Some(in_doc_model(palette.as_ref().map_or(c, |pl| pl.nearest(c)))))
+15
View File
@@ -804,6 +804,21 @@ fn a_null_in_group_colors_removes_that_swatch_and_the_ones_after_it_keep_their_p
assert_eq!(link(&s, green), ("#00ff00".into(), Some(names[2].clone())), "art linked to a swatch that stays keeps its link");
}
/// "keep" in `groupColors` leaves the group's swatch at its place as it is, without Global Adjust,
/// and past the group's swatches it adds none.
#[test]
fn keep_in_group_colors_leaves_that_swatch_as_it_is() {
let mut s = session();
run(&mut s, "swatch.newGroup", json!({"name": "Brand", "colors": ["#2878f0", "#ff0000"]}));
let p = json!({"map": [], "group": "Brand", "groupColors": ["keep", "#00ff00", "keep"], "globalAdjust": {"brightness": -50}});
run(&mut s, "recolor.apply", p);
let g = &s.doc().unwrap().doc.swatch_groups.iter().find(|g| g.name == "Brand").unwrap().swatches;
let g: Vec<String> = g.iter().map(|w| w.paint.color().unwrap().to_hex()).collect();
assert_eq!(g.len(), 2, "{g:?}");
assert_eq!(g[0], "#2878f0", "the kept swatch takes no Global Adjust");
assert_ne!(g[1], "#00ff00", "the other new color takes it");
}
#[test]
fn recolor_of_everything_does_not_count_locked_art_whose_pattern_the_group_rewrite_recolors() {
let mut s = session();
+36 -3
View File
@@ -600,14 +600,16 @@ fn selection(d: &Dialog) -> Vec<usize> {
/// `recolor.apply` parameters: the rows, method, library and the group with its new name and
/// colors: the new colors of the rows with current colors, each in its swatch's place (`null`
/// for a row whose new color was removed, which removes its swatch), then (with art) the group's
/// colors no row took, then the colors added on the Edit tab.
/// for a row whose new color was removed, which removes its swatch; `"keep"` for an excluded row,
/// which leaves its swatch as it is), then (with art) the group's colors no row took, then the
/// colors added on the Edit tab.
fn apply_params(d: &Dialog) -> Value {
let rs = rows(d);
let keep = json!("keep");
let group = d.fields.get("group").and_then(Value::as_str).map(|g| {
// A row made with New Row that holds no current colors gives the group no color; a color
// added on the Edit tab (`added`) joins it after the others.
let took: Vec<&Value> = rs.iter().filter(|r| !no_current(r)).map(|r| &r["to"]).collect();
let took: Vec<&Value> = rs.iter().filter(|r| !no_current(r)).map(|r| if excluded(r) { &keep } else { &r["to"] }).collect();
let assign = d.fields.get("__assign").and_then(Value::as_array).filter(|_| d.bool("__art"));
let rest = assign.map_or(&[][..], |a| a.get(took.len()..).unwrap_or_default());
let added = rs.iter().filter(|r| no_current(r) && is_added(r) && !excluded(r)).map(|r| &r["to"]).filter(|c| !c.is_null());
@@ -2148,6 +2150,37 @@ mod tests {
assert_eq!(apply_params(&d)["groupColors"], json!(["rgb 1 1 1", null, "rgb 3 3 3"]));
}
/// An excluded row with current colors keeps the group's swatch at its place as it is, even
/// after its new color was edited or removed, and with Global Adjust on.
#[test]
fn an_excluded_row_keeps_its_group_swatch() {
let mut d = Dialog::new(KIND, json!({"group": "G", "groupName": "G", "__art": true, "__assign": ["rgb 1 1 1", "rgb 2 2 2", "rgb 3 3 3"]}));
let rs = json!([
{"from": ["rgb 255 0 0"], "to": "rgb 7 7 7", "exclude": true},
{"from": ["rgb 0 0 255"], "to": null, "exclude": true},
{"from": ["rgb 0 255 0"], "to": "rgb 8 8 8"},
]);
d.fields.insert("rows".into(), rs);
assert_eq!(apply_params(&d)["groupColors"], json!(["keep", "keep", "rgb 8 8 8"]));
// Without art the rows are the group's colors.
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
app.run("file.new", json!({"width": 100, "height": 100})).unwrap();
app.run("swatch.newGroup", json!({"name": "G", "colors": ["#2878f0", "#ff0000"]})).unwrap();
app.run("ui.recolorDialog", json!({"group": "G"})).unwrap();
let mut d = app.ui.dialog.clone().unwrap();
let mut rs = super::rows(&d);
rs[0]["to"] = json!("#00ff00");
rs[0]["exclude"] = json!(true);
rs[1]["to"] = json!("#0000ff");
d.fields.insert("rows".into(), json!(rs));
d.fields.insert("globalAdjust".into(), json!({"brightness": -50}));
confirm(&mut app, &d).unwrap();
let g = app.session.doc().unwrap().doc.swatch_groups.iter().find(|g| g.name == "G").unwrap();
let hex: Vec<String> = g.swatches.iter().map(|w| w.paint.color().unwrap().to_hex()).collect();
assert_eq!(hex[0], "#2878f0", "{hex:?}");
assert_ne!(hex[1], "#ff0000", "{hex:?}");
}
#[test]
fn merge_and_separate_rows() {
let mut r = rows();
+1 -1
View File
@@ -295,7 +295,7 @@ on either wheel: `add` (the Add Color tool) adds a click as a new color, the hue
color's brightness (the base color's when none is selected; the patch's color on the segmented wheel), in a new row with no current colors (`from: []`) and
`added: true` that becomes the selected row; `remove` (the Remove Color tool) removes the color of the marker clicked. The base color can't be removed. A removed color's row is dropped, unless it
has current colors and art or a group is open: it then keeps them with `to: null`, that art keeps its colors, and the
group's swatch at that place is removed. A row with no current colors recolors no art; with a group, the new color of
group's swatch at that place is removed, unless the row is excluded: an excluded row with current colors leaves the group's swatch at its place as it is. A row with no current colors recolors no art; with a group, the new color of
one with `added: true` that is not excluded is added to the group after the group's other colors, and a row made with New Row adds none.
The base color is the row with `base: true`, or the first color the Edit tab shows when no row has it; the harmony rules
build from it, and selecting another color for the sliders leaves it the base. A right-click on a wheel marker or a
+4 -1
View File
@@ -4518,7 +4518,10 @@ linked to the global or spot swatch its new color is that share of its tint; `ti
moves a row's `tint` with its new color and removes it from a color it changes, and moves a row's `model` with its new color. `swatch.editGroup {group, colors, rename?}` rewrites a colour group in one undo
step (art linked to its global swatches follows), and `recolor.apply` with `group` recolours the art and rewrites the
group together. A `null` in `groupColors` removes the group's swatch at that place, and the swatches after it keep
their colors and names; art linked to a removed global swatch keeps its color and is no longer linked to it. Art linked to one of the group's global swatches stays linked when the swatch is rewritten with its
their colors and names; art linked to a removed global swatch keeps its color and is no longer linked to it. A `"keep"`
in `groupColors` leaves the group's swatch at that place as it is (`globalAdjust`, `limitTo` and `model` do not change
it) and adds no swatch past the group's last one; the Recolor Artwork dialog sends it for an excluded row with current
colors. A swatch at that place that is not a color is refused. Art linked to one of the group's global swatches stays linked when the swatch is rewritten with its
row's new color (the default without `groupColors` when the rows follow the group's order), so a later `swatch.edit` still
reaches it; the reply's `relinked` counts the other linked paints that took the group's new colors. Art in a row that
is linked to a swatch rewritten with another color keeps its row's new color, even an unchanged one, and is no longer