在 Windows Phone 运行时 c# 中将字符串 HEX 转换为颜色

Converting a string HEX to color in Windows Phone Runtime c#

我正在开发 windows phone 游戏,当我想将 HEX 字符串转换为 Color 时卡住了。在 windows phone 8 silverlight 上这不是问题,但我无法在运行时找到解决方案,因为它不包含 Color.FromArgb 或 Color.FromName 函数。

有人有将字符串 HEX 转换为 Color 的函数吗?

谢谢。

您可以使用

var color = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));

Color.FromArgb 在 Windows.UI 命名空间中。没有 Color.FromName 方法,但您可以使用 Colors. 属性,或者您可以使用反射从字符串中查找名称。

using System.Reflection;     // For GetRuntimeProperty
using System.Globalization;  // For NumberStyles
using Windows.UI;            // for Color and Colors
using Windows.UI.Xaml.Media; // for SystemColorBrush

// from #AARRGGBB string
byte a = byte.Parse(hexColor.Substring(1, 2),NumberStyles.HexNumber);
byte r = byte.Parse(hexColor.Substring(3, 2),NumberStyles.HexNumber);
byte g = byte.Parse(hexColor.Substring(5, 2),NumberStyles.HexNumber);
byte b = byte.Parse(hexColor.Substring(7, 2),NumberStyles.HexNumber);

Windows.UI.Color color = Color.FromArgb(a, r, g, b);
Windows.UI.Xaml.Media.SolidColorBrush br = new SolidColorBrush(color);

// From Name
var prop = typeof(Windows.UI.Colors).GetRuntimeProperty("Aqua");
if (prop != null)
{
    Color c = (Color) prop.GetValue(null);
    br = new SolidColorBrush(c);
}

// From Property
br = new SolidColorBrush(Colors.Aqua);

以防万一有人正在寻找更好的选择。 Universal Windows Platform (Windows 10)中有XamlBindingHelper.ConvertValue,聊胜于无

// Get a Color instance representing #FFFF0000.
var color = XamlBindingHelper.ConvertValue(typeof(Windows.UI.Color), "red");

它可以从 Windows.UI.Xaml 命名空间、布尔值、画笔、颜色和其他简单的东西转换枚举 XAML 解析器能够做到。

我根据这里的最佳答案做了一个方法:

private Windows.UI.Color GetColorFromHex(string hexString)
{
    hexString = hexString.Replace("#", string.Empty);
    byte r = byte.Parse(hexString.Substring(0, 2), NumberStyles.HexNumber);
    byte g = byte.Parse(hexString.Substring(2, 2), NumberStyles.HexNumber);
    byte b = byte.Parse(hexString.Substring(4, 2), NumberStyles.HexNumber);

    return Windows.UI.Color.FromArgb(byte.Parse("1"), r, g, b);
}

Final 解决方案,同样适用于 UWP:

 public static Color GetColorFromHex(string hexString) {
            //add default transparency to ignore exception
            if ( !string.IsNullOrEmpty(hexString) && hexString.Length > 6 ) {
                if ( hexString.Length == 7 ) {
                    hexString = "FF" + hexString;
                }

                hexString = hexString.Replace("#", string.Empty);
                byte a = (byte) ( Convert.ToUInt32(hexString.Substring(0, 2), 16) );
                byte r = (byte) ( Convert.ToUInt32(hexString.Substring(2, 2), 16) );
                byte g = (byte) ( Convert.ToUInt32(hexString.Substring(4, 2), 16) );
                byte b = (byte) ( Convert.ToUInt32(hexString.Substring(6, 2), 16) );
                Color color = Color.FromArgb(a, r, g, b);
                return color;
            }

            //return black if hex is null or invalid
            return Color.FromArgb(255, 0, 0, 0);
        }

在 C# 中将十六进制转换为通用 Windows 平台 (UWP) 的颜色

创建将十六进制字符串转换为 SolidColorBrush 的方法:

  public SolidColorBrush GetSolidColorBrush(string hex)
    {
        hex = hex.Replace("#", string.Empty);
        byte a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
        byte r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
        byte g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
        byte b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16));
        SolidColorBrush myBrush = new SolidColorBrush(Windows.UI.Color.FromArgb(a, r, g, b));
        return myBrush;
    }

现在剩下的就是通过调用方法获取颜色并将十六进制字符串作为参数传递给它:

  var color = GetSolidColorBrush("#FFCD3927").Color;  

参考: http://www.joeljoseph.net/converting-hex-to-color-in-universal-windows-platform-uwp/

这是一个易于使用的代码片段

public Color HexColor(String hex)
{
 //remove the # at the front
 hex = hex.Replace("#", "");

 byte a = 255;
 byte r = 255;
 byte g = 255;
 byte b = 255;

 int start = 0;

 //handle ARGB strings (8 characters long)
 if (hex.Length == 8)
 {
     a = byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
     start = 2;
 }

 //convert RGB characters to bytes
 r = byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber);
 g = byte.Parse(hex.Substring(start+2, 2), System.Globalization.NumberStyles.HexNumber);
 b = byte.Parse(hex.Substring(start+4, 2), System.Globalization.NumberStyles.HexNumber);

 return Color.FromArgb(a, r, g, b);
}

你可以像下面这样简单地调用

Color c = HexColor("#99ccff");

我在 winphone 8.1 上测试过,可以用

Reference

生成文件即:ColorUtils.cs

using Windows.UI.Xaml.Markup;
....
 static class ColorUtils
    {
        public static Color GetColorFromHex(string hexString)
        {
            Color x =(Color)XamlBindingHelper.ConvertValue(typeof(Color), hexString);
            return x;
        }
    }

并像这样使用它

var acolor=(new SolidColorBrush(ColorUtils.GetColorFromHex("#F24C27")):

或使用 alpha

var acolor=(new SolidColorBrush(ColorUtils.GetColorFromHex("#4CF24C27")):