Tint: Swap Colors (#963) (#1060)

Squash of #1060 by @RavoxX, without the docs/images screenshots (kept out of the repo).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jannik Wappler
2026-10-13 04:33:43 -04:00
committed by Brandon Thomas
co-authored by Claude Opus 5.5
parent 9aabb5da4f
commit 1765dc53df
6 changed files with 186 additions and 2 deletions
+39 -1
View File
@@ -117,7 +117,7 @@ fn find_fx(s: &Session, p: &Value, cmd: &str) -> Result<(effectcraft_project::It
let g = match p.get("effect") {
Some(Value::Number(n)) => {
let n = n.as_u64().unwrap_or(0);
fx.groups().find(|g| g.uid == n).or_else(|| fx.groups().nth(n.saturating_sub(1) as usize))
fx.groups().find(|g| g.uid == n).or_else(|| usize::try_from(n.saturating_sub(1)).ok().and_then(|i| fx.groups().nth(i)))
}
Some(Value::String(name)) => fx.groups().find(|g| &g.name == name || g.match_id == *name),
_ => s.state.selected_props.iter().find_map(|(l, u)| (*l == lid).then(|| fx.groups().find(|g| g.uid == *u)).flatten()),
@@ -416,6 +416,35 @@ fn reset(s: &mut Session, p: &Value) -> Result<Value> {
Ok(Value::Null)
}
/// Tint's Swap Colors button edits both swatches at the CTI in one undo step. Like Reset,
/// it keys animated parameters without moving their existing keys or replacing expressions.
fn swap_tint_colors(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "effect.swapTintColors";
if !p.is_object() || p.get("effect").is_some_and(|v| !v.is_string() && v.as_u64().is_none_or(|n| n == 0)) {
return Err(bad(cmd, "effect must be a positive index, uid or name"));
}
let (cid, lid, uid) = find_fx(s, p, cmd)?;
let comp = s.project.comp(cid).ok_or(EngineError::NoComp)?;
let layer = comp.layer(lid).ok_or(EngineError::NoComp)?;
let g = layer.props.find_group(uid).filter(|g| g.match_id == "ec.color.tint").ok_or_else(|| bad(cmd, "not a Tint effect"))?;
let lt = layer.layer_time(s.time_of(cid));
let color = |id| {
g.prop(id)
.map(|pr| pr.value_at(lt))
.filter(|v| matches!(v, effectcraft_keyframe::Value::Color(_)))
.ok_or_else(|| bad(cmd, "Tint is missing a colour parameter"))
};
let (black, white) = (color("black")?, color("white")?);
s.edit("Swap Tint Colors", None, |proj, _| {
let g = layer_mut(proj, cid, lid)?.props.find_group_mut(uid).ok_or_else(|| bad(cmd, "effect gone"))?;
for (id, value) in [("black", white), ("white", black)] {
g.prop_mut(id).ok_or_else(|| bad(cmd, "Tint is missing a colour parameter"))?.set_value_at(lt, value);
}
Ok(())
})?;
Ok(json!({"effect": uid}))
}
fn last(s: &mut Session, p: &Value) -> Result<Value> {
let id = s.state.last_effect.clone().ok_or_else(|| bad("effect.applyLast", "no effect applied yet"))?;
let mut q = p.clone();
@@ -507,6 +536,15 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!("effect.copy", "Copy Effects", [], None, "{layer?, effect? | effects?: [uid|name|index]} (default: the selected effects)", has_layers, copy),
cmd!("effect.paste", "Paste Effects", [], None, "{layers?} — adds the copied effects to the layers", has_layers, paste),
cmd!("effect.reset", "Reset Effect", [], None, "{layer?, effect}", has_layers, reset),
cmd!(
"effect.swapTintColors",
"Swap Tint Colors",
[],
None,
"{comp?, layer?, effect?: index|uid|name} — swaps Tint's Map Black To and Map White To at the current layer time in one undo step",
has_layers,
swap_tint_colors
),
crate::query!(
"effect.list",
"List Effects",
+94
View File
@@ -147,6 +147,100 @@ fn reset_keys_animated_params_at_the_cti() {
assert_eq!(pr.keys[1].value, KV::Scalar(0.0));
}
#[test]
fn tint_swaps_only_its_colors_in_one_undo_step() {
let (mut s, a, b) = comp_with_two_solids();
let first = apply(&mut s, a, "Tint");
let target = apply(&mut s, a, "Tint");
let other = apply(&mut s, b, "Tint");
for (path, value) in [("black", json!([1.0, 0.0, 0.0, 1.0])), ("white", json!([0.0, 0.25, 1.0, 1.0])), ("amount", json!(45.0))] {
s.execute("prop.set", json!({"layer": a, "path": format!("effects/#2/{path}"), "value": value})).unwrap();
}
let before_a = layer(&s, a);
let before_b = layer(&s, b);
let n = s.history.undo.len();
s.execute("effect.swapTintColors", json!({"layer": a, "effect": target})).unwrap();
let swapped = layer(&s, a);
let g = swapped.props.find_group(target).unwrap();
assert_eq!(g.prop("black").unwrap().value, KV::Color([0.0, 0.25, 1.0, 1.0]));
assert_eq!(g.prop("white").unwrap().value, KV::Color([1.0, 0.0, 0.0, 1.0]));
assert_eq!(g.prop("amount").unwrap().value, KV::Scalar(45.0));
assert_eq!(swapped.props.find_group(first), before_a.props.find_group(first));
assert_eq!(layer(&s, b).props.find_group(other), before_b.props.find_group(other));
assert_eq!(s.history.undo.len(), n + 1);
assert!(s.undo());
assert_eq!(layer(&s, a).props, before_a.props);
assert!(s.redo());
assert_eq!(layer(&s, a).props, swapped.props);
}
#[test]
fn tint_swap_keys_animated_colors_at_layer_time() {
let (mut s, a, _) = comp_with_two_solids();
let target = apply(&mut s, a, "Tint");
s.execute("prop.set", json!({"layer": a, "path": "effects/#1/black", "value": [1.0, 0.0, 0.0, 1.0]})).unwrap();
s.execute("prop.set", json!({"layer": a, "path": "effects/#1/white", "value": [0.0, 0.0, 1.0, 1.0]})).unwrap();
s.execute("prop.toggleAnimation", json!({"layer": a, "path": "effects/#1/black"})).unwrap();
s.execute("time.set", json!({"time": 2.0})).unwrap();
s.execute("prop.set", json!({"layer": a, "path": "effects/#1/black", "value": [0.0, 1.0, 0.0, 1.0]})).unwrap();
s.execute("layer.timeStretch", json!({"layers": [a], "percent": 200.0})).unwrap();
// At comp time 2s the stretched layer is at 1s, between its unchanged 0s and 2s keys.
let before = layer(&s, a);
let g = before.props.find_group(target).unwrap();
let lt = before.layer_time(s.time());
let expected = g.prop("black").unwrap().value_at(lt);
let keys = g.prop("black").unwrap().keys.clone();
select_effect(&mut s, a, target);
s.execute("effect.swapTintColors", json!({})).unwrap();
let after = layer(&s, a);
let g = after.props.find_group(target).unwrap();
let black = g.prop("black").unwrap();
assert_eq!(black.keys.len(), 3);
assert_eq!(black.keys[0], keys[0]);
assert_eq!(black.keys[2], keys[1]);
assert_eq!(black.keys[1].time, lt);
assert_eq!(black.keys[1].value, KV::Color([0.0, 0.0, 1.0, 1.0]));
assert_eq!(g.prop("white").unwrap().value, expected);
assert!(g.prop("white").unwrap().keys.is_empty());
assert!(s.undo());
assert_eq!(layer(&s, a).props, before.props);
}
#[test]
fn tint_swap_rejects_invalid_targets_without_editing() {
let mut empty = Session::default();
assert!(empty.execute("effect.swapTintColors", json!({})).is_err());
let (mut s, a, _) = comp_with_two_solids();
let blur = apply(&mut s, a, "Gaussian Blur");
let tint = apply(&mut s, a, "Tint");
for params in [
json!({"layer": a, "effect": blur}),
json!({"layer": a, "effect": "missing"}),
json!({"layer": a, "effect": []}),
json!({"layer": a, "effect": 1.5}),
json!({"layer": a, "effect": 0}),
json!(false),
] {
let before = layer(&s, a);
let n = s.history.undo.len();
assert!(s.execute("effect.swapTintColors", params).is_err());
assert_eq!(layer(&s, a).props, before.props);
assert_eq!(s.history.undo.len(), n);
}
// A damaged project's colour parameter must also fail atomically.
let cid = s.active_comp_id().unwrap();
s.edit("Break Tint", None, |p, _| {
p.comp_mut(cid).unwrap().layer_mut(LayerId(a)).unwrap().props.find_group_mut(tint).unwrap().prop_mut("white").unwrap().value = KV::Scalar(1.0);
Ok(())
})
.unwrap();
let before = layer(&s, a);
let n = s.history.undo.len();
assert!(s.execute("effect.swapTintColors", json!({"layer": a, "effect": tint})).is_err());
assert_eq!(layer(&s, a).props, before.props);
assert_eq!(s.history.undo.len(), n);
}
#[test]
fn copy_layers_clears_effect_clipboard() {
let (mut s, a, b) = comp_with_two_solids();