zperee zperee - 7 days ago 5
C# Question

Xamarin.Forms.Color to hex value

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:

foreach (var cell in Grid.Children)
{
var pixel = new Pixel
{
XAttribute = cell.X ,

YAttribute = cell.Y ,

// I want to convert the color to a hex value here

Color = cell.BackgroundColor

};

}

Answer
        var color = Xamarin.Forms.Color.Orange;
        int red = (int) (color.R * 255);
        int green = (int) (color.G * 255);
        int blue = (int) (color.B * 255);
        int alpha = (int)(color.A * 255);
        string hex = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", red, green, blue, alpha);