Has anyone successfully loaded a multicolor SVG in...
# compose-desktop
d
Has anyone successfully loaded a multicolor SVG into a Compose Desktop app? It strikes me as ridiculous that there is no way to do this, but you can load a single color SVG. Having to create three sizes of PNG for every multicolor icon is really frustrating.
o
Either use
Image
(I guess you are using
Icon
) or give
Color.Unspecified
to the `Icon`'s
tint
See (even if it's a drawable, I guess it's the same) https://kotlinlang.slack.com/archives/CJLTWPH7S/p1729202867919799
d
@Olivier Patry I appreciate the effort, but neither of your suggested solutions work. Using an Image or an Icon with Color.Unspecified defaults to an all black SVG. You get the shape, but they don't load multicolor SVGs. Try it. I've yet to see an example from any developer or AI that works. Claude.ai, who is amazingly good at answering questions like this, is also stumped.
o
can you share you SVG?
d
Sure. Here are three examples. I appreciate you looking at it, and I'm happy to reciprocate. I created the logo.svg and the txt.svg. The folder.svg is from FlatIcon. Note that these SVGs are used on the web and other platforms with no issues.
o
Here is what I get with these SVGs, it seems
folder.svg
is fine but
logo.svg
&
txt.svg
aren't
Copy code
var bytes by remember { mutableStateOf(ByteArray(0)) }
LaunchedEffect(svgFilename) {
    bytes = Res.readBytes("files/$svgFilename")
}
val svgPainter by remember {
    derivedStateOf {
        if (bytes.isNotEmpty()) {
            bytes.decodeToSvgPainter(density)
        } else {
            null
        }
    }
}

svgPainter?.let { painter ->
    Image(painter, null, Modifier.size(48.dp))
}
svgPainter?.let { painter ->
    Icon(painter, null, Modifier.size(48.dp), tint = Color.Unspecified)
}
svgPainter?.let { painter ->
    Icon(painter, null, Modifier.size(48.dp), tint = Color.Red)
}
given the SVGs, I guess this is because of
<style>
not being supported by the SVG painter
Indeed, inlining CSS style rules as SVG attributes fixes the issue
Capture d’écran 2024-11-04 à 10.47.52.png
folder.svg,logo.svg,txt.svg
Note: didn't test on Android, only Compose Desktop
o
@dleuck It wasn’t me. 🙃
😄 2
d
@Olivier Patry This is extremely helpful. I really appreciate you taking the time to figure this out. Thank you!
o
@dleuck No, you should address @Olivier Patry, not me. 😆
😂 2
🤝 1
d
@Oliver.O Thank you for the correction:-)
👍 1