c# - Xamarin.Forms.Color 到十六进制值

标签 c# xamarin colors xamarin.forms hex

我有一个 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/

相关文章:

xamarin - 未处理的异常 : System. TypeLoadException:无法使用 token 0100003b 解析类型

c# - 使用 Xamarin Forms 播放视频

c# - WPF:PropertyChangedCallback 只触发一次

c# - 在 ServiceStack 应用程序中使用时,Application Insights 不跟踪异常

ios - 如何在 Visual Studio for Mac 上更改 iOS 模拟器

javascript - 如何从CSS颜色中提取r、g、b、a值?

javascript - 色彩展示网站开发

python - 保持不同绘图的颜色缩放相同 - Python

c# - 如何将 GUID 属性设置为 null

c# - ASP.NET MVC,用于将验证消息从服务层验证传输到 View 模型的解决方案