This GitHub gist repository contains practical C# code snippets from the Working With SVG Colors in C# chapter of Aspose.SVG for .NET documentation. The examples show how to change SVG fill and stroke colors, add or update SVG background colors, work with CSS color declarations, replace SVG color palettes, extract colors from SVG files, control opacity and transparency, edit gradient colors, and convert color codes such as HEX, RGB, RGBA, HSL, and CMYK.
- Change SVG Element Colors – Update
fill,stroke, and other presentation attributes for shapes such as<circle>,<line>,<path>, and<rect>. - Add and Change SVG Background Color – Insert a background
<rect>, update an existing background element, or create an SVG document with a custom background color. - Apply SVG Colors with CSS – Use inline
styleattributes and internal<style>elements to define SVG fills, strokes, and background colors. - Use JavaScript for SVG Background Color – Append a
<script>element that dynamically adds a background rectangle to an SVG document. - Replace SVG Colors – Replace colors in
fill,stroke,stop-color, and inline CSS declarations, including palette-based color replacement. - Extract SVG Colors – Collect unique colors from SVG attributes, inline styles, and gradient stops for inspection or auditing.
- Normalize Color Values – Convert color names, HEX values, RGB values, LAB values, and other color formats to consistent output formats.
- Control SVG Opacity and Transparency – Set
opacity,fill-opacity,stroke-opacity, RGBA colors, HSLA colors, and transparent export backgrounds. - Change SVG Gradient Colors – Update linear and radial gradient stops, replace all gradient stop colors, and repoint
fill="url(#id)"to another gradient. - Save and Export Edited SVG Files – Save modified SVG documents and export edited color or gradient results to formats such as PNG and PDF.
- Modify SVG Styles with the DOM API – Create gradients, filters, shapes, text, and inline styles programmatically.
- Create a C# project and add the Aspose.SVG for .NET package via NuGet.
- Locate the example that matches your SVG color task.
- Adjust input file paths, output paths, color values, selectors, and gradient IDs as needed.
- Run the code to review the output SVG, PNG, or PDF files and verify the applied color changes.
You can use a temporary license to evaluate Aspose.SVG for .NET without evaluation limitations.
Aspose.SVG for .NET is a cross-platform C# library for creating, editing, converting, and rendering SVG files on Windows, Linux, and macOS. It provides DOM APIs for working with SVG elements, attributes, styles, gradients, filters, and text. The library also includes color parsing and conversion features that help normalize color values and prepare SVG files for rendering, export, or automated processing.
- Official Documentation
- Working with SVG Colors
- How to Change SVG Color
- Change SVG Background Color
- Convert Color Codes
- Convert SVG to Image
- Convert SVG to PDF
- API Reference
- Product page – Aspose.SVG for .NET
- Blog – Aspose.SVG Product Family
- Temporary License
- Free Online Color Converters – Aspose.SVG
- .NET Framework 4.6.1+, .NET Core 2.0+, or .NET 5+.
- Windows, Linux, or macOS.
- A development environment such as Visual Studio, Visual Studio Code, or JetBrains Rider.
- Aspose.SVG for .NET installed via NuGet.