mirror of
https://github.com/storytold/effectcraft.git
synced 2026-12-02 15:01:26 -05:00
Gradient opacity stops given in any order in automation JSON now look the same as in ascending order (#4280)
* Gradient stops given out of order in automation JSON sample as if in order Opacity stops of the plain form, and colour and opacity stops of the typed form, are sorted by position (stably) when coerced, as the plain form's colour stops already were. * Gradient stops at positions 0 and -0 keep the order they were given in The stop sort used total_cmp, which puts -0.0 before 0.0 although both are the same position, so a stable sort reordered such stops. Compare equal positions as equal, for colour and opacity stops in the plain and typed forms.
This commit is contained in:
@@ -196,6 +196,64 @@ fn typed_gradient_round_trips_one_stop_and_rgba() {
|
||||
assert_eq!(cur.coerce_json(&serde_json::json!({"colors": [[0.25, [0.1, 0.2, 0.3, 0.7]]]})), None, "plain JSON needs two colours");
|
||||
}
|
||||
|
||||
/// The sampler walks gradient stops in ascending position, so a gradient whose stops arrive in
|
||||
/// another order has to be sorted when it is read: it must look like the same stops in order.
|
||||
#[test]
|
||||
fn plain_gradient_opacity_stops_in_any_order_sample_alike() {
|
||||
let g = Value::Gradient(Gradient::default());
|
||||
let make = |ops: serde_json::Value| g.coerce_json(&serde_json::json!({"colors": ["#ffffff", "#ffffff"], "opacities": ops}));
|
||||
let (reversed, ascending) = (make(serde_json::json!([[1, 1], [0, 0]])), make(serde_json::json!([[0, 0], [1, 1]])));
|
||||
assert_eq!(reversed, ascending);
|
||||
let Some(Value::Gradient(r)) = reversed else { panic!("plain gradient") };
|
||||
assert!((r.sample(0.5)[3] - 0.5).abs() < 1e-6, "{r:?}");
|
||||
}
|
||||
|
||||
/// The typed form (what prop.get returns, and what scripts may send back) sorts both tracks.
|
||||
#[test]
|
||||
fn tagged_gradient_stops_in_any_order_sample_alike() {
|
||||
let g = Value::Gradient(Gradient::default());
|
||||
let unordered = Gradient { colors: vec![(1.0, [0.0, 0.0, 1.0, 1.0]), (0.0, [1.0, 0.0, 0.0, 1.0])], opacities: vec![(1.0, 1.0), (0.0, 0.0)] };
|
||||
let Some(Value::Gradient(t)) = g.coerce_json(&Value::Gradient(unordered).to_json()) else { panic!("tagged gradient") };
|
||||
assert_eq!(t.colors, vec![(0.0, [1.0, 0.0, 0.0, 1.0]), (1.0, [0.0, 0.0, 1.0, 1.0])]);
|
||||
assert_eq!(t.opacities, vec![(0.0, 0.0), (1.0, 1.0)]);
|
||||
let mid = t.sample(0.5);
|
||||
assert!((mid[0] - 0.5).abs() < 1e-6 && (mid[3] - 0.5).abs() < 1e-6, "{mid:?}");
|
||||
}
|
||||
|
||||
/// Sorting is stable: opacity stops at one position keep the order they were given in, in both
|
||||
/// forms, so a hard edge (full, then none, at 0.5) stays a hard edge after an earlier stop moves.
|
||||
#[test]
|
||||
fn gradient_opacity_stops_at_one_position_keep_their_order() {
|
||||
let g = Value::Gradient(Gradient::default());
|
||||
let given = [(0.5, 1.0_f32), (0.5, 0.0), (0.0, 1.0)];
|
||||
let plain = serde_json::json!({"colors": ["#ffffff", "#ffffff"], "opacities": given});
|
||||
let tagged = Value::Gradient(Gradient { colors: vec![(0.0, [1.0; 4]), (1.0, [1.0; 4])], opacities: given.to_vec() }).to_json();
|
||||
for (form, json) in [("plain", plain), ("tagged", tagged)] {
|
||||
let Some(Value::Gradient(r)) = g.coerce_json(&json) else { panic!("{form} gradient") };
|
||||
assert_eq!(r.opacities, vec![(0.0, 1.0), (0.5, 1.0), (0.5, 0.0)], "{form}");
|
||||
assert_eq!((r.sample(0.25)[3], r.sample(0.75)[3]), (1.0, 0.0), "{form}");
|
||||
}
|
||||
}
|
||||
|
||||
/// `-0.0` and `0.0` are one position, so stops given at them keep their order too (a plain total
|
||||
/// order would put the `-0.0` stop first and flip which of the two the sampler reads at 0).
|
||||
#[test]
|
||||
fn gradient_stops_at_signed_zero_keep_their_order() {
|
||||
let g = Value::Gradient(Gradient::default());
|
||||
let plain = serde_json::json!({"colors": [[0.0, "#ff0000"], [-0.0, "#0000ff"], [1.0, "#ffffff"]], "opacities": [[0.0, 1.0], [-0.0, 0.0], [1.0, 1.0]]});
|
||||
let tagged = Value::Gradient(Gradient {
|
||||
colors: vec![(0.0, [1.0, 0.0, 0.0, 1.0]), (-0.0, [0.0, 0.0, 1.0, 1.0]), (1.0, [1.0; 4])],
|
||||
opacities: vec![(0.0, 1.0), (-0.0, 0.0), (1.0, 1.0)],
|
||||
})
|
||||
.to_json();
|
||||
for (form, json) in [("plain", plain), ("tagged", tagged)] {
|
||||
let Some(Value::Gradient(r)) = g.coerce_json(&json) else { panic!("{form} gradient") };
|
||||
assert_eq!(r.opacities.iter().map(|s| s.1).collect::<Vec<_>>(), vec![1.0, 0.0, 1.0], "{form}");
|
||||
assert_eq!(r.colors.iter().map(|s| s.1[0]).collect::<Vec<_>>(), vec![1.0, 0.0, 1.0], "{form}");
|
||||
assert_eq!(r.sample(0.0)[3], 1.0, "{form}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linear_and_hold() {
|
||||
let keys = vec![Keyframe::new(s(0.0), Value::Scalar(0.0)), Keyframe::new(s(2.0), Value::Scalar(100.0))];
|
||||
|
||||
@@ -230,6 +230,12 @@ fn sample_stops<T: Copy>(stops: &[(f64, T)], t: f64, lerp: impl Fn(T, T, f64) ->
|
||||
stops.last().map(|s| s.1)
|
||||
}
|
||||
|
||||
/// Orders two gradient stop positions for a stable sort. `-0.0` and `0.0` are the same position
|
||||
/// (`total_cmp` would put `-0.0` first and so reorder stops given at one position).
|
||||
fn cmp_position(a: f64, b: f64) -> std::cmp::Ordering {
|
||||
if a == b { std::cmp::Ordering::Equal } else { a.total_cmp(&b) }
|
||||
}
|
||||
|
||||
/// A gradient written the way people (and agents) write it: `{colors: [[pos, color]…],
|
||||
/// opacities?: [[pos, alpha]…]}`, `[[pos, color]…]` or `[color, color…]` (evenly spaced), where a
|
||||
/// colour is `"#rrggbb"` or `[r, g, b, a?]` (0–1). Opacity stops default to the colours' alpha.
|
||||
@@ -281,8 +287,10 @@ fn gradient_json(j: &serde_json::Value) -> Option<Gradient> {
|
||||
_ => return None,
|
||||
};
|
||||
let mut colors = colors;
|
||||
colors.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
let opacities = opacities.unwrap_or_else(|| colors.iter().map(|(p, c)| (*p, c[3])).collect());
|
||||
colors.sort_by(|a, b| cmp_position(a.0, b.0));
|
||||
let mut opacities = opacities.unwrap_or_else(|| colors.iter().map(|(p, c)| (*p, c[3])).collect());
|
||||
// The sampler reads stops in ascending position. The sort is stable: stops at one position keep their order.
|
||||
opacities.sort_by(|a, b| cmp_position(a.0, b.0));
|
||||
Some(Gradient { colors: colors.into_iter().map(|(p, c)| (p, [c[0], c[1], c[2], 1.0])).collect(), opacities })
|
||||
}
|
||||
|
||||
@@ -479,6 +487,15 @@ impl Value {
|
||||
Value::Gradient(_) => serde_json::from_value::<Value>(j.clone())
|
||||
.ok()
|
||||
.filter(|v| matches!(v, Value::Gradient(g) if g.colors.len() <= MAX_GRADIENT_STOPS && g.opacities.len() <= MAX_GRADIENT_STOPS))
|
||||
.map(|v| match v {
|
||||
// Stops given out of order sample as if in order, as the plain form's do.
|
||||
Value::Gradient(mut g) => {
|
||||
g.colors.sort_by(|a, b| cmp_position(a.0, b.0));
|
||||
g.opacities.sort_by(|a, b| cmp_position(a.0, b.0));
|
||||
Value::Gradient(g)
|
||||
}
|
||||
v => v,
|
||||
})
|
||||
.or_else(|| gradient_json(j).map(Value::Gradient)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user