Reputation: 362
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
Reputation: 29000
You can use this function
private string ConvertColorStringToHex(string colorString){
return Color.FromName(colorString).ToArgb().ToString("X8").Substring(2,6);
}
Upvotes: 0
Reputation: 517
Color c = Color.Red;
string hex = c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2");
Upvotes: 0
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