List Colors At Runtime

  • Thread starter Thread starter Derek Hart
  • Start date Start date
D

Derek Hart

How would I list colors at runtime for System.Drawing.Color - I want to give
the user a list of choices at runtime, such as
System.Drawing.Color.AliceBlue (as an example of one color). I could
recreate this list in a database table and pull from it, but is there a way
to do this in code, to get a full list (for use in a combo, or however I
want to). It would even be useful to programmatically get this list once to
place it into a table. A lot of choices to recreate.

Derek
 
Derek Hart said:
How would I list colors at runtime for System.Drawing.Color - I want to
give the user a list of choices at runtime, such as
System.Drawing.Color.AliceBlue (as an example of one color). I could
recreate this list in a database table and pull from it, but is there a
way to do this in code, to get a full list (for use in a combo, or however
I want to). It would even be useful to programmatically get this list
once to place it into a table. A lot of choices to recreate.

You will need to translate this to VB:


foreach(KnownColor col in Enum.GetValues(typeof(KnownColor)))
{
Color color = Color.FromKnownColor(col);
if(!color.IsSystemColor)
{
Console.WriteLine(col.ToString());
}
}
 
Derek,

All colors are ARGB, that is from 0000 to FFFF. A lot of colors as you can
see (exactly 32bit)

Which you can get all with color.fromArgb

Therefore what colors do you mean?

Cor
 
How would I list colors at runtime for System.Drawing.Color - I want to give
the user a list of choices at runtime, such as
System.Drawing.Color.AliceBlue (as an example of one color). I could
recreate this list in a database table and pull from it, but is there a way
to do this in code, to get a full list (for use in a combo, or however I
want to). It would even be useful to programmatically get this list once to
place it into a table. A lot of choices to recreate.

Derek

ColorDialog control? or system.drawing.color provides the list of
colors.

If your problem is listing and presenting them to the user, the
shortest way is to use colordialog in my opinion.

If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'Associate the color with your object
'Example: me.backcolor = colordialog1.color
End If
 
How would I list colors at runtime for System.Drawing.Color - I want to give
the user a list of choices at runtime, such as
System.Drawing.Color.AliceBlue (as an example of one color). I could
recreate this list in a database table and pull from it, but is there a way
to do this in code, to get a full list (for use in a combo, or however I
want to). It would even be useful to programmatically get this list once to
place it into a table. A lot of choices to recreate.

Derek

As Michael said, the easiest way is to loop through the values of the
enumeration System.Drawing.Color. From there you can dump the colors
into whatever control you like, be it a ComboBox, ListBox, etc. To get
the value back from the string, you will need to use [Enum].Parse to
convert back to the enumeration.

Thanks,

Seth Rowe [MVP]
 
That is my exact next question. Could you show an example of how to get the
value back from the string using Enum.Parse. No idea how to do this. I
will store the results in a sql table, and then pull it out to colorize
objects. Thank You!

Derek

rowe_newsgroups said:
How would I list colors at runtime for System.Drawing.Color - I want to
give
the user a list of choices at runtime, such as
System.Drawing.Color.AliceBlue (as an example of one color). I could
recreate this list in a database table and pull from it, but is there a
way
to do this in code, to get a full list (for use in a combo, or however I
want to). It would even be useful to programmatically get this list once
to
place it into a table. A lot of choices to recreate.

Derek

As Michael said, the easiest way is to loop through the values of the
enumeration System.Drawing.Color. From there you can dump the colors
into whatever control you like, be it a ComboBox, ListBox, etc. To get
the value back from the string, you will need to use [Enum].Parse to
convert back to the enumeration.

Thanks,

Seth Rowe [MVP]
 
That is my exact next question. Could you show an example of how to get the
value back from the string using Enum.Parse. No idea how to do this. I
will store the results in a sql table, and then pull it out to colorize
objects. Thank You!

Derek


As Michael said, the easiest way is to loop through the values of the
enumeration System.Drawing.Color. From there you can dump the colors
into whatever control you like, be it a ComboBox, ListBox, etc. To get
the value back from the string, you will need to use [Enum].Parse to
convert back to the enumeration.

Seth Rowe [MVP]

Unfortunately, the System.Drawing.Color is not an enumeration, so we
have to first parse the string into System.Drawing.KnownColor, and
then create the color from that (watch for wrap). You can one-step it,
but even with two lines its already verbose.

/////////////
Dim knownColor As System.Drawing.KnownColor =
DirectCast([Enum].Parse(GetType(System.Drawing.KnownColor),
"AliceBlue"), System.Drawing.KnownColor)
Dim color As System.Drawing.Color =
System.Drawing.Color.FromKnownColor(knownColor)
/////////////

Thanks,

Seth Rowe [MVP]
 
Now I need to grab the color from the ColorDialog, and use it as the string.
Not sure how to do this. In the code:

Dim knownColor As System.Drawing.KnownColor =
DirectCast([Enum].Parse(GetType(System.Drawing.KnownColor), "AliceBlue"),
System.Drawing.KnownColor)
Dim color As System.Drawing.Color =
System.Drawing.Color.FromKnownColor(knownColor)

I need to replace AliceBlue with the value from the color dialog, but the
ToString method gives the following:

? ColorDialog1.Color.ToString
"Color [A=255, R=255, G=128, B=0]"

Is there some other color dialog that can show the list of names with the
colors, such as AliceBlue, or am I stuck with the ColorDialog control only.
If so, how do I convert the value "Color [A=255, R=255, G=128, B=0]" -
Please let me know if the option of a different dialog is available with the
names of the colors, like in the dotnet property sheet, and if not, how to
convert the RGB value.

Thank You!
Derek
rowe_newsgroups said:
That is my exact next question. Could you show an example of how to get
the
value back from the string using Enum.Parse. No idea how to do this. I
will store the results in a sql table, and then pull it out to colorize
objects. Thank You!

Derek


How would I list colors at runtime for System.Drawing.Color - I want
to
give
the user a list of choices at runtime, such as
System.Drawing.Color.AliceBlue (as an example of one color). I could
recreate this list in a database table and pull from it, but is there
a
way
to do this in code, to get a full list (for use in a combo, or however
I
want to). It would even be useful to programmatically get this list
once
to
place it into a table. A lot of choices to recreate.

As Michael said, the easiest way is to loop through the values of the
enumeration System.Drawing.Color. From there you can dump the colors
into whatever control you like, be it a ComboBox, ListBox, etc. To get
the value back from the string, you will need to use [Enum].Parse to
convert back to the enumeration.

Seth Rowe [MVP]

Unfortunately, the System.Drawing.Color is not an enumeration, so we
have to first parse the string into System.Drawing.KnownColor, and
then create the color from that (watch for wrap). You can one-step it,
but even with two lines its already verbose.

/////////////
Dim knownColor As System.Drawing.KnownColor =
DirectCast([Enum].Parse(GetType(System.Drawing.KnownColor),
"AliceBlue"), System.Drawing.KnownColor)
Dim color As System.Drawing.Color =
System.Drawing.Color.FromKnownColor(knownColor)
/////////////

Thanks,

Seth Rowe [MVP]
 
Hmm.... it seems the ColorDialog does not return a known color (such
as AliceBlue) but just a color, which could just be a ARGB value. I
would say you either need to write your own form that exposes just
known colors (say through a dropdown) or just store the ARGB values
and convert them back to a color. And since you asked, you need to use
the Color.FromARGB() method to get the color.

Thanks,

Seth Rowe [MVP]
 
Any chance you could show me an example. Somewhat out of my league here.

Do I have to parse this: Color [A=255, R=255, G=128, B=0] - and somehow get
the RGB value into text, and then use the FromARGB function. How in the
heck do I do that? By the way, any idea what the A value is?

And separately, does dotnet have a way to get the cool list of named colors
(KnownColors), or do I have to completely build that from scratch?

Derek
 
Any chance you could show me an example. Somewhat out of my league here.

Here an example taking a color to a string and back again:

//////////////////
Me.ColorDialog1.ShowDialog()

'// Get the color from the dialog
Dim color As Color = Me.ColorDialog1.Color

'// Turn the color into a string
Dim theString As String = color.ToArgb().ToString()

'// Turn the string back into a color
Dim theColor As Color = color.FromArgb(Integer.Parse(theString))
//////////////////
By the way, any idea what the A value is?
Alpha-transparency

And separately, does dotnet have a way to get the cool list of named colors
(KnownColors), or do I have to completely build that from scratch?

Here an example:

//////////////////
For Each value As String In [Enum].GetNames(GetType(KnownColor))
MessageBox.Show(value)
Next
//////////////////

Thanks,

Seth Rowe [MVP]
 
Is there a KnownColor dialog box, so I don't have to build it from scratch?


rowe_newsgroups said:
Any chance you could show me an example. Somewhat out of my league here.

Here an example taking a color to a string and back again:

//////////////////
Me.ColorDialog1.ShowDialog()

'// Get the color from the dialog
Dim color As Color = Me.ColorDialog1.Color

'// Turn the color into a string
Dim theString As String = color.ToArgb().ToString()

'// Turn the string back into a color
Dim theColor As Color = color.FromArgb(Integer.Parse(theString))
//////////////////
By the way, any idea what the A value is?
Alpha-transparency

And separately, does dotnet have a way to get the cool list of named
colors
(KnownColors), or do I have to completely build that from scratch?

Here an example:

//////////////////
For Each value As String In [Enum].GetNames(GetType(KnownColor))
MessageBox.Show(value)
Next
//////////////////

Thanks,

Seth Rowe [MVP]
 
I found a color dialog with the Infragistics controls. When getting the
color from it, it gives this value from Me.UltraColorPicker1.Value - Color
[LightCoral]

It looks like I could parse out the LightCoral text and use that to write it
back. Do you agree?



rowe_newsgroups said:
Any chance you could show me an example. Somewhat out of my league here.

Here an example taking a color to a string and back again:

//////////////////
Me.ColorDialog1.ShowDialog()

'// Get the color from the dialog
Dim color As Color = Me.ColorDialog1.Color

'// Turn the color into a string
Dim theString As String = color.ToArgb().ToString()

'// Turn the string back into a color
Dim theColor As Color = color.FromArgb(Integer.Parse(theString))
//////////////////
By the way, any idea what the A value is?
Alpha-transparency

And separately, does dotnet have a way to get the cool list of named
colors
(KnownColors), or do I have to completely build that from scratch?

Here an example:

//////////////////
For Each value As String In [Enum].GetNames(GetType(KnownColor))
MessageBox.Show(value)
Next
//////////////////

Thanks,

Seth Rowe [MVP]
 
I believe this is what you are looking for. Unfortunately, I only have it in
C#, and don't have time to convert it right this minute. If you can't figure
it out, post back and I will do that this weekend. Please don't flame me for
posting C#, I'm just trying to help here.

For my users, I started with the windows color dialog, and wasn't happy, so
I now use a combobox to show the colors, with OwnerDraw set up to draw a
square containing the color, and the name of the color, in each line of the
dropdown list. It looks good.

I got code for doing the OwnerDraw from Tim Patrick's "Start-to-Finish VB"
book, or he posted it in this forum last year; I can't remember which. It
didn't have info on how to get the list though.

This uses reflection to get the same color names that are displayed in the
Visual Studio dropdown list, like "Alice Blue" and "Yellow". You have to
import the System.Reflection namespace.

I am actually pulling the RGB values and sorting the list to try to get some
kind of order out of it rather than alphabetical by name, but it's not a
perfect algorithm, so I'm not including it here. If anybody has any other
brilliant ideas on how they get that list in the Visual Studio dialog to put
similar colors next to each other, I'm all ears.

If you want the ownerdraw stuff for the combobox, re-post, and I'll post it.
It will be in C# tho, unless I have time to convert it.

So this is how I'm retrieving the list of colors.

PropertyInfo[] props = typeof(Color).GetProperties();

foreach (PropertyInfo prop in props)
{
string cName = prop.Name;
if (cName != "Transparent")
{
ColorConverter clrConverter = new ColorConverter();
//you'll get R, G, B, A,
// and the properties IsKnownColor, etc.
//put in a try/catch block so if it's not a valid color,
//it just continues
try
{
Color clr = (Color)clrConverter.ConvertFromString(cName);
if (clr.IsNamedColor)
{
System.Diagnostics.Debug.Print("Color name = {0}",
prop.Name);
}
}
catch { }
}

}

Hope that helps.

RobinS.
GoldMail, Inc.
 
Back
Top