mirror of
https://github.com/storytold/designcraft.git
synced 2026-10-24 10:16:59 -06:00
Clamp object.stroke tint and gapTint to 0..1
Found while testing crates/engine/src/cmd/object.rs:299-313 (object.stroke: `st.tint = v as f32`, `st.gap_tint = v as f32`; the no-selection path at :273 clamps, this one doesn't) Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Haiku 5.5
parent
ecd7e5d063
commit
103d7eb3ff
@@ -297,7 +297,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
st.swatch = v.into();
|
||||
}
|
||||
if let Some(v) = p2.get("tint").and_then(Value::as_f64) {
|
||||
st.tint = v as f32;
|
||||
st.tint = v.clamp(0.0, 1.0) as f32;
|
||||
}
|
||||
if let Some(v) = p2.get("weight").and_then(Value::as_f64) {
|
||||
st.weight = v.max(0.0);
|
||||
@@ -309,7 +309,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
st.gap_swatch = v.into();
|
||||
}
|
||||
if let Some(v) = p2.get("gapTint").and_then(Value::as_f64) {
|
||||
st.gap_tint = v as f32;
|
||||
st.gap_tint = v.clamp(0.0, 1.0) as f32;
|
||||
}
|
||||
if let Some(v) = p2.get("gapOverprint").and_then(Value::as_bool) {
|
||||
st.gap_overprint = v;
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
//! `object.stroke` keeps `tint` and `gapTint` within 0..1, so a saved document always reloads.
|
||||
use designcraft_doc::ItemId;
|
||||
use designcraft_engine::Session;
|
||||
use serde_json::json;
|
||||
|
||||
fn stroked(tint: f64, gap_tint: f64) -> (Session, ItemId) {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({})).unwrap();
|
||||
let created = s.execute("frame.create", &json!({"rect": [36, 36, 200, 200]})).unwrap();
|
||||
let id = ItemId(created["id"].as_u64().unwrap());
|
||||
s.execute("object.stroke", &json!({"ids": [id.0], "swatch": "[Black]", "tint": tint, "gapTint": gap_tint})).unwrap();
|
||||
(s, id)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn out_of_range_stroke_tints_are_clamped_and_the_saved_file_reloads() {
|
||||
for (tint, gap, want_tint, want_gap) in [(1e300, 1e300, 1.0, 1.0), (2.0, -1.0, 1.0, 0.0), (-1e300, 0.5, 0.0, 0.5)] {
|
||||
let (s, id) = stroked(tint, gap);
|
||||
let doc = &s.doc().unwrap().doc;
|
||||
let st = &doc.item(id).unwrap().stroke;
|
||||
assert_eq!((st.tint, st.gap_tint), (want_tint, want_gap), "tint {tint}, gapTint {gap}");
|
||||
let bytes = designcraft_format::save(doc).unwrap();
|
||||
designcraft_format::load(&bytes).expect("reload what we just saved");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user