如何从十六进制颜色代码(例如#FFDFD991)中获得颜色?

我正在读取一个文件,并得到一个十六进制的颜色代码。我需要为十六进制颜色代码创建相应的System.Windows.Media.Color实例。框架中是否有一个内置的方法来做到这一点?


当前回答

如果你想用Windows商店应用程序,后面跟着@Hans Kesting和@Jink回答:

    string colorcode = "#FFEEDDCC";
    int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
    tData.DefaultData = Color.FromArgb((byte)((argb & -16777216) >> 0x18),
                          (byte)((argb & 0xff0000) >> 0x10),
                          (byte)((argb & 0xff00) >> 8),
                          (byte)(argb & 0xff));

其他回答

WPF:

using System.Windows.Media;

//hex to color
Color color = (Color)ColorConverter.ConvertFromString("#7AFF7A7A");

//color to hex
string hexcolor = color.ToString();

这篇文章已经成为任何试图从十六进制颜色代码转换为系统颜色的人的首选。因此,我认为我应该添加一个全面的解决方案,处理6位(RGB)和8位(ARGB)十六进制值。

默认情况下,根据微软,当从RGB转换为ARGB值

alpha值隐式为255(完全不透明)。

这意味着通过将FF添加到6位(RGB)十六进制颜色代码中,它就变成了8位ARGB十六进制颜色代码。因此,可以创建一个简单的方法来处理ARGB和RGB十六进制,并将它们转换为适当的颜色结构。

    public static System.Drawing.Color GetColorFromHexValue(string hex)
    {
        string cleanHex = hex.Replace("0x", "").TrimStart('#');

        if (cleanHex.Length == 6)
        {
            //Affix fully opaque alpha hex value of FF (225)
            cleanHex = "FF" + cleanHex;
        }

        int argb;

        if (Int32.TryParse(cleanHex, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out argb))
        {
            return System.Drawing.Color.FromArgb(argb);
        }

        //If method hasn't returned a color yet, then there's a problem
        throw new ArgumentException("Invalid Hex value. Hex must be either an ARGB (8 digits) or RGB (6 digits)");

    }

这是受到汉斯·凯斯汀的回答的启发。

如果你想用Windows商店应用程序,后面跟着@Hans Kesting和@Jink回答:

    string colorcode = "#FFEEDDCC";
    int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
    tData.DefaultData = Color.FromArgb((byte)((argb & -16777216) >> 0x18),
                          (byte)((argb & 0xff0000) >> 0x10),
                          (byte)((argb & 0xff00) >> 8),
                          (byte)(argb & 0xff));

XNA / Monogame (Microsoft.Xna.Framework.Color)。 适用于6或8(带alpha)字符的十六进制字符串 可能还有更好的替代方案(位屏蔽/移位)。

    using Microsoft.Xna.Framework;
    using System.Globalization;
    
    public static class ColorBuilder
    {
        public static Color FromHex(string color)
        {
            var hex = color.Replace("#", string.Empty);
            var h = NumberStyles.HexNumber;

            var r = int.Parse(hex.Substring(0, 2), h);
            var g = int.Parse(hex.Substring(2, 2), h);
            var b = int.Parse(hex.Substring(4, 2), h);
            var a = 255;

            if (hex.Length == 8)
            {
                a = int.Parse(hex.Substring(6, 2), h);
            }
 
            return new Color(r, g, b, a);
        }
    }
    
    //create a blue color
    var color = ColorBuilder.FromHex("#2733C5"); //or ColorBuilder.FromHex("2733C5");
    
    //create a blue color with 50% alpha
    var colorTrans = ColorBuilder.FromHex("#2733C580");

如果你不想使用ColorTranslator,你可以很容易地做到:

string colorcode = "#FFFFFF00";
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);

colorcode只是ARGB值的十六进制表示。

EDIT

如果你需要使用4个值而不是一个整数,你可以使用这个(结合几个注释):

string colorcode = "#FFFFFF00";    
colorcode = colorcode.TrimStart('#');

Color col; // from System.Drawing or System.Windows.Media
if (colorcode.Length == 6)
    col = Color.FromArgb(255, // hardcoded opaque
                int.Parse(colorcode.Substring(0,2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(2,2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(4,2), NumberStyles.HexNumber));
else // assuming length of 8
    col = Color.FromArgb(
                int.Parse(colorcode.Substring(0, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(2, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(4, 2), NumberStyles.HexNumber),
                int.Parse(colorcode.Substring(6, 2), NumberStyles.HexNumber));

注1:NumberStyles在System.Globalization中。 注2:请提供您自己的错误检查(colorcode应该是6或8个字符的十六进制值)