我有一个 Xamarin.Forms.Color,我想将它转换为“十六进制值”。
到目前为止,我还没有找到解决问题的办法。
我的代码如下:
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
};
}
最佳答案
只是快速修复,最后一行是错误的。
Alpha channel 先于其他值:
string hex = String.Format("#{0:X2}{1:X2}{2:X2}{3:X2}", alpha, red, green, blue);
这是最好的扩展方法:
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;
}
}
关于c# - Xamarin.Forms.Color 到十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40890242/