Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Conditional Mode Change: "gray" as a single source mode matches Grayscale (#7042)

David Manncb185320ed8c
diff --git a/crates/engine/src/file_cmds.rs b/crates/engine/src/file_cmds.rs
index 43828b76b..b588846d8 100644
--- a/crates/engine/src/file_cmds.rs
+++ b/crates/engine/src/file_cmds.rs
@@ -903,11 +903,14 @@ fn conditional_mode_change(s: &mut Session, p: &Value) -> Result<Value> {
other => return Err(EngineError::BadParams { cmd: cmd.into(), msg: format!("unsupported target mode \"{other}\" (rgb, grayscale, cmyk, lab)") }),
};
let cur = mode_name(s.active().ok_or(EngineError::NoDocument)?.doc.mode);
+ // One spelling per source mode, for the array and the single-string forms alike (#7042).
+ let norm = |v: &str| {
+ let v = v.to_ascii_lowercase();
+ if v == "gray" { "grayscale".to_string() } else { v }
+ };
let from: Vec<String> = match p.get("from") {
- Some(Value::Array(a)) => {
- a.iter().filter_map(Value::as_str).map(|v| v.to_ascii_lowercase().replace("gray", "grayscale").replace("grayscalescale", "grayscale")).collect()
- }
- Some(Value::String(v)) if v != "any" => vec![v.to_ascii_lowercase()],
+ Some(Value::Array(a)) => a.iter().filter_map(Value::as_str).map(norm).collect(),
+ Some(Value::String(v)) => vec![norm(v)],
_ => vec!["any".into()],
};
if cur == to || !(from.iter().any(|f| f == "any" || f == cur)) {
diff --git a/crates/engine/src/file_cmds/tests.rs b/crates/engine/src/file_cmds/tests.rs
index 3cc72c30c..d262cf706 100644
--- a/crates/engine/src/file_cmds/tests.rs
+++ b/crates/engine/src/file_cmds/tests.rs
@@ -541,6 +541,23 @@ fn fit_image_and_conditional_mode_change() {
assert!(s.execute("file.automate.conditionalModeChange", json!({"to": "duotone"})).is_err());
}
+#[test]
+fn conditional_mode_change_single_gray_source_matches_grayscale() {
+ // A single `"from"` string is normalised like the array form: "gray" names Grayscale (#7042).
+ let mut s = session(4, 4, 8);
+ s.execute("image.mode.grayscale", json!({})).unwrap();
+ let r = s.execute("file.automate.conditionalModeChange", json!({"from": "rgb", "to": "rgb"})).unwrap();
+ assert_eq!(r["changed"], false);
+ assert_eq!(doc(&s).mode, ColorMode::Grayscale);
+ let r = s.execute("file.automate.conditionalModeChange", json!({"from": "gray", "to": "rgb"})).unwrap();
+ assert_eq!(r["changed"], true, "{r}");
+ assert_eq!(doc(&s).mode, ColorMode::Rgb);
+ s.execute("image.mode.grayscale", json!({})).unwrap();
+ let r = s.execute("file.automate.conditionalModeChange", json!({"from": "Gray", "to": "rgb"})).unwrap();
+ assert_eq!(r["changed"], true, "{r}");
+ assert_eq!(doc(&s).mode, ColorMode::Rgb);
+}
+
#[test]
fn conditional_mode_change_cannot_run_a_mode_change_the_gate_denies() {
fn deny_mode(id: &str, _: &Value) -> Result<()> {