Merge pull request #233 from Silvertree2010: keep alpha in a colour's hex form (M8)

# Conflicts:
#	crates/render/src/lottie.rs
This commit is contained in:
Tanish Manku committed 2026-11-21 13:01:59 -07:00
commit 60ff0d83af
3 files changed
+16 -4

No files matched your search

+12 -1
View File
@@ -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<Rgba> {
@@ -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();
+2 -2
View File
@@ -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::<Vec<_>>(),
"symbols": symbols,
@@ -1139,7 +1139,7 @@ pub fn to_html(doc: &Document, opts: &HtmlOptions) -> Result<HtmlExport, String>
// 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("<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n");
+2 -1
View File
@@ -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.