Sorting a list of known colours

J

Jameson

Hi,

I have a list of known colours, generated using:


Dim colorNames As New System.Collections.Generic.List(Of String)

For Each known As KnownColor In [Enum].GetValues(GetType(KnownColor))

Dim specific As Color = Color.FromKnownColor(known)

If Not specific.IsSystemColor And Not specific.Name = "Transparent" Then
colorNames.Add(known.ToString())
End If

Next


I want to sort these colours according to similarity, ie. all the blues
together, all the reds together, etc. as is done in typical colour picker
lists/combo boxes Anyone know how I should do this? Which attribute should
I sort on?
 
R

Rory Becker

Hello Jameson,
I want to sort these colours according to similarity, ie. all the
blues together, all the reds together, etc. as is done in typical
colour picker lists/combo boxes Anyone know how I should do this?
Which attribute should I sort on?

I would think you should sort on the RGB Hex values.

1.> Determine the color for each string

2.> Translate the RGB value (without Alpha component if any ) into a string

3.> Sort by resultant value

Does that get you what you wanted?
 
K

kimiraikkonen

Hi,

I have a list of known colours, generated using:

Dim colorNames As New System.Collections.Generic.List(Of String)

For Each known As KnownColor In [Enum].GetValues(GetType(KnownColor))

Dim specific As Color = Color.FromKnownColor(known)

If Not specific.IsSystemColor And Not specific.Name = "Transparent" Then
colorNames.Add(known.ToString())
End If

Next

I want to sort these colours according to similarity, ie. all the blues
together, all the reds together, etc. as is done in typical colour picker
lists/combo boxes Anyone know how I should do this? Which attribute should
I sort on?

Jameson,
There's a article on CodeProject that seems in C# anyway you can
convert to VB. The thing should be to implement sorting colors by
hue / saturation / brightness.
http://www.codeproject.com/KB/selection/ColorPaletteControl.aspx
 
J

Jameson

Rory Becker said:
Hello Jameson,


I would think you should sort on the RGB Hex values.

1.> Determine the color for each string

2.> Translate the RGB value (without Alpha component if any ) into a
string
3.> Sort by resultant value

Does that get you what you wanted?

Hi Rory,

I tried that and it didn't seem to work (perhaps my implementation was
wrong). Sorting by hue, saturation and then brightness seems to give a nice
usable list though, so I'm doing it this way:


' Usage

myStringListOfColourNames.Sort(new ColourComparer)


' Colour comparer for a list of strings of known colours

Class ColourComparer
Implements IComparer(Of String)

Public Function Compare(ByVal x As String, ByVal y As String) As Integer
Implements System.Collections.Generic.IComparer(Of String).Compare

Dim theColour1 As Color = Color.FromName(x)
Dim theColour2 As Color = Color.FromName(y)

If theColour1.GetHue() = theColour2.GetHue() Then
If theColour1.GetSaturation() = theColour2.GetSaturation() Then
Return
theColour1.GetBrightness().CompareTo(theColour2.GetBrightness())
Else
Return
theColour1.GetSaturation().CompareTo(theColour2.GetSaturation())
End If
Else
Return theColour1.GetHue().CompareTo(theColour2.GetHue())
End If

End Function

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top