Skip to content

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

Actions: a Selective Color step keeps the values of its scalar form

Hamza7835d05a54bc
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index d5c688f0a..a4d6c33de 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -724,12 +724,33 @@ fn selective_from(d: &Descriptor) -> Option<Value> {
}
fn selective_to(p: &Value) -> Option<Descriptor> {
- let mut list = Vec::new();
- for (ps, name) in SELECTIVE_RANGES {
+ // Each range, as the command reads it (`adjust_cmds::selective_from_params`): the array form
+ // `"reds": [c, m, y, k]` first, then the scalar form, which names one range in `"colors"` and
+ // gives the channels it changes. Writing only the arrays dropped the scalar form's values, and
+ // the step exported as an adjustment that changes nothing (#7672).
+ let mut rows: Vec<Option<[f64; 4]>> = vec![None; SELECTIVE_RANGES.len()];
+ for (row, (_, name)) in rows.iter_mut().zip(SELECTIVE_RANGES) {
if let Some(a) = p.get(*name).and_then(Value::as_array) {
let v = |i: usize| a.get(i).and_then(Value::as_f64).unwrap_or(0.0);
+ *row = Some([v(0), v(1), v(2), v(3)]);
+ }
+ }
+ if let Some(named) = jstr(p, "colors") {
+ let i = SELECTIVE_RANGES.iter().position(|(_, name)| *name == named)?;
+ let row = rows.get_mut(i)?;
+ let mut v = row.unwrap_or([0.0; 4]);
+ for (slot, key) in v.iter_mut().zip(["cyan", "magenta", "yellow", "black"]) {
+ if let Some(n) = jnum(p, key) {
+ *slot = n;
+ }
+ }
+ *row = Some(v);
+ }
+ let mut list = Vec::new();
+ for (row, (ps, _)) in rows.iter().zip(SELECTIVE_RANGES) {
+ if let Some(v) = row {
list.push(D::Descriptor(
- desc("ClrC").with("Clrs", enm("Clrs", ps)).with("Cyn ", pct(v(0))).with("Mgnt", pct(v(1))).with("Yllw", pct(v(2))).with("Blck", pct(v(3))),
+ desc("ClrC").with("Clrs", enm("Clrs", ps)).with("Cyn ", pct(v[0])).with("Mgnt", pct(v[1])).with("Yllw", pct(v[2])).with("Blck", pct(v[3])),
));
}
}
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index c4fe00eb9..8ecf7334f 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -166,6 +166,39 @@ fn save_round_trips_through_photoshop_events() {
assert_eq!(r["set"], "Ours");
}
+/// `image.adjustments.selectiveColor` takes a range as an array (`"reds": [c, m, y, k]`) or as
+/// the scalar form that names one range (`"colors": "reds", "cyan": 35`). Only the arrays were
+/// written, so a step recorded the other way exported as an adjustment that changes nothing, with
+/// `skipped` empty (#7672).
+#[test]
+fn a_selective_color_step_in_the_scalar_form_keeps_its_values() {
+ let back = |params: Value| -> Value {
+ let mut s = Session::new();
+ s.actions.list.push(Action::new("A", vec![("image.adjustments.selectiveColor".into(), params)]));
+ let r = s.execute("actions.save", json!({"action": "A"})).unwrap();
+ assert_eq!(r["skipped"], json!([]));
+ let imported = import(&photocraft_paint::tile::b64_decode(r["data"].as_str().unwrap()).unwrap()).unwrap();
+ imported.actions[0].steps[0].1.clone()
+ };
+
+ // The numbers are read back as numbers; a percentage may arrive as 35 or 35.0.
+ let range =
+ |step: &Value, name: &str| -> Vec<f64> { step[name].as_array().unwrap_or(&Vec::new()).iter().map(|v| v.as_f64().unwrap_or(f64::NAN)).collect() };
+
+ // The reported step: one range, one channel.
+ let scalar = back(json!({"method": "absolute", "colors": "reds", "cyan": 35}));
+ assert_eq!(scalar["method"], "absolute");
+ assert_eq!(range(&scalar, "reds"), [35.0, 0.0, 0.0, 0.0], "{scalar}");
+
+ // The scalar form over an array keeps both, as playing the step would.
+ let both = back(json!({"reds": [10, 20, 30, 40], "colors": "reds", "cyan": 35}));
+ assert_eq!(range(&both, "reds"), [35.0, 20.0, 30.0, 40.0], "{both}");
+
+ // The array form is unchanged.
+ let array = back(json!({"method": "relative", "yellows": [0, 0, -15, 5]}));
+ assert_eq!(range(&array, "yellows"), [0.0, 0.0, -15.0, 5.0], "{array}");
+}
+
#[test]
fn every_mapped_command_exports_and_imports_back() {
let cases: Vec<(&str, Value)> = vec![