Color name

Not all RGB values (combinations of Red, Green and Blue) return known color name.
There are few methods that you can check if this combination return a knowncolor but i have no VS here and can't say nothing more from the top of myhead. I guess someone else will give you an example meanwhile.
 
As mentioned only a few color have defined names, for other colors you need an approximation to nearest known color. Posted below is one algorithm I just invented that seems to work not too bad. It checks differences for each RGB channel for each known color, sum these up by power of 2 (this a way to "punish" single channel differences more) and adds to a SortedList. Then the best 5 matches is taken and same differences is sought in HSB color space (an alternate view of colors than RGB), which can in some cases align better to how we perceive colors than the average RGB calculation.
VB.NET:
Public Shared Function GetNearestKnownColor(ByVal col As Color, _
Optional ByVal excludeSystemColors As Boolean = True) As KnownColor
    Dim rgblist As New SortedList(Of Long, KnownColor)
    Dim rgb As Integer, hsb As Single, kcol As Color
    For Each known As KnownColor In System.Enum.GetValues(GetType(KnownColor))
        kcol = Color.FromKnownColor(known)
        If Not excludeSystemColors OrElse Not kcol.IsSystemColor Then
            rgb = RGBdiff(kcol, col)
            If Not rgblist.ContainsKey(rgb) Then
                rgblist.Add(rgb, known)
            End If
        End If
    Next
    Dim hsblist As New SortedList(Of Single, KnownColor)
    For i As Integer = 0 To 4
        kcol = Color.FromKnownColor(rgblist.Values(i))
        hsb = HSBdiff(col, kcol)
        If Not hsblist.ContainsKey(hsb) Then
            hsblist.Add(hsb, rgblist.Values(i))
        End If
    Next
    Return hsblist.Values(0)
End Function

Private Shared Function RGBdiff(ByVal col1 As Color, ByVal col2 As Color) As Integer
    Dim r, g, b As Integer
    r = Math.Abs(CInt(col1.R) - CInt(col2.R))
    g = Math.Abs(CInt(col1.G) - CInt(col2.G))
    b = Math.Abs(CInt(col1.B) - CInt(col2.B))
    Return r ^ 2 + g ^ 2 + b ^ 2
End Function
Private Shared Function HSBdiff(ByVal col1 As Color, ByVal col2 As Color) As Single
    Dim h, s, b As Single
    h = Math.Abs(col1.GetHue - col2.GetHue)
    s = Math.Abs(col1.GetSaturation - col2.GetSaturation)
    b = Math.Abs(col1.GetBrightness - col2.GetBrightness)
    Return h + s + b
End Function
some test code to display colors in pictureboxes and info in textbox:
VB.NET:
Dim rgb As Color = Color.FromArgb(255, 128, 0)
Dim col As Color = Color.FromKnownColor(GetNearestKnownColor(rgb))
PictureBox1.BackColor = rgb
PictureBox2.BackColor = col
Dim sb As New System.Text.StringBuilder
sb.AppendLine("original:" & rgb.ToString)
sb.AppendFormat("nearest name: {0} A=255 R={1} G={2} B={3}", col.Name, col.R, col.G, col.B)
TextBox1.Text = sb.ToString
As for the values you posted this has a very high brightness which makes it harder to distinguish colors, it is also in the borderline of yellow/green like you can see from output of the algorithm.
 
Back
Top