This means one cannot use replaceColour to replace the literal color specified in the SVG (or in your design program such as Figma) as it will no longer match the juce::Colour. For example, here’s an icon with a black circle (at 66% opacity) and a black question mark (at 100%):
Both the rect and path specify fill/stroke colors as #000.
Loaded into juce, when I use replaceColour to replace this black color with something like red, the <rect> (circle) element’s juce::Colour remains black (it doesn’t pass the equality matching, as the circle’s juce::Colour has an alpha value).
Anyway, I’m assuming this minor issue won’t be fixed (even if people agreed it’s a bug) as juce::Path doesn’t support alpha except via fills. But perhaps this post can save someone else some time wondering why replaceColour isn’t doing what they expected.
(A workaround could be to have a flavor of replaceColour that ignores alpha…)
Just had this problem with my SVG (exported from Figma) and was looking for a workaround on the forum. As you said, having ‘stroke-opacity’ doesn’t allow me to replace the colour.
If I understood correctly we don’t have a way to work around this, is this correct? Or can we multiply the base colour of the svg with the stroke-opacity to get the color Juce uses in order to replace it?
Just to follow up here since I spent the morning on this again — the summary is:
The Original Problem: When parsing something like <path opacity="0.25" d="M14 11H2" stroke="black" stroke-width="2"/> JUCE applies the svg opacity as part of the FillType instead of for the path (represented in JUCE by a DrawableShape). As a result, replaceColour is no longer usable — the exact original color specified in the svg (and probably one’s drawing program) is gone, as the FillType has been premultiplied. This is what my original patch addressed.
The original patch would let me preserve the color, and correctly store the opacity information, but more was needed. A DrawableShape (which is what an svg path is parsed into and where svg opacity should be applied) is derived from Component and therefore does have a notion of alpha/opacity, it’s just not currently being used.
Updated Patch: In addition to the original patch parsing and storing the opacity attribute via the Drawables setAlpha, it actually needs to be used by DrawableShape’s paint method, and copied over in DrawablePath’s copy constructor. See: