Maniarasu
Maniarasu

Reputation: 362

String color name to String Color Hex value in C#

i am trying to get the Hex value of color that is passed as string. for example,

private string HexColor(string colorName) // like "Red"
{

 // returns hex value like "12345"
}

i got links for convert a Windows.UI.Color to its Hex value. but not for a string color name to color hex value. Thanks in advance for your help.

Update : For metro app

Upvotes: 0

Views: 1639

Answers (3)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 29000

You can use this function

private string ConvertColorStringToHex(string colorString){
return Color.FromName(colorString).ToArgb().ToString("X8").Substring(2,6);
}

Upvotes: 0

Rob
Rob

Reputation: 517

Color c = Color.Red;
string hex = c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460278

Color.fromName creates a Color structure from the specified name of a predefined color, then ToArgb:

Color.FromName(colorString).ToArgb().ToString("X8").Substring(2,6);

ToString("X8") creates the hexadecimal value as string from an integral type.

Upvotes: 2

Related Questions