public static string ToHexColor(string rgbColor)
{
try
{
//need leading zeros ?
if (rgbColor.Length < 9)
//fill up with leading zeros
rgbColor = rgbColor.PadLeft(9, '0');
//our rgb values
int rValue = Convert.ToInt32(rgbColor.Substring(0,3));
int gValue = Convert.ToInt32(rgbColor.Substring(3,3));
int bValue = Convert.ToInt32(rgbColor.Substring(6));
//convert into hex
string srValue = ((rValue!=0)?rValue.ToString("x"):"00");
string sgValue = ((gValue!=0)?gValue.ToString("x"):"00");
string sbValue = ((bValue!=0)?bValue.ToString("x"):"00");
//build hex color
string hexColor = "#"
//fill with leading zeros, if needed
+ srValue.PadLeft(2, '0')
+ sgValue.PadLeft(2, '0')
+ sbValue.PadLeft(2, '0');
return hexColor;
}
catch(Exception)
{
return "#000000"; // black as default,
}
}