Austin Smith
Austin Smith

Reputation: 53

Getting Color Values C#

I'm having trouble with this one problem. I'm trying to compare Color RGB in a list and sort them by there RGB. I've thought about adding it to a dictionary but don't know exactly how to get the values of the colors once their added to the Dictionary. So how would can I get the RBG of color in a list to compare them with one another. Any help or advice and I will be grateful, thanks.

Upvotes: 0

Views: 181

Answers (2)

Jayant Varshney
Jayant Varshney

Reputation: 1825

See this example

//if you want to compare each value
Color _color = Colors.AliceBlue;
byte R = _color.R;
byte G = _color.G;
byte B = _color.B;

use compareTo also for byte comparison

Upvotes: 0

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57640

You can easily use System.Drawing.Color structure for this. It provides an toArgb method which you can use to get the equivalent integer value of a color.

List<Color> lc = new List<Color>();
Color c = new Color();
c.R = 0xFF;
c.G = 0x00;
c.B = 0x00;
lc.Add(c);
...
...
lc.Sort((c1, c2) => c1.ToArgb().CompareTo(c2.ToArgb));

Upvotes: 1

Related Questions