5
\$\begingroup\$

Kml uses an 8 digit HEX color format. The traditional Hex format for red looks like #FF0000. In Kml, red would look like this FFFF0000. 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 an attribute of a KML.

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); //Color returned in Hex format
    }
}

//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;
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ “The traditional Hex format for black looks like #FF0000.” That's not black, that's red. \$\endgroup\$ – svick Sep 23 '13 at 15:35
  • 1
    \$\begingroup\$ If the format is AABBGGRR, shoudn't red be FF0000FF instead of FFFF000000? \$\endgroup\$ – Pierre-Luc Pineault Sep 23 '13 at 21:18
  • \$\begingroup\$ @Pierre-Luc Pineeault, Thats blue...and there is only 4 zeros not 6. \$\endgroup\$ – Nate S. Sep 24 '13 at 11:25
7
\$\begingroup\$

In the System.Drawing namespace lives the ColorTranslator class which has an ToHtml() method which can be used here. A good way would be to create extension methods for this task. The first extension method will be named ToKMLColor and will expect a String parameter.

By using dec.TryParse() and Math.Min() we assure that the given parameter is a valid representation of a decimal/number with a maximum of 100. If the TryParse won't succeed, we assume that the opacity will be 0. After we have got the computed (/100*255) opacity value we pass it

public static string ToKMLColor(this Color color, String percentageOpacity)
{
    decimal opacity = 0;
    decimal.TryParse(percentageOpacity, out opacity);

    opacity = Math.Min(opacity, 100);
    opacity = Math.Floor(opacity / 100 * 255);

    return color.ToKMLColor(opacity);
}

to the overloaded ToKMLColor() method, which takes a decimal parameter. After converting the decimal to an int we pass it

public static string ToKMLColor(this Color color, decimal opacity)
{
    return color.ToKMLColor(Convert.ToInt32(opacity));
}  

to the overloaded ToKMLColor() method, which takes an integer parameter. Here we call the ToString() method with the X2 format to get the hex representation of the integer parameter and concat the return value of the ToHex() by using the Substring() method to remove the leading #.

public static string ToKMLColor(this Color color, int opacity)
{
    return opacity.ToString("X2") + color.ToHex().Substring(1);
}
public static String ToHex(this Color color)
{
    return System.Drawing.ColorTranslator.ToHtml(Color.FromArgb(0, color));
}  

Now we can call

polyColor = colorDialog1.Color.ToKMLColor(txtOpacity.Text);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.