0
\$\begingroup\$

(I apologize in advance for my awful English)

I'm writing a personal UI library in C++ using SDL2, and when came the moment to implement icons, I struggled to find how to render them in a unique color.

When creating an Icon object, the constructor loads the SVG as a SDL_Texture and then does a SDL_RenderCopy when the render() method is called. The Icon class also has a field for the color. Suppose the initial SVG is black, how do I render the texture in e.g. white?

Thanks!

\$\endgroup\$
7
  • \$\begingroup\$ Have you considered making your SVG inputs white (changing the SVG markup directly if you need to) then modulating the colour when you draw the textures to the screen using SetTextureColorMod? \$\endgroup\$
    – DMGregory
    Commented Jul 22 at 1:59
  • \$\begingroup\$ I tried to do so with one of them and it worked perfectly. But since I designed the icons myself, I'll have to recolor every single one of them, and they're about 125 :( \$\endgroup\$
    – Gkcrafting
    Commented Jul 23 at 20:48
  • \$\begingroup\$ That should be possible to automate. If they're in plaintext SVG, you could do it with a regex search & replace using tools like Notepad++. \$\endgroup\$
    – DMGregory
    Commented Jul 23 at 20:53
  • \$\begingroup\$ I considered doing just that. But the issue is that the SVGs are slightly different from one another. For some it's the stroke that's black, for others it's the fill. But I saw online that it is also possible to achieve the expected result by grayscaling the loaded SVG but unfortunately, I don't know how to do so :| \$\endgroup\$
    – Gkcrafting
    Commented Jul 23 at 21:06
  • \$\begingroup\$ I don't see why that would pose a problem. Want to edit your question to include a sample of 2-3 sets of SVG code you're having trouble adjusting automatically? \$\endgroup\$
    – DMGregory
    Commented Jul 23 at 22:30

1 Answer 1

1
\$\begingroup\$

In order to draw an SVG into the screen in any color in SDL2, the SVG needs to be initially white (if it's black, it won't work at all) then the SDL_SetTextureColorMod function must be called on the loaded texture alongside with the wanted color red, green and blue values.

In case you might need it, here is how to load an SVG in SDL2.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .