我正在寻找一种方法,可以将ColorDialog Box以C#返回的颜色代码转换为KML / KMZ文件格式使用的颜色格式。任何信息将不胜感激!
经过数小时的研究,我已经回答了我自己的问题。
Kml使用8位数的十六进制颜色格式。红色的传统十六进制格式看起来像#FF0000。在Kml中,红色看起来像是FFFF0000。前两位数字代表opacity(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;
}
为获得颜色值的更有效方法而开放
这里是在线颜色转换器。http://www.zonums.com/gmaps/kml_color/前两位数字是不透明度FF-> 100%对于从HTML到KML的颜色,RGB从第一个倒到最后一个。
/// 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];
}