Solved

# Color conversion?

Posted on 2006-05-12
681 Views
Hi
I have a colour

When I hover over mycolor it says

mycolor | "{Name=ffff0000, ARGB=(255, 255, 0, 0)}"

And when I expand mycolor I see
A 255
B 0
G 0
R 255

Does any one know how I'd convert this to the named value of the color?
In this case 'Red'?

Also - how do I get at the value Name=ffff0000, ARGB=(255, 255, 0, 0)?
I though it would be
mycolor.value?

Thanks
Paul
0
Question by:paulwhelan

LVL 40

Expert Comment

Hi paulwhelan,

The names are NOT generally fixed. The names used for the web are fixed. You can get the list of web colours from http://www.w3schools.com/html/html_colornames.asp

Regards,

0

LVL 10

Expert Comment

Hi,

For some reason, if you create a color using FromArgb, it doesn't know that it's a certain color, but running the color through this coversion will sort it out.

private static Color GetRealColor(Color InCol)
{
foreach (KnownColor Col in Enum.GetValues(typeof(KnownColor)))
{
if (Color.FromKnownColor(Col).ToArgb() == InCol.ToArgb()) return Color.FromKnownColor(Col);
}
return InCol;
}

Hope this helps
0

LVL 10

Expert Comment

Also,

Calling the 'ToString()' method of the color will return the string you're after.
0

Author Comment

BTW if I do this

byte bRed = 0;
byte bGreen= 0;
byte bBlue= 0;
Color.FromArgb(bRed, bGreen, bBlue);

I get this

-            Color.FromArgb(bRed, bGreen, bBlue)      "{Name=ff000000, ARGB=(255, 0, 0, 0)}"      System.Drawing.Color

0

LVL 10

Expert Comment

Yes, that's what I was saying, but if you run it through the function I provided above, it will covert it to a color structure which has 'Red' or 'Black" in that case.

Smg.
0

Author Comment

Would you be able to look through this class and see if theres anything that might be doing the same / similar?

I dont want to replicate anything.

ColorHasARealName
and
GetRealColor

Thanks
Paul

using System;
using System.Drawing;
using System.Xml;

namespace Veriton.Utilities
{

public class Colour : ICloneable, IXmlSerializable
{
static public Colour FromString(string colour)
{
if (colour.Length == 0) return null;
return new Colour(colour);
}

static public Colour BLACK = new Colour(0,0,0);
static public Colour RED = new Colour(255,0,0);
static public Colour GREEN = new Colour(0, 255, 0);
static public Colour BLUE = new Colour(0, 0, 255);
static public Colour WHITE = new Colour(255,255,255);

// CONSTRUCTORS

public Colour()
{
_iRed = 0;
_iGreen = 0;
_iBlue = 0;
}

public Colour(byte red,byte green,byte blue)
{
_iRed = red;
_iGreen = green;
_iBlue = blue;
}

public Colour(Colour colour)
{
_iRed = colour._iRed;
_iGreen = colour._iGreen;
_iBlue = colour._iBlue;
}

public Colour(string colour)
{
// This if should not be needed.
if (colour == "")
return;
else if (colour == "Fuchsia" || colour == "Magenta")
colour = "#FF00FF";
else if (colour == "Red")
colour = "#FF0000";
else if (colour == "Yellow")
colour = "#FFFF00";
else if (colour == "Lime")
colour = "#00FF00";
else if (colour == "Aqua" || colour == "Cyan")
colour = "#00FFFF";
else if (colour == "Maroon")
colour = "#800000";
else if (colour == "Green")
colour = "#008000";
else if (colour == "Blue")
colour = "#0000FF";
else if (colour == "Purple")
colour = "#800080";
else if (colour == "Teal")
colour = "#008080";
else if (colour == "Navy")
colour = "#000080";
else if (colour == "Black")
colour = "#000000";
else if (colour == "Olive")
colour = "#808000";
else if (colour == "Gray")
colour = "#808080";
else if (colour == "Silver")
colour = "#C0C0C0";
else if (colour == "White")
colour = "#FFFFFF";
else if (colour[0] != '#' || colour.Length != 7)
throw new ApplicationException("Colour string consists of a # followed by hexadecimal red, green, and blue.");

_iRed = (byte)Int32.Parse(colour.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
_iGreen = (byte)Int32.Parse(colour.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
_iBlue = (byte)Int32.Parse(colour.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}

public Colour(XmlElement xmlElement)
{
_iRed = (byte)XmlUtilities.GetElementValueAsInteger(xmlElement,"Red");
_iGreen = (byte)XmlUtilities.GetElementValueAsInteger(xmlElement, "Green");
_iBlue = (byte)XmlUtilities.GetElementValueAsInteger(xmlElement, "Blue");
}

// METHODS

//This method returns a System Color
public Color GetSystemColour()
{
return Color.FromArgb(Red, Green, Blue);
}

static public Colour Average(Colour colourA, Colour colourB)
{
if (colourA == null && colourB == null) return null;
if (colourA == null) return colourB;
if (colourB == null) return colourA;
return new Colour((byte)((colourA._iRed + colourB._iRed) / 2), (byte)((colourA._iGreen + colourB._iGreen) / 2), (byte)((colourA._iBlue + colourB._iBlue) / 2));
}

public override string ToString()
{
return "#" +
Red.ToString("x2") +
Green.ToString("x2") +
Blue.ToString("x2");
}

public void Save(XmlElement xmlElement)
{
XmlUtilities.AppendElement(xmlElement, "Red", _iRed.ToString());
XmlUtilities.AppendElement(xmlElement, "Green", _iGreen.ToString());
XmlUtilities.AppendElement(xmlElement, "Blue", _iBlue.ToString());
}

/// <summary>
/// Convert the name of a color to a knowncolor (e.g. ffffff00 to Yellow)
/// </summary>
/// <param name="InCol"></param>
/// <returns></returns>
static public Color GetRealColor(Color InCol)
{
foreach (KnownColor Col in Enum.GetValues(typeof(KnownColor)))
{
if (Color.FromKnownColor(Col).ToArgb() == InCol.ToArgb())
return Color.FromKnownColor(Col);
}
return InCol;
}

/// <summary>
/// Find out if the color has a real name (e.g. 'Black' or 'Lime')
/// </summary>
/// <param name="InCol"></param>
/// <returns></returns>
static public bool ColorHasARealName(Color InCol)
{
foreach (KnownColor Col in Enum.GetValues(typeof(KnownColor)))
{
if (Color.FromKnownColor(Col).ToArgb() == InCol.ToArgb()) return true;
}
return false;
}

// PROPERTIES

public byte Red { get { return _iRed; } set { _iRed = value; } }
public byte Green { get { return _iGreen; } set { _iGreen = value; } }
public byte Blue { get { return _iBlue; } set { _iBlue = value; } }

// PRIVATE

private byte _iRed,_iGreen,_iBlue;

#region ICloneable Members

public object Clone()
{
return MemberwiseClone();
}

#endregion
}
}
0

LVL 10

Expert Comment

You could replace your Color(string) constructor to just use

Color.FromName("Red")

Which returns the color which matches the text.

Smg.
0

Author Comment

BTW with this...

static public Color GetRealColor(Color InCol)
{
foreach (KnownColor Col in Enum.GetValues(typeof(KnownColor)))
{
if (Color.FromKnownColor(Col).ToArgb() == InCol.ToArgb())
return Color.FromKnownColor(Col);
}
return InCol;
}

If I select a Color black / #000000

it returns 'ControlText'

Any ideas why?
Thanks
Paul
0

LVL 10

Accepted Solution

Ahh.. becuase the knowncolors enumerator can contain multiple descriptions for one color.

Change the if statement to...

if ((!Color.FromKnownColor(Col).IsSystemColor) && (Color.FromKnownColor(Col).ToArgb() == InCol.ToArgb()))

Smg
0

Author Comment

Great thanks ...

So whats the logic there?

If its not a system color and ...

Thanks
0

LVL 10

Expert Comment

The same as before..

if it's not a systemcolor, and the argb values match, then use that color.. it excludes things like 'ControlLight' etc..

0

Author Comment

Once again thanks.

One final question.

Instead of having two functions - one to check if a name for the color exists and one to return the name I tried to change the function to return null.

static public Color GetRealColor(Color InCol)
{
foreach (KnownColor Col in Enum.GetValues(typeof(KnownColor)))
{
//If its not a systemcolor and the argb values match, then use that color. This excludes things like 'ControlLight' etc.
if ((!Color.FromKnownColor(Col).IsSystemColor) && (Color.FromKnownColor(Col).ToArgb() == InCol.ToArgb()))
return Color.FromKnownColor(Col);
}
//return InCol;
return null;
}

But it gives me an error.

Error      12      Cannot convert null to 'System.Drawing.Color' because it is a value type

Thanks
Paul
0

LVL 10

Expert Comment

the best thing to do is return 'Color.Empty', which you can then compare against... ie.

Color mine=GetRealColor("money");
if (mine==Color.Empty) Console.WriteLine("Don't understand the color of money");
else
{
// Whatever
}
0

## Featured Post

Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…