I have a Xamarin.Forms.Color and I want to convert it to a \'hex value\'.
So far, I haven\'t found a solution to my problem.
My code is as follows:
a bit late but here is how I do that in Xamarin Forms (the Xamarin.Forms.Color class already exposes a FromHex(string) method.
public string ColorHexa { get; set; }
public Color Color
{
get => Color.FromHex(ColorHexa);
set => ColorHexa = value.ToHexString();
}
With this extension :
public static class ColorExtensions
{
public static string ToHexString(this Color color, bool outputAlpha = true)
{
string DoubleToHex(double value)
{
return string.Format("{0:X2}", (int)(value * 255));
}
string hex = "#";
if (outputAlpha) hex += DoubleToHex(color.A);
return $"{hex}{DoubleToHex(color.R)}{DoubleToHex(color.G)}{DoubleToHex(color.B)}";
}
}