How to get color from a Ploygon's Style

Mar 16, 2016 at 11:31 AM
Once I got the Kml file loaded how do I get the Style that is connected to a Polygon and how do I retrieve the color (area color)?
Mar 21, 2016 at 3:29 PM
Edited Mar 22, 2016 at 9:42 AM
I found a way. This snippet does what I need:
Assuming polygon points to a Polygon node in the KML DOM.
StyleMapCollection pStyle = kmlFile.FindStyle(polygon.GetParent<Placemark>().StyleUrl.ToString().Replace("#", string.Empty)) as StyleMapCollection;
foreach (Pair p in pStyle)
    if(p.State.ToString().ToLower() == "normal")
    Style styleFinally = kmlFile.FindStyle(p.StyleUrl.ToString().Replace("#", string.Empty)) as Style;
    SharpKml.Base.Color32 __col = (styleFinally.Polygon.Color != null) ? styleFinally.Polygon.Color.Value : new SharpKml.Base.Color32(255, 255, 255, 255);
I'm sure this is not the best practice as KML files wrap styles in all possible DOM structures; for instance, this snippet works if styles are given in a StyleMap. If they are given as lonely nodes, this won't work.
So if someone has a better, more general way, please share.