How to Convert Hex to RGB in C# for Unity, WPF, and .NET
- System.Drawing: ColorTranslator.FromHtml("#3B82F6") converts hex to a Color struct directly.
- Universal approach: int rgb = Convert.ToInt32(hex, 16); then extract channels with bitwise AND and shift.
- Unity: new Color(r/255f, g/255f, b/255f) with float division.
- Use the converter above for quick lookups — the integers map directly to C# Color constructors.
Table of Contents
C# has multiple routes for hex-to-RGB conversion depending on your target framework. System.Drawing (WinForms, WPF, ASP.NET) has built-in methods. Unity uses a different Color struct with normalized floats. And the universal approach — parsing the hex integer directly with bitwise operations — works everywhere.
ColorTranslator: The Easiest Method in System.Drawing
For WinForms, WPF, and server-side .NET applications:
using System.Drawing;
// Parse hex directly — handles # prefix automatically
Color color = ColorTranslator.FromHtml("#3B82F6");
int r = color.R; // 59
int g = color.G; // 130
int b = color.B; // 246
Console.WriteLine($"rgb({r}, {g}, {b})");
ColorTranslator.FromHtml() accepts the # prefix and is case-insensitive. It also accepts named colors like "red" or "navy" using the same method, making it the most flexible option when your input format is not strictly controlled.
Bitwise Parsing: Works in Any C# Context
This approach has no dependency on System.Drawing and works in Unity, Xamarin, .NET MAUI, and any other C# environment:
static (int R, int G, int B) HexToRgb(string hex)
{
hex = hex.TrimStart('#');
int value = Convert.ToInt32(hex, 16);
int r = (value >> 16) & 0xFF;
int g = (value >> 8) & 0xFF;
int b = value & 0xFF;
return (r, g, b);
}
var (r, g, b) = HexToRgb("#3B82F6");
// r=59, g=130, b=246
Convert.ToInt32(hex, 16) parses the hex string as a base-16 integer. The bitwise shift and AND operations extract each 8-bit channel from the combined 24-bit value.
Unity: Creating a Color Struct from Hex in C#
Unity's Color struct uses normalized floats (0.0f to 1.0f). ColorUtility.TryParseHtmlString() is the cleanest option:
using UnityEngine;
Color myColor;
if (ColorUtility.TryParseHtmlString("#3B82F6", out myColor))
{
renderer.material.color = myColor;
}
Alternatively, use the manual approach with the RGB integers from the converter:
// #3B82F6 = rgb(59, 130, 246)
Color myColor = new Color(59/255f, 130/255f, 246/255f);
The f suffix on 255 is important — without it, integer division produces 0 for any value under 255.
WPF: System.Windows.Media.Color from Hex
WPF uses a different Color struct than WinForms. The ColorConverter class handles hex input:
using System.Windows.Media;
Color wpfColor = (Color)ColorConverter.ConvertFromString("#3B82F6");
byte r = wpfColor.R; // 59
byte g = wpfColor.G; // 130
byte b = wpfColor.B; // 246
Note: WPF's Color struct stores channels as byte (0-255), not float. Do not confuse System.Windows.Media.Color (WPF) with System.Drawing.Color (WinForms) — they are separate types that are not interchangeable.
Get the RGB Integers for Your Hex Code
Paste any hex code above to get R, G, B values ready for Color() or new Color() in C#.
Open Hex to RGB ConverterFrequently Asked Questions
What is the difference between System.Drawing.Color and System.Windows.Media.Color?
They are two separate structs. System.Drawing.Color is used in WinForms and GDI+. System.Windows.Media.Color is used in WPF. They have similar APIs but are not interchangeable. Unity uses its own UnityEngine.Color struct with float channels.
Why does f matter when dividing in Unity (255f vs 255)?
In C#, dividing two integers performs integer division, which truncates the result. 59 / 255 = 0 in integer math. 59 / 255f = 0.231f in float math. The f suffix forces float division and produces the correct normalized value.
Does ColorTranslator.FromHtml work in .NET Core and .NET 5+?
Yes, but it requires the System.Drawing.Common NuGet package on non-Windows platforms (.NET Core and .NET 5+ on Linux/macOS). On Windows it works natively. For cross-platform code, the bitwise parsing approach is more portable.
How do I convert RGB back to hex in C#?
Use $"#{r:X2}{g:X2}{b:X2}" in string interpolation, or string.Format("#{0:X2}{1:X2}{2:X2}", r, g, b). The X2 format specifier outputs a two-character uppercase hex value.

