How to Convert ColorDialog color to KML color format
Asked Answered
R

2

6

I’m looking for a way to convert the color code returned by a ColorDialog Box in C# into the color format utilized by KML/KMZ file formats. Any info would be greatly appreciated!!

Richelieu answered 19/9, 2013 at 18:26 Comment(1)
upon research im finding that KML uses a color schema that is 8 digits long. the first 2 digits are the opacity and the last 6 are hex. The order of expression is aabbggrr, where aa=alpha (00 to ff); bb=blue (00 to ff); gg=green (00 to ff); rr=red (00 to ff).Richelieu
R
5

After hours of research I have answered my own question.

Kml uses an 8 digit HEX color format. The traditional Hex format for red looks like #FF0000. In Kml, red would look like this FF0000FF. The first 2 digits are for opacity(alpha). Color format is in AABBGGRR. I was looking for a way to set the color as well as the opacity and return it in a string to be placed in the attribute of a KML. Here is my solution.

string color
string polyColor;
int opacity;
decimal percentOpacity;
string opacityString;

//This allows the user to set the color with a colorDialog adding the chosen color to a string in HEX (without opacity (BBGGRR))
private void btnColor_Click(object sender, EventArgs e)
{
    if (colorDialog1.ShowDialog() == DialogResult.OK)
    {
        btnColor.BackColor = colorDialog1.Color;
        Color clr = colorDialog1.Color;
        color = String.Format("{0:X2}{1:X2}{2:X2}", clr.B, clr.G, clr.R);
    }
}

//This method takes the Opacity (0% - 100%) set by a textbox and gets the HEX value. Then adds Opacity to Color and adds it to a string.
private void PolyColor()
{
    percentOpacity = ((Convert.ToDecimal(txtOpacity.Text) / 100) * 255);
    percentOpacity = Math.Floor(percentOpacity);  //rounds down
    opacity = Convert.ToInt32(percentOpacity);
    opacityString = opacity.ToString("x");
    polyColor = opacityString + color;

}

Im open for more efficient ways to get the color value

Richelieu answered 20/9, 2013 at 12:45 Comment(0)
G
0

Here is an online color convertor. http://www.zonums.com/gmaps/kml_color/ The two first digits are the opacity FF -> 100% For the colors from HTML to KML RGB are inverted from the first to the last. enter image description here enter image description here

/// Convertion from HTML color to KML Color
/// </summary>
/// <param name="htmlColor"></param>
/// <returns></returns>
public string convertColors_HTML_KML(string htmlColor)
{
    List<string> result = new List<string>(Regex.Split(htmlColor, @"(?<=\G.{2})", RegexOptions.Singleline));
    return "FF" + result[2] + result[1] + result[0];
}
Giguere answered 9/1, 2020 at 9:41 Comment(1)
zonums.com/gmaps/kml_color/ doesn't work, who can say online alternative?Mei

© 2022 - 2024 — McMap. All rights reserved.