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