private static SolidColorBrush ConvertToColor(string text) { SolidColorBrush fallback = new SolidColorBrush(Colors.Black); if (string.IsNullOrEmpty(text)) return fallback; if (!text.StartsWith("#")) text = "#" + text; SolidColorBrush brush = null; var converter = new BrushConverter(); if (converter.CanConvertFrom(typeof (string))) { try { brush = (SolidColorBrush)converter.ConvertFrom(text); } catch (Exception e) { ILogger Logger = ParagonLogManager.GetLogger(); Logger.Error("error ConvertFrom when converting color:" + text + ", exception: " + e.ToString()); // fallback in case of exception brush = fallback; } } if (brush == null) brush = fallback; return brush; }
public Brush DrawingColorToBrush(Color color) { Brush ret = null; var m = new BrushConverter(); var s = "#" + color.ToArgb().ToString("X8"); if (m.CanConvertFrom(typeof(string))) { ret = (Brush)m.ConvertFromString(s); } return(ret); }