C# 如何使用 .NET 从十六进制颜色代码中获取颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2109756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:35:04  来源:igfitidea点击:

How do I get the color from a hexadecimal color code using .NET?

c#wpfcolorshex

提问by viky

How can I get a color from a hexadecimal color code (e.g. #FFDFD991)?

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

I am reading a file and am getting a hexadecimal color code. I need to create the corresponding System.Windows.Media.Colorinstance for the hexadecimal color code. Is there an inbuilt method in the framework to do this?

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

采纳答案by Thorarin

I'm assuming that's an ARGB code... Are you referring to System.Drawing.Coloror System.Windows.Media.Color? The latter is used in WPF for example. I haven't seen anyone mention it yet, so just in case you were looking for it:

我假设这是一个 ARGB 代码......你指的是System.Drawing.Color还是System.Windows.Media.Color?例如,后者在 WPF 中使用。我还没有看到任何人提到它,所以以防万一你正在寻找它:

using System.Windows.Media;

Color color = (Color)ColorConverter.ConvertFromString("#FFDFD991");

回答by Oded

Assuming you mean the HTML type RGB codes (called Hex codes, such as #FFCC66), use the ColorTranslatorclass:

假设您指的是 HTML 类型的 RGB 代码(称为十六进制代码,例如 #FFCC66),请使用ColorTranslator类:

System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#FFCC66");

If, however you are using an ARGB hex code, you can use the ColorConverterclass from the System.Windows.Media namespace:

但是,如果您使用的是 ARGB 十六进制代码,则可以使用System.Windows.Media 命名空间中的ColorConverter类:

Color col = ColorConverter.ConvertFromString("#FFDFD991") as Color;
//or      = (Color) ColorConverter.ConvertFromString("#FFCC66") ;

回答by Wim Hollebrandse

If you mean HashCode as in .GetHashCode(), I'm afraid you can't go back. Hash functions are not bi-directional, you can go 'forward' only, not back.

如果你的意思是 HashCode .GetHashCode(),恐怕你回不去了。散列函数不是双向的,您只能“前进”,而不能后退。

Follow Oded's suggestion if you need to get the color based on the hexadecimal value of the color.

如果需要根据颜色的十六进制值获取颜色,请遵循 Oded 的建议。

回答by herzmeister

Use

System.Drawing.Color.FromArgb(myHashCode);

回答by Hans Ke?ing

If you don't want to use the ColorTranslator, you can do it in easily:

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

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

The colorcode is just the hexadecimal representation of the ARGB value.

颜色代码只是 ARGB 值的十六进制表示。

EDIT

编辑

If you need to use 4 values instead of a single integer, you can use this (combining several comments):

如果您需要使用 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));

Note 1: NumberStyles is in System.Globalization.
Note 2: please provide your own error checking (colorcode should be a hexadecimal value of either 6 or 8 characters)

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

回答by Pengzhi

You could use the following code:

您可以使用以下代码:

Color color = System.Drawing.ColorTranslator.FromHtml("#FFDFD991");

回答by Jink

There is also this neat little extension method:

还有这个简洁的小扩展方法:

static class ExtensionMethods
{
    public static Color ToColor(this uint argb)
    {
        return Color.FromArgb((byte)((argb & -16777216)>> 0x18),      
                              (byte)((argb & 0xff0000)>> 0x10),   
                              (byte)((argb & 0xff00) >> 8),
                              (byte)(argb & 0xff));
    }
}

In use:

正在使用:

Color color = 0xFFDFD991.ToColor();

回答by Mwaffak Jamal Zakariya

You can see Silverlight/WPF sets ellipse with hexadecimal colourfor using a hex value:

您可以看到Silverlight/WPF 使用十六进制颜色设置椭圆以使用十六进制值:

your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)

回答by demp

The three variants below give exactly the same color. The last one has the benefit of being highlighted in the Visual Studio 2010 IDE (maybe it's ReSharper that's doing it) with proper color.

下面的三个变体给出了完全相同的颜色。最后一个的好处是可以在 Visual Studio 2010 IDE 中以适当的颜色突出显示(可能是 ReSharper 这样做)。

var cc1 = System.Drawing.ColorTranslator.FromHtml("#479DEE");

var cc2 = System.Drawing.Color.FromArgb(0x479DEE);

var cc3 = System.Drawing.Color.FromArgb(0x47, 0x9D, 0xEE);

回答by DevXP

WPF:

WPF:

using System.Windows.Media;

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

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