bug in Color32 Parse

May 12, 2012 at 10:26 AM

public static Color32 Parse(string value)       

{           

if (value == null)           

{                return new Color32(0);            }           

uint converted = 0;           

//int max = Math.Min(value.Length, 8);

// We consider only the first eight characters significant.
            byte a = 0;            byte r = 0;            byte g = 0;            byte b = 0;           

//Порядок выражений – aabbggrr           

byte.TryParse(value[0].ToString() + value[1].ToString(), NumberStyles.HexNumber,                          CultureInfo.InvariantCulture, out a);           

byte.TryParse(value[2].ToString() + value[3].ToString(), NumberStyles.HexNumber,                          CultureInfo.InvariantCulture, out b);           

byte.TryParse(value[4].ToString() + value[5].ToString(), NumberStyles.HexNumber,                          CultureInfo.InvariantCulture, out g);           

byte.TryParse(value[6].ToString() + value[7].ToString(), NumberStyles.HexNumber,                          CultureInfo.InvariantCulture, out r);           

//abgr           

converted = (uint)((a << 24) + (b << 16) + (g << 8) + (r));
            /*           

for (int i = 0; i < max; ++i)           

{                // Always increase the color, even if the char isn't a valid number                converted <<= 4; // Move along one hex - 2^4               

string letter = value[i].ToString();               

uint number;               

if (uint.TryParse(letter, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out number))     {                    converted += number;                }           

}           

*/

return new Color32((int)converted);       

}

Coordinator
May 12, 2012 at 10:49 AM

Thanks for posting that code, however, can you provide me an example of the input where the parsing fails?

The existing parsing is designed to match libkml, will allows formats that do not fall within the KML specification. This should be ok as it will also parse formats that are part of the specification.

If you would prefer only formats that meet the specification then I could look at passing an additional parameter to the function to indicate this?