From e77578a53f8bba0d1bf20cce4eb047fb71afcb79 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Tue, 12 Nov 2024 19:26:24 +0800 Subject: [PATCH] svg_img: Fix the conversion error of the SVG fill color with alpha (#410) ### Before image ### After image --- crates/ui/src/svg_img.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/crates/ui/src/svg_img.rs b/crates/ui/src/svg_img.rs index cb028cbf..94f8337a 100644 --- a/crates/ui/src/svg_img.rs +++ b/crates/ui/src/svg_img.rs @@ -131,9 +131,15 @@ impl Asset for Image { let mut buffer = ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.take()) .expect("invalid svg image buffer"); - // Convert from RGBA to BGRA. + // Convert from RGBA with premultiplied alpha to BGRA with straight alpha. for pixel in buffer.chunks_exact_mut(4) { pixel.swap(0, 2); + if pixel[3] > 0 { + let a = pixel[3] as f32 / 255.; + pixel[0] = (pixel[0] as f32 / a) as u8; + pixel[1] = (pixel[1] as f32 / a) as u8; + pixel[2] = (pixel[2] as f32 / a) as u8; + } } Ok(Arc::new(RenderImage::new(SmallVec::from_elem(