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

前端 未结 5 719
你的背包
你的背包 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:58

    Well, Red/Green/Blue are fairly easy to identify by inspection; what range of values do you need to support?

    The problem is that unless you start with a named color, it is very hard to get back to one; IsNamedColor will return false even if you create an obvious color via FromArgb.

    If you only need the actual expected standard colors, you could enumerate the known colors via reflection?

            Color test = Color.FromArgb(255,0,0);
            Color known = (
                       from prop in typeof(Color)
                           .GetProperties(BindingFlags.Public | BindingFlags.Static)
                       where prop.PropertyType == typeof(Color)
                       let color = (Color)prop.GetValue(null, null)
                       where color.A == test.A && color.R == test.R
                         && color.G == test.G && color.B == test.B
                       select color)
                       .FirstOrDefault();
    
            Console.WriteLine(known.Name);
    

    You might also be able to use this approach as a source of known colors for a more sophisticated algorithm.

提交回复
热议问题