authoravatarclover caruso<git@paperclover.net>2026-11-30 19:35:27-08:00
committeravatarclover caruso<git@paperclover.net>2026-11-30 19:55:45-08:00
log993d5986f0810126bd183704c7983593ba0d8ce8
tree3cc6dfacd309e5a76de82ca689897ec1943f0ed8
parentdb8e9f2e18b100438979c989ef5c3d034553b645
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

Fix BezierSpline Loop pre-first-keyframe clamping and spline/SourceOp parity (#45)


2 files changed, 398 insertions(+), 23 deletions(-)

crates/model/src/comp.rs+173-1
...@@ -668,6 +668,34 @@ impl Comp {...@@ -668,6 +668,34 @@ impl Comp {
668 }668 }
669 }669 }
670670
671 // Apply cyclic [0..6] endpoint continuity on LUTBezier splines attached to HueCurves nodes.
672 const HUE_CURVE_PORTS: &[&str] = &[
673 "Hue",
674 "Sat",
675 "Lum",
676 "Red",
677 "Green",
678 "Blue",
679 "RedSuppress",
680 "GreenSuppress",
681 "BlueSuppress",
682 ];
683 let mut hue_spline_names: HashSet<String> = HashSet::new();
684 for n in &nodes {
685 if n.kind == "HueCurves" {
686 for &port in HUE_CURVE_PORTS {
687 if let Some(sp_name) = n.connected_node(port) {
688 hue_spline_names.insert(sp_name.to_owned());
689 }
690 }
691 }
692 }
693 for sp_name in hue_spline_names {
694 if let Some(sp) = splines.get_mut(&sp_name) {
695 sp.apply_hue_curves_cyclic_ends();
696 }
697 }
698
671 // Infer comp dimensions from first Background/TextPlus if Prefs omitted Width/Height699 // Infer comp dimensions from first Background/TextPlus if Prefs omitted Width/Height
672 if width == 1920 && height == 1080 {700 if width == 1920 && height == 1080 {
673 for n in &nodes {701 for n in &nodes {
...@@ -2199,7 +2227,12 @@ fn sync_spline_table(spline_name: &str, spline: &BezierSpline, tbl: &mut LuaTabl...@@ -2199,7 +2227,12 @@ fn sync_spline_table(spline_name: &str, spline: &BezierSpline, tbl: &mut LuaTabl
2199 if existing.color != spline.color {2227 if existing.color != spline.color {
2200 tbl.set(LuaKey::Str("SplineColor".to_owned()), spline_color_lua(spline.color));2228 tbl.set(LuaKey::Str("SplineColor".to_owned()), spline_color_lua(spline.color));
2201 }2229 }
2202 if existing.keyframes != spline.keyframes {2230 let unchanged_cyclic = tbl.tag.as_deref() == Some("LUTBezier") && {
2231 let mut normalized = existing.clone();
2232 normalized.apply_hue_curves_cyclic_ends();
2233 normalized.keyframes == spline.keyframes
2234 };
2235 if existing.keyframes != spline.keyframes && !unchanged_cyclic {
2203 let new_kfs = LuaValue::Table(build_keyframes_table(spline));2236 let new_kfs = LuaValue::Table(build_keyframes_table(spline));
2204 if tbl.tag.as_deref() == Some("LUTBezier") && tbl.get_table("KeyColorSplines").is_some() {2237 if tbl.tag.as_deref() == Some("LUTBezier") && tbl.get_table("KeyColorSplines").is_some() {
2205 if let Some(kcs) = tbl.get_table_mut("KeyColorSplines") {2238 if let Some(kcs) = tbl.get_table_mut("KeyColorSplines") {
...@@ -3301,4 +3334,143 @@ mod expression_modifier_tests {...@@ -3301,4 +3334,143 @@ mod expression_modifier_tests {
3301 assert_eq!(comp.eval_text(node, "StyledText", 10.0, "", &expr), "first line");3334 assert_eq!(comp.eval_text(node, "StyledText", 10.0, "", &expr), "first line");
3302 assert_eq!(comp.eval_text(node, "StyledText", 25.0, "", &expr), "second line");3335 assert_eq!(comp.eval_text(node, "StyledText", 25.0, "", &expr), "second line");
3303 }3336 }
3337
3338 #[test]
3339 fn source_op_inherits_all_color_corrector_params_and_respects_blend_override() {
3340 // Mirrors repro_seed_110: N4 inherits Wheel*/TintMode/*RGBGain/PreDividePostMultiply
3341 // from N2 while keeping its own `Input` wire and `Blend = 0.75` override.
3342 let src = r#"Composition {
3343 Tools = ordered() {
3344 S0 = Loader {},
3345 S1 = DaySky {},
3346 N2 = ColorCorrector {
3347 Inputs = {
3348 Input = Input { SourceOp = "S0", Source = "Output", },
3349 WheelHue1 = Input { Value = -0.235, },
3350 WheelSaturation1 = Input { Value = 0.65, },
3351 WheelTintAngle1 = Input { Value = 0.323, },
3352 WheelTintLength1 = Input { Value = 0.67, },
3353 TintMode = Input { Value = 0, },
3354 MasterRGBGain = Input { Value = 0.65, },
3355 ShadowsRGBGain = Input { Value = 0.84, },
3356 HighlightsRGBGain = Input { Value = 0.75, },
3357 PreDividePostMultiply = Input { Value = 1, },
3358 Blend = Input { Value = 1.0, },
3359 },
3360 },
3361 N4 = ColorCorrector {
3362 SourceOp = "N2",
3363 Inputs = {
3364 Input = Input { SourceOp = "S1", Source = "Output", },
3365 Blend = Input { Value = 0.75, },
3366 },
3367 },
3368 N5 = ColorCorrector {
3369 SourceOp = "N2",
3370 Inputs = {
3371 Input = Input { SourceOp = "S1", Source = "Output", },
3372 },
3373 },
3374 },
3375 }"#;
3376 let comp = Comp::from_fusion_str(src).expect("parse seed 110 style SourceOp");
3377 let expr = ExprEngine::new();
3378 let n4 = comp.node(comp.find_node("N4").unwrap());
3379 assert_eq!(n4.connected_node("Input"), Some("S1"));
3380 assert!((comp.eval_number(n4, "Blend", 1.0, 1.0, &expr) - 0.75).abs() < 1e-6);
3381 assert!((comp.eval_number(n4, "WheelHue1", 1.0, 0.0, &expr) - (-0.235)).abs() < 1e-6);
3382 assert!((comp.eval_number(n4, "WheelSaturation1", 1.0, 1.0, &expr) - 0.65).abs() < 1e-6);
3383 assert!((comp.eval_number(n4, "WheelTintAngle1", 1.0, 0.0, &expr) - 0.323).abs() < 1e-6);
3384 assert!((comp.eval_number(n4, "WheelTintLength1", 1.0, 0.0, &expr) - 0.67).abs() < 1e-6);
3385 assert!((comp.eval_number(n4, "TintMode", 1.0, 2.0, &expr) - 0.0).abs() < 1e-6);
3386 assert!((comp.eval_number(n4, "MasterRGBGain", 1.0, 1.0, &expr) - 0.65).abs() < 1e-6);
3387 assert!((comp.eval_number(n4, "ShadowsRGBGain", 1.0, 1.0, &expr) - 0.84).abs() < 1e-6);
3388 assert!((comp.eval_number(n4, "HighlightsRGBGain", 1.0, 1.0, &expr) - 0.75).abs() < 1e-6);
3389 assert!((comp.eval_number(n4, "PreDividePostMultiply", 1.0, 0.0, &expr) - 1.0).abs() < 1e-6);
3390
3391 let n5 = comp.node(comp.find_node("N5").unwrap());
3392 assert!((comp.eval_number(n5, "Blend", 1.0, 0.0, &expr) - 1.0).abs() < 1e-6);
3393 }
3394
3395 #[test]
3396 fn hue_curves_lut_bezier_applies_cyclic_ends_and_roundtrips_cleanly() {
3397 let src = r#"Composition {
3398 Tools = ordered() {
3399 Bg = Background {},
3400 HC_RSup = LUTBezier {
3401 SmoothEndsTransition = true,
3402 KeyColorSplines = {
3403 [0] = {
3404 [0] = { 0.5 },
3405 [2] = { 1.0 },
3406 [6] = { 0.5 },
3407 }
3408 },
3409 },
3410 CC_Red = LUTBezier {
3411 SmoothEndsTransition = true,
3412 KeyColorSplines = {
3413 [0] = {
3414 [0.0] = { 0.0 },
3415 [0.5] = { 0.8 },
3416 [1.0] = { 0.2 },
3417 }
3418 },
3419 },
3420 HC1 = HueCurves {
3421 Inputs = {
3422 Input = Input { SourceOp = "Bg", Source = "Output", },
3423 RedSuppress = Input { SourceOp = "HC_RSup", Source = "Value", },
3424 },
3425 },
3426 CC1 = ColorCurves {
3427 Inputs = {
3428 Input = Input { SourceOp = "Bg", Source = "Output", },
3429 Red = Input { SourceOp = "CC_Red", Source = "Value", },
3430 },
3431 },
3432 },
3433 }"#;
3434 let comp = Comp::from_fusion_str(src).expect("parse HueCurves + ColorCurves");
3435 let rsup = comp.splines.get("HC_RSup").expect("HC_RSup");
3436 assert!(rsup.keyframes[2].lh.is_some());
3437 assert!((rsup.eval(5.0) - 0.4140625).abs() < 1e-3);
3438
3439 // ColorCurves [0..1] must NOT get cyclic HueCurves wrapping even with SmoothEndsTransition = true.
3440 let cc_red = comp.splines.get("CC_Red").expect("CC_Red");
3441 assert!((cc_red.eval(1.0) - 0.2).abs() < 1e-6);
3442 assert!((cc_red.eval(0.75) - 0.5).abs() < 1e-6);
3443
3444 // Lossless roundtrip keeps KeyColorSplines untouched when unedited.
3445 let saved = comp.to_fusion_string();
3446 assert!(!saved.contains("LH ="));
3447 let reloaded = Comp::from_fusion_str(&saved).expect("reload");
3448 assert!((reloaded.splines["HC_RSup"].eval(5.0) - rsup.eval(5.0)).abs() < 1e-9);
3449 }
3450
3451 #[test]
3452 fn comp_eval_number_clamps_loop_spline_before_first_keyframe() {
3453 let src = r#"Composition {
3454 Tools = ordered() {
3455 CG = ColorGain {
3456 Inputs = {
3457 HighHue = Input { SourceOp = "ColorGain1HighHue", Source = "Value", },
3458 },
3459 },
3460 ColorGain1HighHue = BezierSpline {
3461 KeyFrames = {
3462 [4129] = { 0, RH = { 4137.66666666667, 0.333333333333333 }, Flags = { Linear = true, Loop = true } },
3463 [4155] = { 1, LH = { 4146.33333333333, 0.666666666666667 }, Flags = { Linear = true, Loop = true } },
3464 },
3465 },
3466 },
3467 }"#;
3468 let comp = Comp::from_fusion_str(src).expect("parse underwater loop spline");
3469 let expr = ExprEngine::new();
3470 let cg = comp.node(comp.find_node("CG").unwrap());
3471 assert!((comp.eval_number(cg, "HighHue", 3933.0, 0.0, &expr) - 0.0).abs() < 1e-9);
3472 assert!((comp.eval_number(cg, "HighHue", 4142.0, 0.0, &expr) - 0.5).abs() < 1e-9);
3473 assert!((comp.eval_number(cg, "HighHue", 4155.0, 0.0, &expr) - 0.0).abs() < 1e-9);
3474 assert!((comp.eval_number(cg, "HighHue", 4168.0, 0.0, &expr) - 0.5).abs() < 1e-9);
3475 }
3304}3476}
crates/model/src/spline.rs+225-22
...@@ -132,30 +132,33 @@ impl BezierSpline {...@@ -132,30 +132,33 @@ impl BezierSpline {
132 let first = &kfs[0];132 let first = &kfs[0];
133 let last = &kfs[kfs.len() - 1];133 let last = &kfs[kfs.len() - 1];
134134
135 // Handle loop / relative-loop (`LoopRel`) / ping-pong if set on first or last keyframe135 // Fusion never loops backwards before the first keyframe (`Loop` / `LoopRel` / `PingPong` all clamp).
136 if t <= first.time {
137 return first.value;
138 }
139
140 // In Fusion, `Loop` / `LoopRel` requires at least two loop-flagged keyframes (`loop_start < loop_end`).
141 // The loop repeats `[loop_start, loop_end]` for `t >= loop_end.time` up to the next keyframe (if any).
142 let is_loop_key = |k: &Keyframe| k.flags.loop_after || k.flags.loop_rel;
143 let loop_pair = kfs.iter().position(is_loop_key).and_then(|s| {
144 let e = kfs.iter().rposition(is_loop_key)?;
145 (s < e).then_some((s, e))
146 });
147
136 let mut t = t;148 let mut t = t;
137 let mut value_offset = 0.0_f64;149 let mut value_offset = 0.0_f64;
138 let span = last.time - first.time;150 if let Some((ls_idx, le_idx)) = loop_pair {
139 let dv_cycle = last.value - first.value;151 let ls = &kfs[ls_idx];
140 let do_loop_after = last.flags.loop_after || last.flags.loop_rel || last.flags.ping_pong;152 let le = &kfs[le_idx];
141 let do_loop_before = first.flags.loop_after || first.flags.loop_rel || first.flags.ping_pong;153 let next_after_time = kfs.get(le_idx + 1).map(|k| k.time);
142154 let span = le.time - ls.time;
143 if span > 1e-9 && ((t > last.time && do_loop_after) || (t < first.time && do_loop_before)) {155 if span > 1e-9 && t >= le.time && next_after_time.is_none_or(|nt| t < nt) {
144 let offset = t - first.time;156 let offset = t - ls.time;
145 let cycle = (offset / span).floor();157 let cycle = (offset / span).floor();
146 let rem = offset - cycle * span;158 let rem = offset - cycle * span;
147 if (t > last.time && last.flags.ping_pong) || (t < first.time && first.flags.ping_pong) {159 t = ls.time + rem;
148 t = if (cycle as i64) & 1 == 0 {160 if ls.flags.loop_rel || le.flags.loop_rel {
149 first.time + rem161 value_offset = cycle * (le.value - ls.value);
150 } else {
151 last.time - rem
152 };
153 } else {
154 t = first.time + rem;
155 if (t > last.time || offset >= span) && (last.flags.loop_rel || first.flags.loop_rel) {
156 value_offset = cycle * dv_cycle;
157 } else if offset < 0.0 && (first.flags.loop_rel || last.flags.loop_rel) {
158 value_offset = cycle * dv_cycle;
159 }162 }
160 }163 }
161 }164 }
...@@ -174,6 +177,46 @@ impl BezierSpline {...@@ -174,6 +177,46 @@ impl BezierSpline {
174 eval_segment(k0, k1, t) + value_offset177 eval_segment(k0, k1, t) + value_offset
175 }178 }
176179
180 /// Applies Fusion `HueCurves` cyclic `[0..6]` endpoint continuity when tangent handles are omitted.
181 pub fn apply_hue_curves_cyclic_ends(&mut self) {
182 let kfs = &mut self.keyframes;
183 if kfs.len() < 2 {
184 return;
185 }
186 let first_t = kfs[0].time;
187 let last_t = kfs[kfs.len() - 1].time;
188 if (first_t - 0.0).abs() > 1e-6 || (last_t - 6.0).abs() > 1e-6 {
189 return;
190 }
191 let n = kfs.len();
192 let first_v = kfs[0].value;
193 let last = &kfs[n - 1];
194 if last.flags.linear || kfs[0].flags.step_out || last.flags.step_in {
195 if last.flags.linear && (last.value - first_v).abs() > 1e-9 {
196 kfs[n - 1].value = first_v;
197 }
198 return;
199 }
200 if kfs[0].rh.is_some() || last.lh.is_some() {
201 return;
202 }
203 let dt0 = kfs[1].time - first_t;
204 let dv0 = kfs[1].value - first_v;
205 let rh0 = [first_t + dt0 / 3.0, first_v + dv0 / 3.0];
206 // Preserve the 2-keyframe initial chord slope in `RH` before snapping `last.value`.
207 if n == 2 {
208 kfs[0].rh = Some(rh0);
209 }
210 let dt_last = last_t - kfs[n - 2].time;
211 let dx_last = dt_last / 3.0;
212 let slope0 = if dt0.abs() > 1e-12 { dv0 / dt0 } else { 0.0 };
213 kfs[n - 1].value = first_v;
214 kfs[n - 1].lh = Some([
215 last_t - dx_last,
216 first_v - slope0 * dx_last,
217 ]);
218 }
219
177 /// Returns the effective cubic Bezier control points `[p0, p1, p2, p3]` for segment `idx -> idx+1`.220 /// Returns the effective cubic Bezier control points `[p0, p1, p2, p3]` for segment `idx -> idx+1`.
178 pub fn segment_points(&self, idx: usize) -> Option<[[f64; 2]; 4]> {221 pub fn segment_points(&self, idx: usize) -> Option<[[f64; 2]; 4]> {
179 let k0 = self.keyframes.get(idx)?;222 let k0 = self.keyframes.get(idx)?;
...@@ -489,3 +532,163 @@ pub fn read_vec2(t: &LuaTable) -> Option<[f64; 2]> {...@@ -489,3 +532,163 @@ pub fn read_vec2(t: &LuaTable) -> Option<[f64; 2]> {
489 let y = t.get_index(2)?.as_f64()?;532 let y = t.get_index(2)?.as_f64()?;
490 Some([x, y])533 Some([x, y])
491}534}
535
536#[cfg(test)]
537mod tests {
538 use super::*;
539
540 fn kf(time: f64, value: f64, flags: KeyFlags) -> Keyframe {
541 Keyframe {
542 time,
543 value,
544 lh: None,
545 rh: None,
546 flags,
547 handle_mode: if flags.linear { HandleMode::Vector } else { HandleMode::Free },
548 }
549 }
550
551 fn spline(keyframes: Vec<Keyframe>) -> BezierSpline {
552 BezierSpline {
553 name: "test".to_owned(),
554 color: [255, 255, 255],
555 keyframes,
556 }
557 }
558
559 #[test]
560 fn loop_clamps_before_first_keyframe_and_loops_after_last() {
561 let loop_flags = KeyFlags { linear: true, loop_after: true, ..Default::default() };
562 let sp = spline(vec![
563 kf(4129.0, 0.0, loop_flags),
564 kf(4155.0, 1.0, loop_flags),
565 ]);
566 // Pre-first keyframe must clamp to first.value (54_underwater ColorGain1HighHue @ 3933).
567 assert!((sp.eval(3933.0) - 0.0).abs() < 1e-9);
568 assert!((sp.eval(4129.0) - 0.0).abs() < 1e-9);
569 assert!((sp.eval(4142.0) - 0.5).abs() < 1e-9);
570 // At and after last.time, Loop wraps to first.time.
571 assert!((sp.eval(4155.0) - 0.0).abs() < 1e-9);
572 assert!((sp.eval(4168.0) - 0.5).abs() < 1e-9);
573 assert!((sp.eval(4181.0) - 0.0).abs() < 1e-9);
574 }
575
576 #[test]
577 fn loop_requires_both_endpoints_flagged() {
578 let lin = KeyFlags { linear: true, ..Default::default() };
579 let loop_f = KeyFlags { linear: true, loop_after: true, ..Default::default() };
580
581 let first_only = spline(vec![kf(10.0, 0.2, loop_f), kf(20.0, 0.8, lin)]);
582 assert!((first_only.eval(0.0) - 0.2).abs() < 1e-9);
583 assert!((first_only.eval(25.0) - 0.8).abs() < 1e-9);
584
585 let last_only = spline(vec![kf(10.0, 0.2, lin), kf(20.0, 0.8, loop_f)]);
586 assert!((last_only.eval(0.0) - 0.2).abs() < 1e-9);
587 assert!((last_only.eval(25.0) - 0.8).abs() < 1e-9);
588 }
589
590 #[test]
591 fn loop_rel_clamps_before_first_and_accumulates_after_last() {
592 let lin = KeyFlags { linear: true, ..Default::default() };
593 let rel_both = KeyFlags { linear: true, loop_after: true, loop_rel: true, ..Default::default() };
594 let sp = spline(vec![
595 kf(10.0, 0.4, rel_both),
596 kf(20.0, 0.6, rel_both),
597 ]);
598 assert!((sp.eval(0.0) - 0.4).abs() < 1e-9);
599 assert!((sp.eval(15.0) - 0.5).abs() < 1e-9);
600 assert!((sp.eval(20.0) - 0.6).abs() < 1e-9);
601 assert!((sp.eval(25.0) - 0.7).abs() < 1e-9);
602 assert!((sp.eval(35.0) - 0.9).abs() < 1e-9);
603
604 // LoopRel without Loop flag still loops relatively when both endpoints are marked.
605 let rel_only = KeyFlags { linear: true, loop_rel: true, ..Default::default() };
606 let sp2 = spline(vec![kf(10.0, 0.4, rel_only), kf(20.0, 0.6, rel_only)]);
607 assert!((sp2.eval(0.0) - 0.4).abs() < 1e-9);
608 assert!((sp2.eval(25.0) - 0.7).abs() < 1e-9);
609
610 // Single-endpoint LoopRel does not loop.
611 let single = spline(vec![kf(10.0, 0.4, lin), kf(20.0, 0.6, rel_both)]);
612 assert!((single.eval(25.0) - 0.6).abs() < 1e-9);
613 }
614
615 #[test]
616 fn ping_pong_flag_semantics_match_fusion_21() {
617 let pp = KeyFlags { linear: true, ping_pong: true, ..Default::default() };
618 let sp_pp = spline(vec![kf(10.0, 0.2, pp), kf(20.0, 0.8, pp)]);
619 assert!((sp_pp.eval(0.0) - 0.2).abs() < 1e-9);
620 assert!((sp_pp.eval(15.0) - 0.5).abs() < 1e-9);
621 assert!((sp_pp.eval(25.0) - 0.8).abs() < 1e-9);
622
623 let loop_pp = KeyFlags { linear: true, loop_after: true, ping_pong: true, ..Default::default() };
624 let sp_lpp = spline(vec![kf(10.0, 0.2, loop_pp), kf(20.0, 0.8, loop_pp)]);
625 assert!((sp_lpp.eval(0.0) - 0.2).abs() < 1e-9);
626 assert!((sp_lpp.eval(20.0) - 0.2).abs() < 1e-9);
627 assert!((sp_lpp.eval(25.0) - 0.5).abs() < 1e-9);
628 }
629
630 #[test]
631 fn subrange_loop_repeats_between_flagged_keyframes_until_next_key() {
632 let lin = KeyFlags { linear: true, ..Default::default() };
633 let lp = KeyFlags { linear: true, loop_after: true, ..Default::default() };
634 let sp = spline(vec![
635 kf(10.0, 0.2, lp),
636 kf(15.0, 0.7, lp),
637 kf(25.0, 0.9, lin),
638 ]);
639 assert!((sp.eval(5.0) - 0.2).abs() < 1e-9);
640 assert!((sp.eval(13.0) - 0.5).abs() < 1e-9);
641 assert!((sp.eval(15.0) - 0.2).abs() < 1e-9);
642 assert!((sp.eval(18.0) - 0.5).abs() < 1e-9);
643 assert!((sp.eval(23.0) - 0.5).abs() < 1e-9);
644 assert!((sp.eval(25.0) - 0.9).abs() < 1e-9);
645 assert!((sp.eval(27.0) - 0.9).abs() < 1e-9);
646
647 let tail_loop = spline(vec![
648 kf(10.0, 0.2, lin),
649 kf(15.0, 0.9, lp),
650 kf(20.0, 0.5, lp),
651 ]);
652 assert!((tail_loop.eval(0.0) - 0.2).abs() < 1e-9);
653 assert!((tail_loop.eval(13.0) - 0.62).abs() < 1e-9);
654 assert!((tail_loop.eval(20.0) - 0.9).abs() < 1e-9);
655 assert!((tail_loop.eval(23.0) - 0.66).abs() < 1e-9);
656 assert!((tail_loop.eval(33.0) - 0.66).abs() < 1e-9);
657 }
658
659 #[test]
660 fn apply_hue_curves_cyclic_ends_wraps_last_handle_and_preserves_explicit_handles() {
661 // [0]=0.5, [2]=1.0, [6]=0.5 (HueCurves RedSuppress curve):
662 // slope0 = (1.0 - 0.5)/2 = +0.25, dt_last = 4 -> dx_last = 4/3, dy = 1/3 -> lh = (14/3, 1/6)
663 let smooth = KeyFlags::default();
664 let mut rsup = spline(vec![
665 kf(0.0, 0.5, smooth),
666 kf(2.0, 1.0, smooth),
667 kf(6.0, 0.5, smooth),
668 ]);
669 rsup.apply_hue_curves_cyclic_ends();
670 let last_lh = rsup.keyframes[2].lh.expect("cyclic last.lh");
671 assert!((last_lh[0] - (6.0 - 4.0 / 3.0)).abs() < 1e-9);
672 assert!((last_lh[1] - (0.5 - 1.0 / 3.0)).abs() < 1e-9);
673 // At h=5.0 (u=0.75 on [2..6]), cubic evaluates to ~0.414 instead of linear 0.625.
674 assert!((rsup.eval(5.0) - 0.4140625).abs() < 1e-3, "got {}", rsup.eval(5.0));
675
676 // 2-keyframe cyclic curve [0]=0.5, [6]=1.0 snaps last.value to 0.5 and mirrors chord RH->LH.
677 let mut two = spline(vec![kf(0.0, 0.5, smooth), kf(6.0, 1.0, smooth)]);
678 two.apply_hue_curves_cyclic_ends();
679 assert!((two.eval(0.0) - 0.5).abs() < 1e-9);
680 assert!((two.eval(1.5) - 0.546875).abs() < 1e-6);
681 assert!((two.eval(3.0) - 0.5).abs() < 1e-9);
682 assert!((two.eval(4.5) - 0.453125).abs() < 1e-6);
683 assert!((two.eval(6.0) - 0.5).abs() < 1e-9);
684
685 // Explicit LH/RH handles must not be overwritten.
686 let mut explicit = spline(vec![
687 Keyframe { rh: Some([0.5, 0.9]), ..kf(0.0, 0.5, smooth) },
688 Keyframe { lh: Some([5.5, 0.8]), ..kf(6.0, 0.5, smooth) },
689 ]);
690 explicit.apply_hue_curves_cyclic_ends();
691 assert_eq!(explicit.keyframes[0].rh, Some([0.5, 0.9]));
692 assert_eq!(explicit.keyframes[1].lh, Some([5.5, 0.8]));
693 }
694}