十六进制转换code到颜色名称 [英] convert hex code to color name
问题描述
我如何转换这种六code =#2088C1
成颜色名称,如蓝色或红色
How can i convert this hexa code = #2088C1
into colour name Like Blue or Red
我的目标是我想要得到的颜色名称,如蓝色为给定的六code
My aim is i want to get the colour name like "blue" for the given hexa code
我曾尝试下面的code,但它没有给出任何颜色的名字。
I have tried the below code but it was not giving any colour name ..
System.Drawing.Color col = System.Drawing.ColorTranslator.FromHtml("#2088C1");
Color col = ColorConverter.ConvertFromString("#2088C1") as Color;
但它不会给颜色名称,比如这个aquablue
but it does not giving the colour name like this "aquablue"
我用用C#的WinForms应用
I am using winforms applications with c#
推荐答案
我偶然发现了一个的德语网站但这正是你想要什么:
I stumbled upon a german site that does exactly what you want:
/// <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]|[a-f]|[A-F]){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);
}
要得到你可以按如下方式获得<一个使用它的颜色名称href=\"http://msdn.microsoft.com/en-us/library/system.drawing.knowncolor%28v=VS.80%29.aspx\">KnownColor:
To get the color name you can use it as follows to get the KnownColor:
private KnownColor GetColor(string colorCode)
{
Color color = GetSystemDrawingColorFromHexString(colorCode);
return color.GetKnownColor();
}
这篇关于十六进制转换code到颜色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!