trying to convert rgb from a .net Color to a string such as “red” or “blue”

前端 未结 5 734
你的背包
你的背包 2020-12-02 00:41

All of my methods are failing me in various ways. different lighting can mess it all up too.

has anyone every trying to return a name given a rgb value? \"red\" \"gr

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 00:45

    Here is a simple name scheme using two qualifiers and a color name:

    string ColorName(Color c)
    {
        List hues = new List()
        { 0, 15, 35, 44, 54, 63, 80, 160, 180, 200, 244, 280, 350, 360};
        List hueNames = new List()
            { "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;
    }
    

    You can easily adapt it if you want the blues to be more differentiated etc..

提交回复
热议问题