Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows runtime 如何在windows 8中将十六进制代码转换为颜色_Windows Runtime - Fatal编程技术网

Windows runtime 如何在windows 8中将十六进制代码转换为颜色

Windows runtime 如何在windows 8中将十六进制代码转换为颜色,windows-runtime,Windows Runtime,我有十六进制代码“FFB800”,我需要在WinRT中转换为“颜色” 提前感谢。在tweet中实现这一点的捷径: (Color)XamlReader.Load(string.Format("<Color xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation …\">{0}</Color>", c)); 这比使用XamlReader要快得多,所以如果您需要多次调用它,建议使用它。您还可以从以下位

我有十六进制代码“FFB800”,我需要在WinRT中转换为“颜色”


提前感谢。

在tweet中实现这一点的捷径:

(Color)XamlReader.Load(string.Format("<Color xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation …\">{0}</Color>", c));
这比使用
XamlReader
要快得多,所以如果您需要多次调用它,建议使用它。您还可以从以下位置进行克隆或复制粘贴:

#region FromString()
/// <summary>
/// Returns a Color based on XAML color string.
/// </summary>
/// <param name="c">The color string. Any format used in XAML should work.</param>
/// <returns></returns>
public static Color FromString(string c)
{
    if (string.IsNullOrEmpty(c))
        throw new ArgumentException("Invalid color string.", "c");

    if (c[0] == '#')
    {
        switch (c.Length)
        {
            case 9:
            {
                //var cuint = uint.Parse(c.Substring(1), NumberStyles.HexNumber);
                var cuint = Convert.ToUInt32(c.Substring(1), 16);
                var a = (byte)(cuint >> 24);
                var r = (byte)((cuint >> 16) & 0xff);
                var g = (byte)((cuint >> 8) & 0xff);
                var b = (byte)(cuint & 0xff);

                return Color.FromArgb(a, r, g, b);
            }
            case 7:
            {
                var cuint = Convert.ToUInt32(c.Substring(1), 16);
                var r = (byte)((cuint >> 16) & 0xff);
                var g = (byte)((cuint >> 8) & 0xff);
                var b = (byte)(cuint & 0xff);

                return Color.FromArgb(255, r, g, b);
            }
            case 5:
            {
                var cuint = Convert.ToUInt16(c.Substring(1), 16);
                var a = (byte)(cuint >> 12);
                var r = (byte)((cuint >> 8) & 0xf);
                var g = (byte)((cuint >> 4) & 0xf);
                var b = (byte)(cuint & 0xf);
                a = (byte)(a << 4 | a);
                r = (byte)(r << 4 | r);
                g = (byte)(g << 4 | g);
                b = (byte)(b << 4 | b);

                return Color.FromArgb(a, r, g, b);
            }
            case 4:
            {
                var cuint = Convert.ToUInt16(c.Substring(1), 16);
                var r = (byte)((cuint >> 8) & 0xf);
                var g = (byte)((cuint >> 4) & 0xf);
                var b = (byte)(cuint & 0xf);
                r = (byte)(r << 4 | r);
                g = (byte)(g << 4 | g);
                b = (byte)(b << 4 | b);

                return Color.FromArgb(255, r, g, b);
            }
            default:
                throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format.", c));
        }
    }
    else if (
        c.Length > 3 &&
        c[0] == 's' &&
        c[1] == 'c' &&
        c[2] == '#')
    {
        var values = c.Split(',');

        if (values.Length == 4)
        {
            var scA = double.Parse(values[0].Substring(3));
            var scR = double.Parse(values[1]);
            var scG = double.Parse(values[2]);
            var scB = double.Parse(values[3]);

            return Color.FromArgb(
                (byte)(scA * 255),
                (byte)(scR * 255),
                (byte)(scG * 255),
                (byte)(scB * 255));
        }
        else if (values.Length == 3)
        {
            var scR = double.Parse(values[0].Substring(3));
            var scG = double.Parse(values[1]);
            var scB = double.Parse(values[2]);

            return Color.FromArgb(
                255,
                (byte)(scR * 255),
                (byte)(scG * 255),
                (byte)(scB * 255));
        }
        else
        {
            throw new FormatException(string.Format("The {0} string passed in the c argument is not a recognized Color format (sc#[scA,]scR,scG,scB).", c));
        }
    }
    else
    {
        var prop = typeof(Colors).GetTypeInfo().GetDeclaredProperty(c);
        return (Color)prop.GetValue(null);
    }
}
#endregion
#区域FromString()
/// 
///返回基于XAML颜色字符串的颜色。
/// 
///颜色字符串。XAML中使用的任何格式都应该有效。
/// 
来自字符串的公共静态颜色(字符串c)
{
if(string.IsNullOrEmpty(c))
抛出新的ArgumentException(“无效的颜色字符串。”,“c”);
如果(c[0]='#')
{
开关(c.长度)
{
案例9:
{
//var cuint=uint.Parse(c.Substring(1),NumberStyles.HexNumber);
var cuint=Convert.ToUInt32(c.子字符串(1),16);
变量a=(字节)(提示>>24);
变量r=(字节)((电路>>16)和0xff);
变量g=(字节)((电路>>8)和0xff);
变量b=(字节)(提示和0xff);
返回颜色。来自argb(a,r,g,b);
}
案例7:
{
var cuint=Convert.ToUInt32(c.子字符串(1),16);
变量r=(字节)((电路>>16)和0xff);
变量g=(字节)((电路>>8)和0xff);
变量b=(字节)(提示和0xff);
返回颜色。来自argb(255,r,g,b);
}
案例5:
{
var cuint=Convert.ToUInt16(c.子字符串(1),16);
变量a=(字节)(提示>>12);
变量r=(字节)((电路>>8)和0xf);
变量g=(字节)((电路>>4)和0xf);
变量b=(字节)(cuint&0xf);
a=(字节)(a4)和0xf);
变量b=(字节)(cuint&0xf);

r=(byte)(r这个问题的目的是什么?在普通XAML中是否可以这样做?XAML使用十六进制代码


否则,在“代码隐藏”中,我在Windows 8应用程序中或多或少使用了以下内容:

var hexCode=“#FFFFB800”;
var color=新颜色();
color.A=byte.Parse(hexCode.Substring(1,2),NumberStyles.allowehexspecifier);
color.R=byte.Parse(hexCode.Substring(3,2),NumberStyles.allowehexspecifier);
color.G=byte.Parse(hexCode.Substring(5,2),NumberStyles.allowehexspecifier);
color.B=byte.Parse(hexCode.Substring(7,2),NumberStyles.allowehexspecifier);
如何为xaml矩形对象设置为填充

矩形填充=新的SolidColorBrush(颜色)

像这样的另一个解决方案可以工作,但返回的参数顺序不正确 如果只有6位十六进制而不是8位,只需将a设置为255即可:

public struct MyColor : Windows.UI.Color
{
    /// <summary>
    /// Convert hexdecimal value into color.
    /// </summary>
    /// <param name="hexCode">hexdecimal of color.</param>
    /// <returns></returns>
    public Windows.UI.Xaml.Media.Brush ColorToBrush(string hexCode)
    {
        hexCode = hexCode.Replace("#", "");

        if (hexCode.Length == 6)
            return new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.ColorHelper.FromArgb(255,
                byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
                byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
                byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.HexNumber)));
        else if (hexCode.Length == 8)
        {
            var color = new Windows.UI.Color();
            color.A = byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.R = byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.G = byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.B = byte.Parse(hexCode.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
        }

        return null;
    }
}
public struct MyColor:Windows.UI.Color
{
/// 
///将十六进制值转换为颜色。
/// 
///十六进制的颜色。
/// 
public Windows.UI.Xaml.Media.Brush ColorToBrush(字符串十六进制代码)
{
hexCode=hexCode。替换(“#”和“”);
if(hexCode.Length==6)
返回新的Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.ColorHelper.FromArgb(255,
Parse(hexCode.Substring(0,2),System.Globalization.NumberStyles.HexNumber),
Parse(hexCode.Substring(2,2),System.Globalization.NumberStyles.HexNumber),
Parse(hexCode.Substring(4,2),System.Globalization.NumberStyles.HexNumber));
else if(hexCode.Length==8)
{
var color=newwindows.UI.color();
color.A=byte.Parse(hexCode.Substring(0,2),System.Globalization.NumberStyles.allowehexspecifier);
color.R=byte.Parse(hexCode.Substring(2,2),System.Globalization.NumberStyles.allowehexspecifier);
color.G=byte.Parse(hexCode.Substring(4,2),System.Globalization.NumberStyles.allowehexspecifier);
color.B=byte.Parse(hexCode.Substring(6,2),System.Globalization.NumberStyles.allowehexspecifier);
}
返回null;
}
}

根据我目前的发现,似乎只能在WinRT中从单独的RGB组件创建一个
Color
。因此,您可以将其解析为int,然后分离出字节,然后创建一个
Color
(这可能会在内部再次将它们合并为单个int.)我想知道为什么有人会否决这个问题……尤其是在没有评论和失去声誉的情况下。你不需要那些
&0xff
的顺便说一句,对
字节的转换已经解决了这个问题
        var hexCode = "#FFFFB800";
        var color = new Color();
        color.A = byte.Parse(hexCode.Substring(7, 2), NumberStyles.AllowHexSpecifier);
        color.R = byte.Parse(hexCode.Substring(1, 2), NumberStyles.AllowHexSpecifier);
        color.G = byte.Parse(hexCode.Substring(3, 2), NumberStyles.AllowHexSpecifier);
        color.B = byte.Parse(hexCode.Substring(5, 2), NumberStyles.AllowHexSpecifier);
public struct MyColor : Windows.UI.Color
{
    /// <summary>
    /// Convert hexdecimal value into color.
    /// </summary>
    /// <param name="hexCode">hexdecimal of color.</param>
    /// <returns></returns>
    public Windows.UI.Xaml.Media.Brush ColorToBrush(string hexCode)
    {
        hexCode = hexCode.Replace("#", "");

        if (hexCode.Length == 6)
            return new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.ColorHelper.FromArgb(255,
                byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
                byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
                byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.HexNumber)));
        else if (hexCode.Length == 8)
        {
            var color = new Windows.UI.Color();
            color.A = byte.Parse(hexCode.Substring(0, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.R = byte.Parse(hexCode.Substring(2, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.G = byte.Parse(hexCode.Substring(4, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
            color.B = byte.Parse(hexCode.Substring(6, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
        }

        return null;
    }
}