From 1f58af08e7a3261ef988ae2118de04f967a2d15f Mon Sep 17 00:00:00 2001 From: NetterSilver <133535268+Silvertree2010@users.noreply.github.com> Date: Thu, 12 Nov 2026 14:51:19 +0100 Subject: [PATCH] Keep alpha in Rgba::to_hex so the hex round trip is lossless Found while testing crates/doc/src/lib.rs:77-81 (Rgba::to_hex) Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 Co-Authored-By: Claude Haiku 5.5 Co-Authored-By: Claude Sonnet 5.5 --- crates/doc/src/lib.rs | 13 ++++++++++++- crates/render/src/html.rs | 4 ++-- crates/render/src/lottie.rs | 2 +- crates/render/src/svg.rs | 3 ++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index b6cde82e..a74fd096 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -85,7 +85,11 @@ impl Rgba { pub fn to_hex(self) -> String { let s = self.sanitized(); let b = |v: f32| (v * 255.0).round() as u8; - format!("#{:02x}{:02x}{:02x}", b(s.r), b(s.g), b(s.b)) + if s.a < 1.0 { + format!("#{:02x}{:02x}{:02x}{:02x}", b(s.r), b(s.g), b(s.b), b(s.a)) + } else { + format!("#{:02x}{:02x}{:02x}", b(s.r), b(s.g), b(s.b)) + } } /// Parses `#rrggbb`, `rrggbb` or `#rrggbbaa`. pub fn parse_hex(s: &str) -> Option { @@ -1687,6 +1691,13 @@ mod tests { assert_eq!(LoopMode::Loop.local_frame(u32::MAX, u32::MAX, 0), 0); } + #[test] + fn hex_roundtrip_keeps_alpha() { + let c = Rgba::parse_hex("#ff000080").unwrap(); + assert_eq!(c.to_hex(), "#ff000080"); + assert_eq!(Rgba::parse_hex(&c.to_hex()).unwrap(), c); + } + #[test] fn hex_roundtrip() { let c = Rgba::parse_hex("#ff8000").unwrap(); diff --git a/crates/render/src/html.rs b/crates/render/src/html.rs index 3b070ec9..01446842 100644 --- a/crates/render/src/html.rs +++ b/crates/render/src/html.rs @@ -863,7 +863,7 @@ pub fn movie(doc: &Document, opts: &HtmlOptions) -> Movie { let json = json!({ "v": FORMAT_VERSION, "generator": format!("AniCraft {}", env!("CARGO_PKG_VERSION")), - "stage": {"w": stage.width.max(1), "h": stage.height.max(1), "fps": num(fps, 1000.0), "bg": stage.background.to_hex()}, + "stage": {"w": stage.width.max(1), "h": stage.height.max(1), "fps": num(fps, 1000.0), "bg": stage.background.with_alpha(1.0).to_hex()}, "fit": opts.fit, "scenes": doc.scenes.iter().enumerate().map(|(i, s)| json!({"name": s.name, "tl": i})).collect::>(), "symbols": symbols, @@ -913,7 +913,7 @@ pub fn to_html(doc: &Document, opts: &HtmlOptions) -> Result // the JSON is inside a string, where `<` reads the same. let data = m.json.to_string().replace('<', "\\u003c"); let title = escape_html(&opts.title); - let bg = doc.stage.background.to_hex(); + let bg = doc.stage.background.with_alpha(1.0).to_hex(); let (w, h) = (doc.stage.width.max(1), doc.stage.height.max(1)); let mut html = String::with_capacity(data.len() + PLAYER_JS.len() + FILTERS_JS.len() + CANVAS_JS.len() + 1024); html.push_str("\n\n\n\n\n"); diff --git a/crates/render/src/lottie.rs b/crates/render/src/lottie.rs index 5f04704a..5b1b216d 100644 --- a/crates/render/src/lottie.rs +++ b/crates/render/src/lottie.rs @@ -531,7 +531,7 @@ pub fn to_lottie(doc: &Document, name: &str, from: u32, to: u32, background: boo if background { let bg = doc.stage.background.sanitized(); layers.push(json!({"ddd": 0, "ind": shape_layers + mattes + 1, "ty": 1, "nm": "Stage", "sr": 1, "ks": static_ks(), "ao": 0, "ip": 0, "op": frames, "st": 0, "bm": 0, - "sc": bg.to_hex(), "sw": doc.stage.width, "sh": doc.stage.height})); + "sc": bg.with_alpha(1.0).to_hex(), "sw": doc.stage.width, "sh": doc.stage.height})); } let root = json!({ "v": "5.7.0", diff --git a/crates/render/src/svg.rs b/crates/render/src/svg.rs index f0f55234..02bb7b87 100644 --- a/crates/render/src/svg.rs +++ b/crates/render/src/svg.rs @@ -210,8 +210,9 @@ fn style_attrs(st: &anicraft_doc::Stroke, k: f32) -> String { out } +/// Opaque `#rrggbb`: SVG carries alpha in separate opacity attributes. fn hex(c: Rgba) -> String { - c.sanitized().to_hex() + c.sanitized().with_alpha(1.0).to_hex() } /// `fill="#rrggbb"` (or `stroke=…`), plus the opacity when not opaque.