sammycage / lunasvg

SVG rendering and manipulation library in C++
MIT License
866 stars 124 forks source link

How to set color for bitmap before rendering? #63

Closed aeris170 closed 2 years ago

aeris170 commented 2 years ago

I'm rendering an SVG with the example code yoou provided and it works great, but I would like to alter the output color of the rendered SVG. Is this possible? I'd expect something along the lines of:

auto document = Document::loadFromFile(filename);
auto bitmap = document->renderToBitmap(width, height, fgColor, bgColor);
sammycage commented 2 years ago

I don't understand? What did you mean by fgColor(foreground color)?

aeris170 commented 2 years ago

I don't understand? What did you mean by fgColor(foreground color)?

I would like to change the color of rendered bitmap image. I have my SVG's as black on top of transparent, is it possible to render them with a different color? Like, red or white or blue?

sammycage commented 2 years ago

is it possible to render them with a different color? Like, red or white or blue?

Yes.. backgroundColor the third parameter in Document::renderToBitmap is there for you.

Bitmap renderToBitmap(std::uint32_t width = 0, std::uint32_t height = 0, std::uint32_t backgroundColor = 0x00000000) const;

backgroundColor format is 0xRRGGBBAA

Name HexCode BackgroundColor
Red #FF0000 0xFF0000FF
Breen #00FF00 0x00FF00FF
Blue #0000FF 0x0000FFFF
Yellow #FFFF00 0xFFFF00FF
White #FFFFFF 0xFFFFFFFF
Pink #FFC0CB 0xFFC0CBFF

Example

<svg id="svg1" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
    <circle id="circle1" cx="100" cy="100" r="80" fill="green"/>
</svg>
auto bitmap = document->renderToBitmap(0, 0, 0xFFFF00FF);

circle-yellow-bg

auto bitmap = document->renderToBitmap(0, 0, 0xFF0000FF);

circle-red-bg

auto bitmap = document->renderToBitmap(0, 0, 0xFFC0CBFF);

circle-red-bg

aeris170 commented 2 years ago

I understand, my initial question was probably not well formulated. I wanted to change the color of "green circle" in the example you provided. Now I see how I can. Since SVG are just markup (I just discovered this, thanks to you), I can loop every every child of tag set attrib fill="red". That way the whole SVG wil appear red. Thank you. I'm closing this issue since it is not LunaSVG's job to manipulate the SVG.