Getting the list of colors!

  • Thread starter Thread starter Sinex
  • Start date Start date
S

Sinex

Hi,
I want to retrieve the list of Colors available and display them in a drop
down combo. But the Color strcture doesnt seem to have a method that returns
the list of colors. Is there an enumerator that I can use?

Sinex
 
There are so many colors - with 1 byte for red, 1 byte for blue, 1 byte
for green, there are over 16 millions.
- you want to get the list of 16 basic colors?
- you want to get the list of colors used by windows components (title
bar, button background, etc.)?

Thi
 
To get the list of all "known color", get all members of KnownColor
enum then pass to Color.GetKnownColor. Known colors contains system
colors used by windows components, if you don't want to get them, the
alternative is using reflection to get static fields of Color struct.

void Color[] GetKnowColors()
{
KnownColor[] colors = Enum.GetValues(...);
Color[] cs = new Color[colors.Length];
for (...)
{
cs = Color.GetKnownColor(colors);
}
return cs;
}

The reflection approach not shown here.

Thi
 
Here is the reflection approach:

Type t = typeof(Color);
PropertyInfo[] ps = t.GetProperties(BindingFlags.Static |
BindingFlags.Public);
ArrayList colorList = new ArrayList(ps.Length);
foreach (PropertyInfo p in ps)
{
if (p.PropertyType == t)
{
colorList.Add(p.GetValue(null, null));
}
}

Now the colorList contains the list of colors defined in Color struct.

Hope it helps,
Thi
 
Just so know (and you probably already do know), this can be used to
generate 256x256x256 custom colors:

Color::FromArgb( r, g, b ) ; // r,g,b in the range of 0 -> 255

Or even transparent colors with the first component being the alpha blend
(how transparent it is on a scale of 0 (see-through) to 255 (opaque)):

Color::FromArgb( a, r, g, b ) ;

Hence, if you wanted to display ALL possible colors that would be 4 billion
of them!

I know what you mean though, you want a list of the ones which are
standardly built into the system (e.g., Red, Green, Biege, DarkGreen, Gray,
etc.)...can't help you there...

[==P==]

PS - DirectX likes colors like this: Color::FromArgb(r,g,b).ToArgb(), which
is an integer representation of the color...
 
In the mail sent to me, the OP said he just want the list of colors
defined as static properties in Color structure (KnownColor contains
more, like ActiveBorder, ActiveCaption, etc., in fact, it includes
colors defined in either Color or SystemColors) . The reflection
approach has solved this.
 
Back
Top