将hex代码转换为颜色名称

我如何将这个hexa code = #2088C1转换成蓝色或红色的颜色名称

我的目标是我想获得给定六代码的颜色名称,如“蓝色”

我已经尝试了下面的代码,但它没有给任何颜色名称..

 System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#2088C1"); Color col = ColorConverter.ConvertFromString("#2088C1") as Color; 

但它不会给出这样的颜色名称“aquablue”

我正在使用winforms应用程序与c#

我偶然发现了一个完全符合你的要求的德国网站 :

 /// <summary> /// Gets the System.Drawing.Color object from hex string. /// </summary> /// <param name="hexString">The hex string.</param> /// <returns></returns> private System.Drawing.Color GetSystemDrawingColorFromHexString(string hexString) { if (!System.Text.RegularExpressions.Regex.IsMatch(hexString, @"[#]([0-9]|[af]|[AF]){6}\b")) throw new ArgumentException(); int red = int.Parse(hexString.Substring(1, 2), NumberStyles.HexNumber); int green = int.Parse(hexString.Substring(3, 2), NumberStyles.HexNumber); int blue = int.Parse(hexString.Substring(5, 2), NumberStyles.HexNumber); return Color.FromArgb(red, green, blue); } 

要获取颜色名称,您可以使用它来获得KnownColor :

 private KnownColor GetColor(string colorCode) { Color color = GetSystemDrawingColorFromHexString(colorCode); return color.GetKnownColor(); } 

使用这种方法

 Color myColor = ColorTranslator.FromHtml(htmlColor); 

另请参阅链接

这可以通过一些反思来完成。 没有优化,但它的工作原理:

 string GetColorName(Color color) { var colorProperties = typeof(Color) .GetProperties(BindingFlags.Public | BindingFlags.Static) .Where(p => p.PropertyType == typeof(Color)); foreach(var colorProperty in colorProperties) { var colorPropertyValue = (Color)colorProperty.GetValue(null, null); if(colorPropertyValue.R == color.R && colorPropertyValue.G == color.G && colorPropertyValue.B == color.B) { return colorPropertyValue.Name; } } //If unknown color, fallback to the hex value //(or you could return null, "Unkown" or whatever you want) return ColorTranslator.ToHtml(color); } 

我只是想出了这个:

 enum MatchType { NoMatch, ExactMatch, ClosestMatch }; static MatchType FindColour (Color colour, out string name) { MatchType result = MatchType.NoMatch; int least_difference = 0; name = ""; foreach (PropertyInfo system_colour in typeof (Color).GetProperties (BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy)) { Color system_colour_value = (Color) system_colour.GetValue (null, null); if (system_colour_value == colour) { name = system_colour.Name; result = MatchType.ExactMatch; break; } int a = colour.A - system_colour_value.A, r = colour.R - system_colour_value.R, g = colour.G - system_colour_value.G, b = colour.B - system_colour_value.B, difference = a * a + r * r + g * g + b * b; if (result == MatchType.NoMatch || difference < least_difference) { result = MatchType.ClosestMatch; name = system_colour.Name; least_difference = difference; } } return result; } static void Main (string [] args) { string colour; MatchType match_type = FindColour (Color.FromArgb (0x2088C1), out colour); Console.WriteLine (colour + " is the " + match_type.ToString ()); match_type = FindColour (Color.AliceBlue, out colour); Console.WriteLine (colour + " is the " + match_type.ToString ()); } 

没有现成的function。 您将必须遍历已知颜色的列表,并将每种已知颜色的RGB与未知的RGB进行比较。

看看这个链接: http : //bytes.com/topic/visual-basic-net/answers/365789-argb-color-know-color-name

如果您有权访问SharePoint程序集,则Microsoft.SharePoint将包含一个Microsoft.SharePoint.Utilities.ThemeColor类,其中包含一个静态方法GetScreenNameForColor ,该对象接受一个System.Drawing.Color对象并返回一个描述该对象的string 。 有大约20个不同的颜色名称,可以返回光照和黑暗的变化。

这是一个旧的post,但这里是一个优化的颜色到KnownColor转换器,因为内置的.NET ToKnownColor()不能正确使用adhoc Color结构。 当你第一次调用这个代码时,它会延迟加载已知的颜色值,并且轻微的perf命中。 顺序调用函数是一个简单的字典查找和快速。

 public static class ColorExtensions { private static Lazy<Dictionary<uint, KnownColor>> knownColors = new Lazy<Dictionary<uint, KnownColor>>(() => { Dictionary<uint, KnownColor> @out = new Dictionary<uint, KnownColor>(); foreach (var val in Enum.GetValues(typeof(KnownColor))) { Color col = Color.FromKnownColor((KnownColor)val); @out[col.PackColor()] = (KnownColor)val; } return @out; }); /// <summary>Packs a Color structure into a single uint (argb format).</summary> /// <param name="color">The color to package.</param> /// <returns>uint containing the packed color.</returns> public static uint PackColor(this Color color) => (uint)((color.A << 24) | (color.R << 16) | (color.G << 8) | (color.B << 0)); /// <summary>Unpacks a uint containing a Color structure.</summary> /// <param name="color">The color to unpackage.</param> /// <returns>A new Color structure containing the color defined by color.</returns> public static Color UnpackColor(this uint color) => Color.FromArgb((byte)(color >> 24), (byte)(color >> 16), (byte)(color >> 8), (byte)(color >> 0)); /// <summary>Gets the name of the color</summary> /// <param name="color">The color to get the KnownColor for.</param> /// <returns>A new KnownColor structure.</returns> public static KnownColor? GetKnownColor(this Color color) { KnownColor @out; if (knownColors.Value.TryGetValue(color.PackColor(), out @out)) return @out; return null; } } 

如果你正在寻找颜色的名字,你可以做到这一点,而不需要通过以下步骤将颜色转换为hex:

 Color c = (Color) yourColor; yourColor.Color.Tostring; 

然后删除返回的不需要的符号,大多数情况下,如果您的颜色未定义,它将返回一个ARGB值,在这种情况下,没有内置的名称,但它包含许多名称值。

此外,如果您需要hex代码,则ColorConverter是将hex转换为名称的一种好方法。

做了一个wpfstring颜色转换器,因为我需要一个:

  class StringColorConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string colorString = value.ToString(); //Color colorF = (Color)ColorConverter.ConvertFromString(color); //displays r,g ,b values Color colorF = ColorTranslator.FromHtml(colorString); return colorF.Name; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 

可用

  <TextBlock Width="40" Height="80" Background="DarkViolet" Text="{Binding Background, Converter={StaticResource StringColorConverter}, Mode=OneWay, RelativeSource={RelativeSource Self}}" Foreground="White" FontWeight="SemiBold"/>