Convert string to Color in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Converting a string to a color in C# sounds simple until you notice that color strings can come in several formats. You might receive a named color such as Red, a web hex value such as #FF0000, or an ARGB-style value that needs custom parsing.
Choose the API Based on the Input Format
In C#, there is no single universal parser that always means the same thing across every UI stack. The most common choices are:
- '
ColorTranslator.FromHtmlfor HTML-style color strings' - '
Color.FromNamefor known color names' - '
ColorConverterfor more flexible component-model conversion'
If you are using System.Drawing.Color, ColorTranslator.FromHtml is often the easiest answer for web-style colors.
Parsing Hex and Named Colors
Here is a practical helper for System.Drawing.Color:
Usage:
This handles the two most common formats cleanly.
Supporting Alpha Values
One detail that surprises people is alpha handling. ColorTranslator.FromHtml works well for standard HTML-style colors, but if your input includes a custom #AARRGGBB format, you may want to parse it yourself.
Example:
This is useful when the incoming format is controlled by your own application or API.
ColorConverter as a Flexible Option
ColorConverter can also parse many textual representations:
This is convenient in configuration-heavy code, but for a narrow known input format, a dedicated parser is often clearer.
Be Careful About Framework Differences
If you are writing WPF code, you may be using System.Windows.Media.Color instead of System.Drawing.Color. The APIs are similar in spirit but not interchangeable.
That means the correct parser depends on which Color type your app actually uses. Mixing them is a common source of confusion in older desktop codebases.
Error Handling Strategy
A good parser should fail explicitly on unsupported input rather than silently returning black or transparent by accident. Color parsing bugs are easy to miss because the UI still renders something.
In reusable code, prefer one of these patterns:
- throw
ArgumentExceptionfor invalid input - provide a
TryParse-style method that returnsbool - log and fall back only when the UI genuinely has a safe default
Common Pitfalls
The biggest mistake is assuming Color.FromName validates all input. Unknown names usually return a color object that looks harmless but does not represent a real known color in the way most developers expect.
Another issue is ignoring alpha format. Some inputs use #RRGGBB, while others use #AARRGGBB, and those are not interchangeable.
Developers also often mix System.Drawing.Color with WPF color types and then wonder why converter code does not compile or behaves differently.
Finally, do not silently swallow bad color strings. Invalid UI data should be easy to diagnose.
Summary
- Pick the parsing API based on the expected input format.
- Use
ColorTranslator.FromHtmlfor common web-style hex values. - Use
Color.FromNameorColorConverterfor named colors when appropriate. - Parse alpha-inclusive formats explicitly if your input uses them.
- Be clear about whether your app uses
System.Drawing.Coloror a different color type.
Related reading
- Convert string to hex-string in C
- Convert string to int in one line of code using LINQ
- Convert string to nullable type int, double, etc...
- Convert String to SecureString
- Convert String to Type in C
- Convert String to Uri
- Convert time span value to format hhmm Am/Pm using C
- Convert.ChangeType fails on Nullable Types

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.