Convert KML hex color to 32 bit ARGB

Viewed 3088

How to convert a hex color into 32 bit ARGB using C#. (without using the built-in color functions)

I tried this but it is not producing the correct color:

string colorcode = "#ff465a82";
int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);

Thanks Before Hand

Update #1:

Found this but does not work: (Also I am sure it can be done in one line of code)

string colorcode = "ff465a82";

string a = colorcode.Substring(0, 2);
string r = colorcode.Substring(2, 2);
string g = colorcode.Substring(4, 2);
string b = colorcode.Substring(6, 2);

// To integer
int iCol = (a << 24) | (r << 16) | (g << 8) | b;

Solution

Michael Liu, you got this one! Here is the final solution, notice google earth uses ABGR and the standard is ARGB!

// Note Google KML Colors are not in standard format of ARGB
// Google KML Colors are stored as ABGR
public int kmlToARGB(string kmlhexcolor)
{

    kmlhexcolor = kmlhexcolor.TrimStart('#');

    string A = kmlhexcolor.Substring(0, 2);
    string B = kmlhexcolor.Substring(2, 2);
    string G = kmlhexcolor.Substring(4, 2);
    string R = kmlhexcolor.Substring(6, 2);
    int decValue = int.Parse(A + R + G + B, NumberStyles.HexNumber);

    return decValue;

}
3 Answers
Related