patterncsharpMinor
Getting the KML color code from colordialog box more efficiently
Viewed 0 times
efficientlythekmlcolormoregettingcodefromcolordialogbox
Problem
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;
}Solution
In the
By using
After we have got the computed (
to the overloaded
to the overloaded
Now we can call
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 itpublic 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);Code Snippets
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);
}public static string ToKMLColor(this Color color, decimal opacity)
{
return color.ToKMLColor(Convert.ToInt32(opacity));
}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));
}polyColor = colorDialog1.Color.ToKMLColor(txtOpacity.Text);Context
StackExchange Code Review Q#31689, answer score: 7
Revisions (0)
No revisions yet.