|
| Argb Color to Know Color Name |
|
|
|
|
| Messages |
|
Related Types |
This message was discovered on microsoft.public.dotnet.languages.vb.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
| Samuel L Matzen |
Hi NG,
I have an Argb color stored in a database as a 32 bit integer.
If this color is a KnownColor, is there any way I can get the known color name so I can display the known color name on the form and in my reports?
The System.DrawingColor.ToKnowColor method will not do this for me.
Thanks in advance for any help.
-Sam Matzen
|
|
|
| |
|
| |
| |
| Peter Huang (VIP) |
Hi Samuel,
As the document said, Return Value An element of the KnownColor enumeration, if the Color structure is created from a pre-defined color by using either the FromName method or the FromKnownColor method; otherwise, zero.
Remarks A pre-defined color is also called a known color and is represented by an element of the KnownColor enumeration. When the ToKnownColor method is applied to a Color structure that is created by using the FromArgb method, the ToKnownColor method returns zero, even if the ARGB value matches the ARGB value of a pre-defined color. The ToKnownColor method also returns zero when it is applied to a Color structure that is created by using the FromName method with an invalid string name.
[link] http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ frlrfSystemDrawingColorClassToKnownColorTopic.asp
So I think we can compare the argb value with the knowncolors' to know which knowncolor matched current color.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim kc As Color = Color.FromKnownColor(KnownColor.Blue) Dim cr As Color = Color.FromArgb(kc.ToArgb()) Dim c As KnownColor For i As Integer = 1 To 137 If Color.FromKnownColor(i).ToArgb() = cr.ToArgb() Then Debug.WriteLine(CType(i, KnownColor).ToString()) End If Next End Sub
So you will use the method very frequently, we can build a hashtable with argb to knowncolor name mapping, so that the performance will be better.
Best regards,
Peter Huang Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security This posting is provided "AS IS" with no warranties, and confers no rights.
|
|
|
| |
|
|
| |
| | |
|
| | |
| |
| rawCoder |
Hi Samuel,
While searchig your problem on newsgroups .. i found a simple (not best) solution
public bool GetKnownColor(int iARGBValue, out string strKnownColor) { Color someColor;
Array aListofKnownColors = Enum.GetValues(typeof(KnownColor)); foreach (KnownColor eKnownColor in aListofKnownColors) { someColor = Color.FromKnownColor(eKnownColor); if (iARGBValue == someColor.ToArgb() && !someColor.IsSystemColor) { strKnownColor = someColor.Name; return true; } } strKnownColor = ""; return false; }
Thanx to Robert Hachtel (Click here to reveal e-mail address) on microsoft.public.dotnet.framework.drawing who posted this on 2002-02-20 11:49:37 PST in response to How can find if a given ARGB value is a known color?
btw .. the hashtable thing that Peter Huang recommended is the ideal thing.
I hope u will take time to convert it to VB ... and that wont be much of a problem ...
Thank You, rawCoder
"Samuel L Matzen" <Click here to reveal e-mail address> wrote in message news:%Click here to reveal e-mail address... [Original message clipped]
|
|
|
| |
|
| |
| | |
|
|
|
|
|
|
|
|
BootFX
Reliable and powerful .NET application framework. |
|
|
|
|
|
|