diff --git a/crates/engine/src/cmd/format.rs b/crates/engine/src/cmd/format.rs index dcb4ba3b8..8b8785aa8 100644 --- a/crates/engine/src/cmd/format.rs +++ b/crates/engine/src/cmd/format.rs @@ -590,9 +590,9 @@ fn font_edits(p: &Value) -> Result { fn color_command_value(c: &deckcraft_model::ColorRef) -> Value { use deckcraft_model::ColorBase; let mut o = serde_json::Map::new(); - for m in &c.mods { - // `inv`, `comp` and `gray` have no value. - o.insert(m.xml_name().to_string(), m.value().map_or(json!(true), |v| json!(v))); + // An ordered list, so the order of the transforms and repeated ones such as `[inv, inv]` are kept. + if !c.mods.is_empty() { + o.insert("mods".into(), json!(c.mods)); } // `#RRGGBB`, or `#RRGGBBAA` for a color that is not opaque. let hex = |c: &deckcraft_color::Rgba| if c.a == 255 { format!("#{}", c.hex()) } else { format!("#{}{:02X}", c.hex(), c.a) }; @@ -1556,7 +1556,10 @@ mod tests { let inverted = ColorRef { base: ColorBase::Preset { name: "red".into() }, mods: vec![] }.with(ColorTransform::Inv); let translucent = ColorRef { base: ColorBase::System { name: "windowText".into(), last: Rgba::rgba(0x12, 0x34, 0x56, 0x80) }, mods: vec![] }; let rgb = ColorRef::rgb(Rgba::rgba(0x12, 0x34, 0x56, 0x80)); - for color in [preset, system, inverted, translucent, rgb] { + // A repeated transform, and two transforms in an order that is not alphabetical. + let twice = inverted.clone().with(ColorTransform::Inv); + let ordered = ColorRef::scheme(SchemeSlot::Accent2).with(ColorTransform::LumOff(20_000)).with(ColorTransform::LumMod(50_000)); + for color in [preset, system, inverted, translucent, rgb, twice, ordered] { let mut s = shape(); let fill = Fill::Solid { color: color.clone() }; format_run(&mut s, &|r| r.fill = Some(fill.clone())).unwrap(); diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index f0f1141fb..9c6aadd2f 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -372,7 +372,8 @@ pub(crate) fn ok() -> Result { /// Parse a colour param: `"#RRGGBB"`, `"accent1"`…, or `{"scheme":"accent2","lumMod":75000}`. /// A preset or system colour keeps its base: `{"preset":"red"}`, -/// `{"system":"windowText","last":"#000000"}`. +/// `{"system":"windowText","last":"#000000"}`. `"mods"` lists transforms in order: +/// `{"preset":"red","mods":[{"op":"inv"},{"op":"inv"}]}`. pub(crate) fn color_param(p: &Value, key: &str) -> Option { let v = p.get(key)?; color_value(v) @@ -412,6 +413,10 @@ pub(crate) fn color_value(v: &Value) -> Option { c.mods.push(t); } } + // `mods` lists transforms in order, repeats included: `[{"op":"inv"},{"op":"lumMod","val":50000}]`. + if let Some(list) = o.get("mods") { + c.mods.extend(serde_json::from_value::>(list.clone()).ok()?); + } Some(c) } _ => None, @@ -669,5 +674,9 @@ mod tests { assert!(color_value(&json!({"preset": name})).is_some(), "{name}"); } assert!(color_value(&json!({"system": "3dDkShadow", "last": "#000000"})).is_some()); + let listed = color_value(&json!({"preset": "red", "mods": [{"op": "inv"}, {"op": "lumOff", "val": 20_000}, {"op": "inv"}]})); + let mods = vec![ColorTransform::Inv, ColorTransform::LumOff(20_000), ColorTransform::Inv]; + assert_eq!(listed, Some(ColorRef { base: ColorBase::Preset { name: "red".into() }, mods })); + assert_eq!(color_value(&json!({"preset": "red", "mods": [{"op": "notATransform"}]})), None); } } diff --git a/crates/ui-egui/src/font_dialog_tests.rs b/crates/ui-egui/src/font_dialog_tests.rs index e3076c62a..60b18f02d 100644 --- a/crates/ui-egui/src/font_dialog_tests.rs +++ b/crates/ui-egui/src/font_dialog_tests.rs @@ -66,9 +66,12 @@ fn command_t_opens_the_font_dialog_and_cancel_keeps_the_text() { #[test] fn preset_and_system_colors_show_in_the_dialog_and_ok_keeps_them() { - for (color, shown) in - [(json!({"preset": "red", "lumMod": 50000}), "red"), (json!({"system": "windowText", "last": "#123456", "alpha": 60000}), "#123456")] - { + for (color, shown) in [ + (json!({"preset": "red", "lumMod": 50000}), "red"), + (json!({"system": "windowText", "last": "#123456", "alpha": 60000}), "#123456"), + (json!({"preset": "red", "mods": [{"op": "inv"}, {"op": "inv"}]}), "red"), + (json!({"scheme": "accent2", "mods": [{"op": "lumOff", "val": 20000}, {"op": "lumMod", "val": 50000}]}), "accent2"), + ] { let mut h = harness(); h.state_mut().session.execute("format.fontDialog", &json!({ "color": color })).unwrap(); let before = fill_of(&h);