获取最近颜色,然后将其转换为颜色特定名称

时间:2016-12-11 08:15:16

标签: c#

我正在使用此功能,我在互联网上找到但我想添加更多颜色,如:粉色,米色,白色,黑色等。我如何根据我的需要调整此代码?

public static string ColorName(Color c)
        {
            List<float> hues = new List<float>()
            { 0, 15, 35, 44, 54, 63, 80, 160, 180, 200, 244, 280, 350, 360};
            List<string> hueNames = new List<string>()
            { "red", "orange-red", "orange", "yellow-orange", "yellow",
            "yellow-green",   "green"  , "blue-green" , "cyan", "blue",
            "violet", "purple", "red" };

            float h = c.GetHue();
            float s = c.GetSaturation();
            float b = (c.R * 0.299f + c.G * 0.587f + c.B * 0.114f) / 256f;

            string name = s < 0.35f ? "pale " : s > 0.8f ? "vivid " : "";
            name += b < 0.35f ? "dark " : b > 0.8f ? "light " : "";
            for (int i = 0; i < hues.Count - 1; i++)
                if (h >= hues[i] && h <= hues[i + 1])
                {
                    name += hueNames[i];
                    break;
                }
            return name;
        }

1 个答案:

答案 0 :(得分:0)

这可能适用于hsb确定浅/暗苍白/鲜艳,但不太适合粉红/黄/蓝/橙色。此处将颜色空间设置为xy图。要查找最接近的颜色,请查看CIELab颜色空间。它允许欧几里德距离计算,可以识别像黄色这样的东西比橙色色调的类似饱和度值更接近米色。