Skip to content

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

Actions: a Paint Bucket step keeps its explicit colour through .atn (#7199) (#7208)

Korg1ch84f326fa259d
diff --git a/crates/engine/src/atn_cmds.rs b/crates/engine/src/atn_cmds.rs
index ebeda4868..fa86e292f 100644
--- a/crates/engine/src/atn_cmds.rs
+++ b/crates/engine/src/atn_cmds.rs
@@ -1986,6 +1986,10 @@ fn fill_from(d: &Descriptor) -> Mapped {
match get_enum(d, "Usng").as_deref() {
Some("FrgC") | None => {}
Some("Pttn") => p["contents"] = json!("pattern"),
+ Some("Clr") => match get_obj(d, "Clr ").and_then(color_hex) {
+ Some(c) => p["color"] = json!(c),
+ None => return Mapped::Unsupported { harmless: false },
+ },
Some(_) => return Mapped::Unsupported { harmless: false },
}
if let Some(t) = get_num(d, "Tlrn") {
@@ -2032,7 +2036,16 @@ fn fill_to(id: &str, p: &Value) -> Option<Descriptor> {
if id == "paint.bucket" {
let mut d = desc("null").with("From", D::Descriptor(desc("Pnt ").with("Hrzn", px(jnum(p, "x")?)).with("Vrtc", px(jnum(p, "y")?))));
d = d.with("Tlrn", long(jnum(p, "tolerance").unwrap_or(32.0))).with("AntA", D::Boolean(jbool(p, "antiAlias").unwrap_or(true)));
- d = d.with("Usng", enm("FlCn", if jstr(p, "contents") == Some("pattern") { "Pttn" } else { "FrgC" }));
+ // `source` is the command's older name for `contents`.
+ d = if p.get("contents").or_else(|| p.get("source")).and_then(Value::as_str) == Some("pattern") {
+ d.with("Usng", enm("FlCn", "Pttn"))
+ } else {
+ // An explicit colour overrides the foreground (#7199): it travels as Fill's `Clr `.
+ match bucket_color(p.get("color"))? {
+ Some(c) => d.with("Usng", enm("FlCn", "Clr ")).with("Clr ", D::Descriptor(c)),
+ None => d.with("Usng", enm("FlCn", "FrgC")),
+ }
+ };
if let Some(o) = jnum(p, "opacity") {
d = d.with("Opct", pct(o));
}
@@ -2054,6 +2067,29 @@ fn fill_to(id: &str, p: &Value) -> Option<Descriptor> {
Some(d)
}
+/// The Paint Bucket's `color` as `paint.bucket` reads it (`[r, g, b(, a)]` in 0..=1 or
+/// `#rrggbb(aa)`): `Some(None)` when it fills with the foreground (no colour, or a string the
+/// command doesn't read), `None` when the colour can't be written (not opaque, or outside 0..=1).
+fn bucket_color(v: Option<&Value>) -> Option<Option<Descriptor>> {
+ let rgba = match v {
+ Some(Value::Array(a)) if a.len() >= 3 => {
+ let c: Vec<f64> = a.iter().map(|x| x.as_f64().unwrap_or(0.0)).collect();
+ [*c.first()?, *c.get(1)?, *c.get(2)?, c.get(3).copied().unwrap_or(1.0)]
+ }
+ Some(Value::String(h)) => {
+ let h = h.trim_start_matches('#');
+ let c = |i: usize| h.get(i..i + 2).and_then(|x| u8::from_str_radix(x, 16).ok()).map(|v| f64::from(v) / 255.0);
+ let (Some(r), Some(g), Some(b)) = (c(0), c(2), c(4)) else { return Some(None) };
+ [r, g, b, c(6).unwrap_or(1.0)]
+ }
+ _ => return Some(None),
+ };
+ if rgba[3] != 1.0 || !rgba.iter().all(|v| (0.0..=1.0).contains(v)) {
+ return None;
+ }
+ Some(Some(desc("RGBC").with("Rd ", D::Double(rgba[0] * 255.0)).with("Grn ", D::Double(rgba[1] * 255.0)).with("Bl ", D::Double(rgba[2] * 255.0))))
+}
+
const STROKE: &[F] = &[
F("Wdth", "width", Kind::Long),
F("Lctn", "location", Kind::Enum("StrL", STROKE_LOCATION)),
diff --git a/crates/engine/src/atn_cmds/tests.rs b/crates/engine/src/atn_cmds/tests.rs
index 871300372..980d5eb9e 100644
--- a/crates/engine/src/atn_cmds/tests.rs
+++ b/crates/engine/src/atn_cmds/tests.rs
@@ -432,6 +432,55 @@ fn every_mapped_command_exports_and_imports_back() {
}
}
+/// `paint.bucket` exported to .atn and imported back → its params, or None when it doesn't export.
+fn bucket_round_trip(params: Value) -> Option<Value> {
+ let item = export_item("paint.bucket", &params, "S")?;
+ let e = event_for(&item.event)?;
+ match (e.from)(item.descriptor.as_ref()?) {
+ Mapped::Steps(steps) => steps.first().map(|(id, p)| {
+ assert_eq!(id, "paint.bucket");
+ p.clone()
+ }),
+ _ => None,
+ }
+}
+
+/// A Paint Bucket step with an explicit colour keeps it through .atn (#7199): replayed on a fresh
+/// document, whose foreground is black, it still fills red. A colour .atn can't hold makes the
+/// step not export rather than fill with the foreground.
+#[test]
+fn bucket_keeps_an_explicit_color_through_atn() {
+ let fill = |params: &Value| {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 8, "height": 8, "background": "#ffffff"})).unwrap();
+ s.execute("paint.bucket", params.clone()).unwrap();
+ let d = s.active().unwrap();
+ d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().pixel(2, 2)
+ };
+ let red = json!({"x": 2, "y": 2, "antiAlias": false, "color": "#ff0000"});
+ let back = bucket_round_trip(red.clone()).unwrap();
+ assert_eq!(back["color"], "#ff0000", "{back}");
+ assert_eq!(fill(&back), fill(&red));
+ assert_eq!(fill(&back), vec![1.0, 0.0, 0.0, 1.0]);
+ // The colour as an array of channels.
+ assert_eq!(bucket_round_trip(json!({"x": 1, "y": 1, "color": [0.0, 0.5, 1.0]})).unwrap()["color"], "#0080ff");
+ // No colour, or one the command can't read: the foreground, as before.
+ for p in [json!({"x": 1, "y": 1}), json!({"x": 1, "y": 1, "color": "red"})] {
+ let back = bucket_round_trip(p.clone()).unwrap();
+ assert!(back.get("color").is_none() && back.get("contents").is_none(), "{p} → {back}");
+ }
+ // A pattern fill ignores the colour, also when named with the older `source`.
+ for key in ["contents", "source"] {
+ let back = bucket_round_trip(json!({"x": 1, "y": 1, key: "pattern", "color": "#ff0000"})).unwrap();
+ assert_eq!(back["contents"], "pattern", "{key}: {back}");
+ assert!(back.get("color").is_none(), "{key}: {back}");
+ }
+ // Translucent or out-of-range colours have no .atn form.
+ for c in [json!("#ff000080"), json!([1.0, 0.0, 0.0, 0.5]), json!([2.0, 0.0, 0.0])] {
+ assert!(bucket_round_trip(json!({"x": 1, "y": 1, "color": c})).is_none(), "{c}");
+ }
+}
+
#[test]
fn unsupported_shapes_become_photoshop_steps_not_errors() {
// Image size in two different percentages, a Paint Bucket click in percent of the canvas, a