|
-
Nov 11th, 2007, 06:35 PM
#1
Thread Starter
New Member
Get Color Name
Hi, I've a color pick dialog form, and I need which color is selected by the user and get the name.
I see that some colors have the Name property set to a system color name (yellow, red, etc...) but some others are in the RGB style.
How can I do, to get a valid name or show only a set of colours with a valid system name ?
Thank you.
Hope you understand my problem and sorry for my english
-
Nov 11th, 2007, 06:41 PM
#2
Re: Get Color Name
This has ben answered before. I remember it. Try a forums search and you will find the thread and its source code example.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Nov 11th, 2007, 06:44 PM
#3
Re: Get Color Name
All Colors known by the system can be retrieved using the KnownColor enumeration. Just be aware that that enumeration includes SystemColors too, which are the colours assigned to theme elements.
vb.net Code:
Dim c As Color
For Each value As KnownColor In [Enum].GetValues(GetType(KnownColor))
c = Color.FromKnownColor(value)
MessageBox.Show(c.ToArgb().ToString("X"), c.Name)
Next value
You should note from running that code that all the SystemColors are listed first, so that should make it relatively easy to exclude those from your list if you so desire.
-
Nov 11th, 2007, 06:57 PM
#4
Thread Starter
New Member
Re: Get Color Name
 Originally Posted by jmcilhinney
You should note from running that code that all the SystemColors are listed first, so that should make it relatively easy to exclude those from your list if you so desire.
I don't understand sorry, I need to create a custom ColorDialog form for exclude the color without a system name ?
This my simple code for get the color from the color dialog picker:
Code:
private void coloreToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() != DialogResult.Cancel)
{
string col = colorDialog1.Color.Name;
functionColor(col);
}
}
where I need to pass a valid color name to the functionColor.
-
Nov 11th, 2007, 07:35 PM
#5
Re: Get Color Name
You should be testing the IsNamedColor property of the selected Color.
Note that it is considered good style to always test for a positive condition where possible, so the tenets of good style dictate that you change this:
CSharp Code:
if (colorDialog1.ShowDialog() != DialogResult.Cancel)
to this:
CSharp Code:
if (colorDialog1.ShowDialog() == DialogResult.OK)
By the way, sorry for my VB code before. I often forget that I'm in the C# forum. Call me goldfish.
-
Nov 11th, 2007, 08:02 PM
#6
Re: Get Color Name
In reading the documentation I see that the IsNamedColor property doesn't consider the RGB value of a Color, so even if the Color represents a named Color the property will still return False if the Color was created from an Argb value rather than a name. To see what I mean try this code:
CSharp Code:
Color c1 = Color.FromArgb(255, 255, 255, 255);
Color c2 = Color.FromKnownColor(KnownColor.White);
Color c3 = Color.FromName("White");
MessageBox.Show(c1.ToArgb().ToString("X"), c1.Name);
MessageBox.Show(c2.ToArgb().ToString("X"), c2.Name);
MessageBox.Show(c3.ToArgb().ToString("X"), c3.Name);
All three Colors have the same Argb value but the first one has no common name.
This might suggest that if the user created a custom Color using the dialogue then it would never have a common name, but in some brief testing that didn't seem to be the case. You'd want to test it out yourself to make sure but I'd suggest that the ColorDialog first compares the selected Argb value to KnownColors and then calls FromKnownColor if a match exists.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|