From 3fbf957fe308490a7a7cce6a3dd4bb73d871c5a2 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sun, 15 Nov 2026 12:01:47 -0800 Subject: [PATCH] Recolor Artwork: Randomize leaves rows with no current colors out A New Row row has no current colors and recolors nothing, but recolor.randomize shuffled its new color with the other rows', so an art row's color could move onto the empty row and leave the art. Randomize now shuffles and varies only the rows that hold current colors and are not excluded; an empty row keeps its new color. docs/mcp.md says so, and says to Shift-click a row below the new row before Merge, since Merge keeps the topmost selected row's new color. --- crates/engine/src/cmd/recolor.rs | 11 +++++++---- crates/engine/src/tests/recolor.rs | 21 +++++++++++++++++++++ docs/mcp.md | 5 +++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/recolor.rs b/crates/engine/src/cmd/recolor.rs index e54d68204e..47e11f9e81 100644 --- a/crates/engine/src/cmd/recolor.rs +++ b/crates/engine/src/cmd/recolor.rs @@ -52,7 +52,7 @@ pub fn specs() -> Vec { "Randomize Colors", [], None, - "{map: rows as recolor.apply, order?: true (shuffle the new colours among the rows that aren't excluded), saturationBrightness?: false (random saturation and brightness, hue kept), seed?: 0} → {map: the rows with their new colours as keys}", + "{map: rows as recolor.apply, order?: true (shuffle the new colours among the rows that aren't excluded and hold current colours), saturationBrightness?: false (random saturation and brightness, hue kept), seed?: 0; an excluded row, and a row whose from is empty, keeps its new colour} → {map: the rows with their new colours as keys}", always, randomize ), @@ -1746,8 +1746,11 @@ fn randomize(_: &mut Session, p: &Value) -> Result { let mut rng = Rng::new(super::menucmds::whole_or(p, "seed", 0, C)?); let (order, saturation_brightness) = (switch_or(p, "order", true, C)?, switch_or(p, "saturationBrightness", false, C)?); // Each new color with its row's tint and model: a tint moves with its color, and goes when - // the color changes; a model moves with its color. - let mut news: Vec<(Color, f32, Option)> = rows.iter().filter_map(|r| r.new_color().map(|c| (c, r.tint, r.model))).collect(); + // the color changes; a model moves with its color. A row with no current colors recolors + // nothing, so it keeps its new color and takes no other row's. + let takes = |r: &Row| !r.from.is_empty() && r.new_color().is_some(); + let mut news: Vec<(Color, f32, Option)> = + rows.iter().filter(|r| takes(r)).filter_map(|r| r.new_color().map(|c| (c, r.tint, r.model))).collect(); if order { rng.shuffle(&mut news); } @@ -1765,7 +1768,7 @@ fn randomize(_: &mut Session, p: &Value) -> Result { .iter() .map(|r| { let (to, tint, model) = - if r.new_color().is_some() { news.next().map_or((None, 1.0, None), |(c, t, m)| (Some(c), t, m)) } else { (r.to, r.tint, r.model) }; + if takes(r) { news.next().map_or((None, 1.0, None), |(c, t, m)| (Some(c), t, m)) } else { (r.to, r.tint, r.model) }; let mut row = json!({"from": r.from.iter().map(Match::to_json).collect::>(), "to": to.as_ref().map(key_str)}); if r.exclude { row["exclude"] = json!(true); diff --git a/crates/engine/src/tests/recolor.rs b/crates/engine/src/tests/recolor.rs index 390248f2a9..eff144557a 100644 --- a/crates/engine/src/tests/recolor.rs +++ b/crates/engine/src/tests/recolor.rs @@ -380,6 +380,27 @@ fn randomize_shuffles_new_colours_and_skips_excluded_rows() { assert!((hue(&sb["map"][0]["to"]) - 0.0).abs() < 1.0 && (hue(&sb["map"][1]["to"]) - 120.0).abs() < 1.0, "hue kept"); } +/// A row with no current colors (New Row) keeps its new color through Randomize, and its color +/// goes to no row that holds current colors: the other rows shuffle their own colors only. +#[test] +fn randomize_leaves_an_empty_row_out() { + let mut s = session(); + let map = json!([ + {"from": [], "to": "rgb 0 255 0"}, + {"from": ["rgb 255 0 0"], "to": "rgb 255 0 0"}, + {"from": ["rgb 0 0 255"], "to": "rgb 0 0 255"}, + ]); + for seed in 0..16 { + let r = run(&mut s, "recolor.randomize", json!({"map": map, "seed": seed})); + assert_eq!(r["map"][0], map[0], "seed {seed}: the empty row keeps its color"); + let mut tos: Vec = [1, 2].iter().map(|i| r["map"][*i]["to"].as_str().unwrap().to_string()).collect(); + tos.sort(); + assert_eq!(tos, ["rgb 0 0 255", "rgb 255 0 0"], "seed {seed}: a permutation of the filled rows' colors"); + } + let sb = run(&mut s, "recolor.randomize", json!({"map": map, "order": false, "saturationBrightness": true, "seed": 3})); + assert_eq!(sb["map"][0], map[0], "the empty row keeps its saturation and brightness"); +} + #[test] fn edit_group_rewrites_in_order_as_one_undo_step() { let mut s = session(); diff --git a/docs/mcp.md b/docs/mcp.md index e623b86cac..eb8aa7ecbf 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -4508,9 +4508,10 @@ darker than the row's average become tints and shades) or `hueShift` (the most s the others turn by the same hue). `limitTo` snaps new colours to a swatch library's nearest colour; `recolor.randomize {map, order?, saturationBrightness?, seed?}` shuffles or varies new colours; a row with `exclude: true` keeps its colours. A row's `from` may be an empty list: the row recolors nothing, adds no library swatch with -`limitTo` and gives `group` no color, and `recolor.randomize` and `recolor.weigh` keep it. A row without `from`, or with +`limitTo` and gives `group` no color, and `recolor.randomize` and `recolor.weigh` keep it; `recolor.randomize` leaves its +new color as it is and shuffles only the rows that hold current colors. A row without `from`, or with a current color that is not a color, is refused. With art selected, the Recolor Artwork dialog's New Row button (beside -Merge) adds such a row above the first selected row, or at the top, with that row's new color, and selects it; Shift-click another row and Merge to move its colors into it. An +Merge) adds such a row above the first selected row, or at the top, with that row's new color, and selects it; Shift-click a row below it and Merge to move that row's colors into it (Merge keeps the topmost selected row's new color). An empty row does not count toward Colors. With `limitTo`, a row's `tint` (a percentage, default 100) gives the art linked to the global or spot swatch its new color is that share of its tint; `tint` must be a number. `recolor.randomize` 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