For future reference, here is a manual method for parsing HTML/CSS colors to Color (nullable color) in C#. This code snippet covers the standard cases for web styling.
''' <summary>
''' Named groups "r", "g", "b".
''' </summary>
Private Shared Regex_Html_RRGGBB As New Regex("^#(?<r>[0-9a-f][0-9a-f])(?<g>[0-9a-f][0-9a-f])(?<b>[0-9a-f][0-9a-f])$", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)
''' <summary>
''' Named groups "r", "g", "b".
''' </summary>
Private Shared Regex_Html_RGB As New Regex("^#(?<r>[0-9a-f])(?<g>[0-9a-f])(?<b>[0-9a-f])$", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)
''' <summary>
''' Named groups "r", "g", "b".
''' </summary>
Private Shared Regex_Html_RGB_funct As New Regex("^rgb\((?<r>[0-255]{1,3})[\t ]*,[\t ]*(?<g>[0-255]{1,3})[\t ]*,[\t ]*(?<b>[0-255]{1,3})\)$", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)
''' <summary>
''' Named groups "r", "g", "b", "a" where A is only 0-1 range decimal.
''' </summary>
Private Shared Regex_Html_RGBA_funct As New Regex("^rgba\((?<r>[0-255]{1,3})[\t ]*,[\t ]*(?<g>[0-255]{1,3})[\t ]*,[\t ]*(?<b>[0-255]{1,3}),[\t ]*(?<a>[0-1][\.]{0,1}[0-9]{0,9})\)$", RegexOptions.ExplicitCapture Or RegexOptions.IgnoreCase Or RegexOptions.Compiled)
''' <summary>
''' Reverse convertion from css format color to drawing color.
''' Supported formats #RRGGBB, #RGB, rgb(i,i,i), rgba(i,i,i,d), Named Colors.
''' Returns Nothing on failure.
''' </summary>
''' <param name="value"></param>
Public Shared Function FromHtmlColor(value As String) As Color?
If value.IsNullOrWhitespace Then
Return Nothing
Else ' must parse
' color translator doesn't account for transparancy.
' must check manually first.
Dim m As Match
m = Regex_Html_RGBA_funct.Match(value)
If m.Success Then
Dim r As Integer = ToRange(0, ToInt(m.Groups("r").Value), 255)
Dim g As Integer = ToRange(0, ToInt(m.Groups("g").Value), 255)
Dim b As Integer = ToRange(0, ToInt(m.Groups("b").Value), 255)
Dim a As Integer = ToRange(0, ToInt(255 * ToDbl(m.Groups("a").Value)), 255) ' convert 0-1 to 0-255.
Return Color.FromArgb(a, r, g, b)
End If
m = Regex_Html_RRGGBB.Match(value)
If m.Success Then
Dim r As Integer = Convert.ToInt32(m.Groups("r").Value, 16)
Dim g As Integer = Convert.ToInt32(m.Groups("g").Value, 16)
Dim b As Integer = Convert.ToInt32(m.Groups("b").Value, 16)
Return Color.FromArgb(r, g, b)
End If
m = Regex_Html_RGB_funct.Match(value)
If m.Success Then
Dim r As Integer = ToRange(0, ToInt(m.Groups("r").Value), 255)
Dim g As Integer = ToRange(0, ToInt(m.Groups("g").Value), 255)
Dim b As Integer = ToRange(0, ToInt(m.Groups("b").Value), 255)
Return Color.FromArgb(r, g, b)
End If
m = Regex_Html_RGB.Match(value) ' #rgb that needs values doubled before conversion.
If m.Success Then
Dim r As Integer = Convert.ToInt32(m.Groups("r").Value & m.Groups("r").Value, 16) ' double up each character for "#rgb"
Dim g As Integer = Convert.ToInt32(m.Groups("g").Value & m.Groups("g").Value, 16)
Dim b As Integer = Convert.ToInt32(m.Groups("b").Value & m.Groups("b").Value, 16)
Return Color.FromArgb(r, g, b)
End If
Try ' color translator as final fallback (this supports very little surprisingly
Return Drawing.ColorTranslator.FromHtml(value)
Catch ex As Exception
' ignore.
End Try
Return Nothing
End If
End Function