Argb Color to Know Color Name

  • Thread starter Thread starter Samuel L Matzen
  • Start date Start date
S

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
 
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.
 
Hi Samuel,

I am afraid I dont know how to solve this problem, but I can tell you why
the ToKnownColor doesnt work. It does this due to the following reason
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.

got from MSDN
http://msdn.microsoft.com/library/d...fsystemdrawingcolorclasstoknowncolortopic.asp



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 ([email protected]) 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
 
Back
Top