将十六进制代码转换为颜色名称 [英] convert hex code to color name

查看:44
本文介绍了将十六进制代码转换为颜色名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can i convert this hexa code = #2088C1 into colour name Like Blue or Red

我的目标是我想为给定的六进制代码获得像蓝色"这样的颜色名称

My aim is i want to get the colour name like "blue" for the given hexa 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}"))
        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);
}

要获取颜色名称,您可以按如下方式使用它来获取 已知颜色:

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();
}

然而,System.Color.GetKnownColor 似乎在较新版本的 .NET 中被删除

However, System.Color.GetKnownColor seems to be removed in newer versions of .NET

这篇关于将十六进制代码转换为颜色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆