如何将ColorDialog颜色转换为KML颜色格式

5
我正在寻找一种方法,将C#中ColorDialog Box返回的颜色代码转换为KML / KMZ文件格式使用的颜色格式。任何信息都将不胜感激!

经过研究,我发现KML使用的颜色方案是8位数字。前两位是不透明度,后六位是十六进制。表达顺序为aabbggrr,其中aa=alpha(00到ff);bb=blue(00到ff);gg=green(00到ff);rr=red(00到ff)。 - Nate S.
2个回答

4

经过数小时的研究,我已回答了自己的问题。

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;

}

我希望能找到更加高效的方法来获取颜色值。


0
这是一个在线颜色转换器。 http://www.zonums.com/gmaps/kml_color/ 前两个数字是不透明度FF -> 100% 对于从HTML到KML的颜色,RGB从第一个到最后一个是颠倒的。 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];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接