Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge t13-gradients: gradient PRs #765 #763 #826 #823 #825

aiongga4ec5b31d6c5
diff --cc crates/engine/src/cmd/object.rs
index 59fb91075,964e12078..fcb2315d1
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@@ -3545,31 -3524,40 +3548,63 @@@ mod gradient_tests
assert_eq!(fill(&s).gradient_vector, None);
assert_eq!(fill(&s).gradient_angle, Some(90.0));
}
+
+ #[test]
+ fn swatches_dropped_on_stops_keep_their_cmyk() {
+ use designcraft_color::Color;
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let r = s.execute("frame.create", &json!({"rect": [100, 100, 300, 200]})).unwrap();
+ let id = ItemId(r["id"].as_u64().unwrap());
+ // LYH Studio episode 6: magenta on the left stop, cyan dropped at 68.636 %, a grey third.
+ let r = s
+ .execute(
+ "object.gradient",
+ &json!({"stops": [
+ {"location": 0, "swatch": "C=0 M=100 Y=0 K=0"},
+ {"location": 68.636, "swatch": "C=100 M=0 Y=0 K=0"},
+ {"location": 100, "color": {"gray": 25}}
+ ]}),
+ )
+ .unwrap();
+ let d = &s.doc().unwrap().doc;
+ let g = designcraft_color::swatch::resolve_gradient(&d.swatches, r["swatch"].as_str().unwrap()).unwrap();
+ assert_eq!(d.item(id).unwrap().fill.swatch, r["swatch"].as_str().unwrap());
+ assert_eq!(g.stops[0].color, Color::cmyk(0.0, 1.0, 0.0, 0.0));
+ assert_eq!(g.stops[1].color, Color::cmyk(1.0, 0.0, 0.0, 0.0));
+ assert!((g.stops[1].offset - 0.68636).abs() < 1e-6);
+ assert_eq!(g.stops[2].color, Color::gray(0.25));
+ // Not a colour: refused, nothing changes.
+ for bad in ["[None]", "Missing", r["swatch"].as_str().unwrap()] {
+ let p = json!({"stops": [{"location": 0, "swatch": bad}, {"location": 100, "color": "#000000"}]});
+ assert!(s.execute("object.gradient", &p).is_err(), "{bad}");
+ }
+ }
}
+#[cfg(test)]
+mod reference_point_tests {
+ use super::*;
+
+ #[test]
+ fn rotate_and_flip_turn_about_the_reference_point() {
+ let mut s = Session::new();
+ s.execute("file.new", &json!({})).unwrap();
+ let id = ItemId(s.execute("frame.create", &json!({"rect": [100, 100, 200, 150]})).unwrap()["id"].as_u64().unwrap());
+ let item = |s: &Session| s.doc().unwrap().doc.item(id).unwrap().clone();
+ // 90° counterclockwise about the top-left: that corner stays, the frame swings up.
+ s.execute("transform.rotate", &json!({"angle": 90, "ref": 0, "ids": [id.0]})).unwrap();
+ let it = item(&s);
+ assert!((it.xf * Point::new(100.0, 100.0) - Point::new(100.0, 100.0)).hypot() < 1e-9, "{:?}", it.xf);
+ let b = it.bounds();
+ assert!((b.x0 - 100.0).abs() < 1e-9 && (b.y0 - 0.0).abs() < 1e-9 && (b.x1 - 150.0).abs() < 1e-9 && (b.y1 - 100.0).abs() < 1e-9, "{b:?}");
+ // Flip horizontally about the right edge's middle: the frame lands to the right of it.
+ s.execute("transform.flip", &json!({"axis": "horizontal", "ref": 5, "ids": [id.0]})).unwrap();
+ let b = item(&s).bounds();
+ assert!((b.x0 - 150.0).abs() < 1e-9 && (b.x1 - 200.0).abs() < 1e-9 && (b.y0 - 0.0).abs() < 1e-9, "{b:?}");
+ }
+}
+
#[cfg(test)]
mod again_tests {
use super::*;