This project has moved. For the latest updates, please go here.

Supporting colors with a leading "#"

Apr 13, 2016 at 7:00 PM
I know this is not in the kml spec, but I need to parse in a file that has colors in this format.
Would it be possible to modify the Color32 Parse method to trim off the leading "#" if it exists?
Something like the following solves the issue I'm having.
public static Color32 Parse(string value)
    if (value == null)
        return new Color32(0);
    value = value.TrimStart(new char[] { '#' }); // Trim leading # characters, if any
Thank you.
Apr 24, 2016 at 9:38 PM
I've updated the library to support that, so if you get the latest NuGet package/zip from the downloads then it should handle that. Note that it won't output the colour with a hash sign, so hopefully what ever is generated the file your parsing will be able to cope with that.

Thanks for the suggestion!
Apr 25, 2016 at 3:20 PM
Thanks for the response Sam! There's a small issue with the implementation though.
The updated code for reference:
if ((value.Length > 0) && (value[0] == '#'))
    start = 1;

int max = Math.Min(value.Length - start, start + 8); // We consider only the first eight characters significant.
for (int i = start; i < max; ++i)
    // Always increase the color, even if the char isn't a valid number
    converted <<= 4; // Move along one hex - 2^4
    converted += ParseHexChar(value, i);
When you take the min value of length-start and start + 8, length (9) minus start (1) is 8, and the for loop index stops after 7 characters (1 through 8). The "max" should always be start + 8, or at most "length". I would suggest:
int max = Math.Min(value.Length, start+8);
Thank you for your help!
Apr 25, 2016 at 8:05 PM
Well that's just embarrassing... the unit test had a copy+paste error or it would have picked that up.

All fixed now, so sorry about that,
Apr 25, 2016 at 8:23 PM
No worries, it happens. Thanks for the quick response.