Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert color codes in C#, parse a CSS color string with Color.FromString(), then output the value with methods such as ToRgbString(), ToRgbHexString(), or ToRgbaHexString(). Use this workflow to convert HEX to RGB, RGB to HEX, HSL to HEX, named colors to HEX, and SVG color values to one normalized format.
This article shows how to convert color codes programmatically using Aspose.SVG for .NET. The Color class can parse CSS color strings such as HEX, RGB, RGBA, HSL, HSLA, HWB, LAB, LCH, OKLAB, OKLCH, CMYK, NCOL, and color names, then convert them to a required output format.
Color conversion is useful when you need to compare SVG colors, normalize values extracted from a file, prepare a design palette, or convert CSS color formats for reporting and validation.
You will learn how to:
The examples use the Aspose.SVG drawing API:
| API | Used for |
|---|---|
| Color | Represents a parsed color and converts it between output formats |
| Color.FromString() | Parses CSS color strings such as #ff31ca, rgb(...), hsl(...), black, or cmyk(...) |
| Color.FromRgb() | Creates a color from RGB component values |
| Color.FromHsl() | Creates a color from HSL component values |
| Color.FromCmyk() | Creates a color from CMYK component values |
| ToRgbString() | Outputs a color as rgb(R, G, B) |
| ToRgbHexString() | Outputs a color as #RRGGBB |
| ToRgbaHexString() | Outputs a color as #RRGGBBAA |
| Convert() | Returns color components in a selected ColorModel |
Use this table as a quick map for common C# color conversion workflows:
| Task | C# pattern |
|---|---|
| Convert HEX to RGB | Color.FromString("#ff31ca").ToRgbString() |
| Convert RGB to HEX | Color.FromString("rgb(255, 49, 202)").ToRgbHexString() |
| Convert HSL to HEX | Color.FromString("hsl(315, 100%, 60%)").ToRgbHexString() |
| Convert color name to HEX | Color.FromString("black").ToRgbHexString() |
| Convert RGBA to 8-digit HEX | Color.FromString("rgba(255, 49, 202, 0.5)").ToRgbaHexString() |
| Normalize SVG colors | Parse each value with Color.FromString() and output ToRgbHexString() |
The following examples cover the most common color conversion tasks in C#: converting HEX, RGB, HSL, named colors, RGBA values, component-based colors, and extracted SVG colors.
The most common conversion is HEX to RGB. Parse the HEX value with Color.FromString() and call ToRgbString():
1using Aspose.Svg.Drawing;
2using System; 1// Convert a HEX color value to RGB format in C#
2
3// Parse HEX color from a string
4Color color = Color.FromString("#ff31ca");
5
6// Convert HEX to RGB
7string rgbColor = color.ToRgbString();
8
9// Print result to console
10Console.WriteLine(rgbColor);
11// Result: rgb(255, 49, 202)Use this conversion when you receive color values as CSS HEX codes but need RGB output for reports, validation tools, or downstream image processing.
To convert RGB to HEX, parse the RGB string and output a normalized six-digit HEX value with ToRgbHexString():
1using Aspose.Svg.Drawing;
2using System; 1// Convert an RGB color value to HEX format in C#
2
3// Parse RGB color from a string
4Color color = Color.FromString("rgb(255, 49, 202)");
5
6// Convert RGB to HEX
7string hexColor = color.ToRgbHexString();
8
9// Print result to console
10Console.WriteLine(hexColor);
11// Result: #FF31CAThis is useful when you want all extracted SVG or CSS colors to use the same format.
SVG and CSS may store colors in HSL notation. You can parse the HSL color string and convert it to HEX:
1using Aspose.Svg.Drawing;
2using System; 1// Convert an HSL color value to HEX format in C#
2
3// Parse HSL color from a string
4Color color = Color.FromString("hsl(315, 100%, 60%)");
5
6// Convert HSL to HEX
7string hexColor = color.ToRgbHexString();
8
9// Print result to console
10Console.WriteLine(hexColor);
11// Result: #FF33CCUse this pattern when CSS authoring tools, design systems, or themes store colors as HSL but your application needs HEX values for comparison or output.
The Color.FromString() method can parse CSS color names. This makes it easy to normalize named values when processing SVG attributes.
1using Aspose.Svg.Drawing;
2using System; 1// Convert a named color value to HEX format in C#
2
3// Parse a named color from a string
4Color color = Color.FromString("black");
5
6// Convert named color to HEX
7string hexColor = color.ToRgbHexString();
8
9// Print result to console
10Console.WriteLine(hexColor);
11// Result: #000000For SVG workflows, this is especially useful because design tools and hand-written SVG files may mix named colors, HEX values, and functional CSS color values in the same document.
If a color has transparency, use 8-digit HEX output when the alpha channel must be preserved. The alpha channel is important for SVG opacity, overlays, shadows, and transparent UI elements.
1using Aspose.Svg.Drawing;
2using System;1// Convert an RGBA color value to 8-digit HEX format in C#
2
3// Parse RGBA color from a string
4Color color = Color.FromString("rgba(255, 49, 202, 0.5)");
5
6// Convert RGBA to 8-digit HEX
7Console.WriteLine(color.ToRgbaHexString());
8// Result: #FF31CA7FUse ToRgbHexString() when you only need the RGB color. Use ToRgbaHexString() when transparency must be preserved in HEX output.
For opacity-focused SVG editing, see SVG Opacity and Transparent Colors in C#.
You can create colors directly from component values instead of parsing a string. The following example creates the same red color from RGB, HSL, and CMYK components:
1using Aspose.Svg.Drawing;
2using System; 1// Create the same red color from RGB, HSL, and CMYK components in C#
2
3// Create a color from RGB components
4Color rgbColor = Color.FromRgb(255, 0, 0);
5
6// Create a color from HSL components
7Color hslColor = Color.FromHsl(0, 1, 0.5f);
8
9// Create a color from CMYK components
10Color cmykColor = Color.FromCmyk(0, 1, 1, 0);
11
12// Print converted color values to console
13Console.WriteLine(rgbColor.ToRgbHexString());
14Console.WriteLine(hslColor.ToRgbString());
15Console.WriteLine(cmykColor.ToRgbHexString());
16// Result:
17// #FF0000
18// rgb(255, 0, 0)
19// #FF0000Creating colors from components is convenient for palette generators, import pipelines, and applications that receive color channel values from a database or UI controls.
SVG files often contain equivalent colors written in different formats:
1<rect fill="#808080" />
2<circle fill="rgb(128, 128, 128)" />
3<path stroke="hsl(0, 0%, 50%)" />For comparison, reporting, or deduplication, normalize values with Color.FromString() and ToRgbHexString():
1using Aspose.Svg.Drawing;
2using System;
3using System.Collections.Generic; 1// Normalize SVG color values to HEX format in C#
2
3
4// Store equivalent colors written in different formats
5List<string> sourceColors = new List<string>
6{
7 "#808080",
8 "rgb(128, 128, 128)",
9 "hsl(0, 0%, 50%)"
10};
11
12foreach (string sourceColor in sourceColors)
13{
14 // Parse each color value from a string
15 Color color = Color.FromString(sourceColor);
16
17 // Convert each color to HEX and print the result
18 Console.WriteLine($"{sourceColor} -> {color.ToRgbHexString()}");
19}Expected output:
1#808080 -> #808080
2rgb(128, 128, 128) -> #808080
3hsl(0, 0%, 50%) -> #808080If you need to extract colors from a real SVG file before normalizing them, see Extract SVG Colors in C#.
You can convert color codes online or programmatically. The free online Color Converter supports common color names and popular color formats such as HEX, RGB, HSL, HSV, LAB, HWB, CMYK, LCH, XYZ, and NCOL. Enter a color code or select a color visually, and the app displays equivalent values in other formats.
Color formats describe the same visual color in different ways. Some formats are common in CSS and SVG, some are useful for design systems, and others are used in print or color science. The CSS color syntax is defined by the CSS Color Module Level 4.
RGB (Red, Green, Blue) defines a color with red, green, and blue channels in the sRGB color space. In CSS and SVG, RGB values are commonly written as rgb(255, 0, 0) or as percentages such as rgb(100%, 0%, 0%). RGBA (Red, Green, Blue, Alpha) adds an alpha channel for transparency, for example rgba(255, 0, 0, 0.5).
Use RGB when you need direct screen-oriented color values or interoperability with image APIs. Use RGBA when the color itself must carry opacity, for example in semi-transparent SVG fills, strokes, shadows, overlays, or generated preview images. In SVG editing workflows, remember that color alpha and SVG opacity attributes are related but not identical: rgba(...) stores transparency in the color value, while opacity, fill-opacity, and stroke-opacity are separate SVG properties.
HEX (hexadecimal) is a compact sRGB notation for CSS and SVG colors. It can be written as #RGB, #RGBA, #RRGGBB, or #RRGGBBAA. In the long form, RR, GG, and BB store red, green, and blue channel values from 00 to FF. When alpha is included, AA stores transparency: 00 means fully transparent and FF means fully opaque. See the CSS Color Module Level 4
hex notation and MDN
<hex-color> references.
HEX is widely used in SVG attributes, CSS declarations, design systems, icon sets, and web UI configuration. It is also a good normalized output format because it is short, stable, and easy to compare as text.
HSL represents colors as hue, saturation, and lightness. Hue is an angle on the color wheel, saturation controls color intensity, and lightness controls how close the result is to black or white. HSLA (Hue, Saturation, Lightness, Alpha) adds alpha transparency.
Use HSL when you need to create color variations such as lighter, darker, or more saturated theme colors. It is especially convenient for design systems because changing the lightness or saturation is usually more intuitive than editing RGB channels. For comparison, reporting, or SVG cleanup, HSL values are often normalized back to RGB or HEX.
HSV (Hue, Saturation, Value) stores hue, saturation, and value. It is common in color pickers and design tools because it maps naturally to choosing a hue and then adjusting brightness and saturation. HWB stores hue, whiteness, and blackness, which can be useful for generating tint and shade variations.
These formats are less common in hand-written SVG but may appear in color conversion tools, design exports, or UI palette editors. When your application receives HSV or HWB values, convert them to a common output format before comparing colors with SVG attributes.
LAB and LCH are perceptual color spaces, designed so that numerical changes better match perceived visual changes. OKLAB and OKLCH are newer perceptual models used in modern CSS and design workflows. They are useful when RGB channel differences are not enough to describe how different two colors look to a human observer.
Use these models when perceptual consistency matters, such as palette generation, accessibility tooling, color difference analysis, or creating smooth color ramps. For SVG output, you may still convert the final colors to HEX or RGB if your downstream renderer, design pipeline, or validation rules expect traditional web color syntax.
CMYK (Cyan, Magenta, Yellow, Key/Black) is a print-oriented model based on cyan, magenta, yellow, and black ink components. It is common in prepress and print design, but it is not the usual color syntax for SVG presentation attributes or browser CSS. XYZ is a device-independent CIE color space often used as a reference in color science and conversion pipelines.
Use CMYK for print-related data and XYZ for technical color conversions or interoperability with color management systems. For web SVG output, convert print or device-independent colors to an RGB-based representation such as HEX or RGB before writing fill, stroke, or stop-color values.
| Problem | Cause | Fix |
|---|---|---|
| Equivalent colors do not match | One color is written as a name, another as HEX or RGB | Normalize both values with Color.FromString() and ToRgbHexString() before comparing |
| Transparency is lost | The code outputs ToRgbString() or ToRgbHexString() for an RGBA color | Use ToRgbaHexString() when alpha must be preserved in HEX output |
| A non-color SVG value fails to parse | SVG paint values such as none, currentColor, and url(#id) are not direct color values | Filter these values before calling Color.FromString() |
| Output differs from the original text | Color conversion normalizes the value and may change notation or casing | Keep the original string when source formatting matters, and store the normalized value separately |
| CMYK or LAB output is expected as a CSS string | These models are component-based in the API | Use Convert(ColorModel) when you need components for a specific color model |
How do I convert HEX to RGB in C#?
Use Color.FromString("#ff31ca") to parse the HEX color, then call ToRgbString() to output rgb(255, 49, 202).
How do I convert RGB to HEX in C#?
Use Color.FromString("rgb(255, 49, 202)"), then call ToRgbHexString() to output a normalized #RRGGBB value.
Can I convert HSL to HEX in C#?
Yes. Parse the HSL value with Color.FromString("hsl(315, 100%, 60%)"), then call ToRgbHexString().
Can I parse CSS color names in C#?
Yes. Color.FromString() supports CSS named colors. After parsing, output the value with ToRgbHexString() or another conversion method. For examples with expected output, prefer simple unambiguous names such as black.
How do I normalize SVG colors to HEX?
Collect direct color values from SVG attributes or styles, skip non-color paint values such as none, currentColor, and url(#id), parse each value with Color.FromString(), and output ToRgbHexString().
How do I keep transparency when converting colors?
Use ToRgbaHexString() for 8-digit HEX output. RGB and six-digit HEX output do not preserve the alpha channel.
Can Aspose.SVG convert CMYK, LAB, LCH, OKLAB, and OKLCH colors?
Yes. The Color class supports many color spaces and can parse or create colors in several models. Use Color.FromString() for supported CSS color strings or model-specific factory methods such as FromCmyk(), FromLab(), FromLch(), FromOklab(), and FromOklch().
Color Converter is a free online application for transforming color values between formats. Enter a color code and get equivalent color values immediately.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.