From 345efa4e36dd40a3eaf50ab5d024a06ed4b1f7b9 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Wed, 18 Sep 2024 01:50:36 +0800 Subject: [PATCH] gpui: Fix img element to render correct SVG color (#15488) Release Notes: - N/A It should convert RGBA to BGRA. > I added an example color svg, that was I make based on [Lucide grip icon](https://lucide.dev/icons/grip). ## Before image ## After image Co-authored-by: Marshall Bowers --- crates/gpui/examples/image/color.svg | 13 +++++++++++++ crates/gpui/examples/image/image.rs | 2 +- crates/gpui/src/elements/img.rs | 7 ++++++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 crates/gpui/examples/image/color.svg diff --git a/crates/gpui/examples/image/color.svg b/crates/gpui/examples/image/color.svg new file mode 100644 index 0000000000..84e9809d09 --- /dev/null +++ b/crates/gpui/examples/image/color.svg @@ -0,0 +1,13 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/crates/gpui/examples/image/image.rs b/crates/gpui/examples/image/image.rs index cc8e1a686c..ac7af186d3 100644 --- a/crates/gpui/examples/image/image.rs +++ b/crates/gpui/examples/image/image.rs @@ -131,7 +131,7 @@ fn main() { PathBuf::from_str("crates/gpui/examples/image/app-icon.png").unwrap(), ), remote_resource: "https://picsum.photos/512/512".into(), - asset_resource: "image/app-icon.png".into(), + asset_resource: "image/color.svg".into(), }) }) .unwrap(); diff --git a/crates/gpui/src/elements/img.rs b/crates/gpui/src/elements/img.rs index 07f5acc95b..f1e8bb68e3 100644 --- a/crates/gpui/src/elements/img.rs +++ b/crates/gpui/src/elements/img.rs @@ -408,9 +408,14 @@ impl Asset for ImageAsset { // TODO: Can we make svgs always rescale? svg_renderer.render_pixmap(&bytes, SvgSize::ScaleFactor(1.0))?; - let buffer = + let mut buffer = ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.take()).unwrap(); + // Convert from RGBA to BGRA. + for pixel in buffer.chunks_exact_mut(4) { + pixel.swap(0, 2); + } + RenderImage::new(SmallVec::from_elem(Frame::new(buffer), 1)) };