我正在寻找一种方法,将C#中ColorDialog Box返回的颜色代码转换为KML / KMZ文件格式使用的颜色格式。任何信息都将不胜感激!
经过数小时的研究,我已回答了自己的问题。
Kml使用8个十六进制(HEX)颜色格式。传统的红色Hex格式看起来像#FF0000。在Kml中,红色看起来像这样FF0000FF。前2位数字是透明度(alpha)。颜色格式为AABBGGRR。我正在寻找一种设置颜色以及透明度并将其作为字符串返回到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);
}
}
//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;
}
我希望能找到更加高效的方法来获取颜色值。
/// 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];
}