diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index d951c220..91e3dbf1 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -100,7 +100,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 { @@ -2511,6 +2515,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 5a62dfdc..64c7b9ce 100644 --- a/crates/render/src/html.rs +++ b/crates/render/src/html.rs @@ -1071,7 +1071,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, @@ -1139,7 +1139,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/svg.rs b/crates/render/src/svg.rs index d796a1dc..d785d4ca 100644 --- a/crates/render/src/svg.rs +++ b/crates/render/src/svg.rs @@ -257,8 +257,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.