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

前端 未结 5 725
你的背包
你的背包 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条回答
  •  萌比男神i
    2020-12-02 00:50

    You can try this code

    static char[] hexDigits = {
             '0', '1', '2', '3', '4', '5', '6', '7',
             '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    
    
    
    public static string ColorToHexString(Color color)
            {
                byte[] bytes = new byte[4];
                bytes[0] = color.A;
                bytes[1] = color.R;
                bytes[2] = color.G;
                bytes[3] = color.B;
                char[] chars = new char[bytes.Length * 2];
                for (int i = 0; i < bytes.Length; i++)
                {
                    int b = bytes[i];
                    chars[i * 2] = hexDigits[b >> 4];
                    chars[i * 2 + 1] = hexDigits[b & 0xF];
                }
                return new string(chars);
            }
    

提交回复
热议问题