By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,144 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,144 IT Pros & Developers. It's quick & easy.

Argb Color to Know Color Name

P: n/a
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


Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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/de...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.

Nov 20 '05 #2

P: n/a
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/de...colortopic.asp

rawCoder

"Samuel L Matzen" <sm*****@slm.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #3

P: n/a
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 (ro***********@panoramicsci.com) 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" <sm*****@slm.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #4

P: n/a
Peter,

Thanks, that is what I was looking for.

-Sam Matzen
""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:JJ**************@cpmsftngxa10.phx.gbl...
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/de...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.

Nov 20 '05 #5

P: n/a
rawCoder,

Thanks for the research, that is what I was looking for.

-Sam Matzen
"rawCoder" <ra******@hotmail.com> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
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 (ro***********@panoramicsci.com) 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" <sm*****@slm.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.