Merge pull request #7958 from dotCooCoo/fix/recolor-randomize-skips-empty-rows

Recolor Artwork: Randomize leaves rows with no current colors out
This commit is contained in:
BFlat
2026-11-16 06:54:48 +07:00
committed by GitHub
3 changed files with 31 additions and 6 deletions
+7 -4
View File
@@ -52,7 +52,7 @@ pub fn specs() -> Vec<CommandSpec> {
"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 (an excluded row, or a row whose from is empty, keeps its new color), order?: true (shuffle the new colours among the rows that aren't excluded and hold current colors), saturationBrightness?: false (random saturation and brightness, hue kept), seed?: 0} → {map: the rows with their new colours as keys}",
always,
randomize
),
@@ -1746,8 +1746,11 @@ fn randomize(_: &mut Session, p: &Value) -> Result<Value> {
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<ModelRule>)> = 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<ModelRule>)> =
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<Value> {
.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::<Vec<_>>(), "to": to.as_ref().map(key_str)});
if r.exclude {
row["exclude"] = json!(true);
+21
View File
@@ -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<String> = [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();