Just a quick fix, the last line is incorrect.
The alpha channel comes before other values:
string hex = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", alpha, red, green, blue);
and this is best suited for the extension method:
public static class ExtensionMethods { public static string GetHexString(this Xamarin.Forms.Color color) { var red = (int)(color.R * 255); var green = (int)(color.G * 255); var blue = (int)(color.B * 255); var alpha = (int)(color.A * 255); var hex = $"#{alpha:X2}{red:X2}{green:X2}{blue:X2}"; return hex; } }
source share