private static void ApplyColorMap(XNode xaml, ColorMap colorMap) { if (!(xaml.XPathEvaluate("//@*") is IEnumerable attributes)) { return; } IEnumerable <XAttribute> colorValues = attributes.OfType <XAttribute>().Where(a => a.Value.StartsWith("#")); foreach (XAttribute colorAttribute in colorValues) { if (!(colorConverter.ConvertFromInvariantString(colorAttribute.Value) is Color color)) { continue; } if (!(colorAttribute.Parent is XElement parent)) { continue; } double opacity = 1.0; if (parent.Attribute("Opacity")?.Value is string opacityString) { opacity = double.Parse(opacityString, CultureInfo.InvariantCulture); } ColorEx colorEx = new ColorEx(color, opacity); if (colorMap.TryGetValue(colorEx, out ColorMapEntry entry) && colorConverter.ConvertToInvariantString(entry.ModifiedColor.Color) is string newValue) { colorAttribute.Value = newValue; parent.SetAttributeValue("Opacity", entry.ModifiedColor.Opacity); } else { colorMap.Add(new ColorMapEntry(colorEx)); } } }
public ColorMapEntry(ColorEx originalColor) { OriginalColor = originalColor; ModifiedColor = originalColor; }
public bool TryGetValue(ColorEx colorEx, out ColorMapEntry colorMapEntry) { colorMapEntry = this.FirstOrDefault(e => e.OriginalColor == colorEx); return(colorMapEntry != null); }