Topaz Filer: if you use e-mail for business, we can save you money and decrease your risk.
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

Reply to this message...
Vote that this is a GOOD answer...
 
Auto-following on Twitter
Ubuntu and XP on one “desktop”
 
    
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.

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Samuel L Matzen
Peter,

Thanks, that is what I was looking for.

-Sam Matzen

""Peter Huang"" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
rawCoder
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/default.asp?url=/library/en-us/cpref/html/frlrfsystemdrawingcolorclasstoknowncolortopic.asp

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]

Reply to this message...
Vote that this is a GOOD answer...
 
Outlook interop - stopping user properties appearing on Outlook message print
Seriously, why is “cut and paste” majorly newsworthy???
 
    
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]

Reply to this message...
Vote that this is a GOOD answer...
 
Email Archiving and Email Filing - what’s the difference?
Web-based task/todo list management
 
    
Samuel L Matzen
rawCoder,

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

-Sam Matzen

"rawCoder" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
 
System.Diagnostics.Debug
System.Drawing.Color
System.Drawing.KnownColor
System.Enum
System.EventArgs
System.Object




Ad
BootFX
Reliable and powerful .NET application framework.
Recession Busting Bespoke Software
Get through the recession by investing in bespoke software to decrease costs and create commercial opportunities.
Other DN247 Network Sites
.NET 247
SQL Server Wins
Old Skool Developer
 
Copyright © AMX Software Ltd 2008-2009. Portions copyright © Matthew Baxter-Reynolds 2001-2009. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - .NET 247 is a member of the DN247 Network - 4.0.30129.1734