/// <summary> /// Converts values to <see cref="SolidColorBrush"/>. /// </summary> /// <param name="value">Value to convert. It can be one of the following types: /// <see cref="SolidColorBrush"/>, <see cref="Color"/>, /// <see cref="string"/> defining system name or hexadecimal representation (#AARRGGBB) of color /// or any numeric. If the value is of numeric type then <see cref="Palette"/> is used for convertion.</param> /// <param name="targetType"></param> /// <param name="parameter">The instance of <see cref="DataSeries"/> representing colors.</param> /// <param name="culture"></param> /// <returns><see cref="SolidColorBrush"/> from specified value.</returns> public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { DataSeries data = parameter as DataSeries; if (value is double) { if (palette.IsNormalized) { palette = new Palette(false, new Range(data.MinValue, data.MaxValue), palette); } return(new SolidColorBrush(Palette.GetColor((double)value))); } else { SolidColorBrush solidColorBrush = value as SolidColorBrush; if (solidColorBrush != null) { return(solidColorBrush); } else if (value is Color) { return(new SolidColorBrush((Color)value)); } else { string str = value as string; if (str != null) { Color color = new Color(); if (Char.IsLetter(str[0])) { bool isNamed = false; Type colors = typeof(Colors); foreach (var property in colors.GetProperties()) { if (property.Name == str) { color = (Color)property.GetValue(null, null); isNamed = true; } } if (!isNamed) { throw new ArgumentException("Wrong name of color"); } } else if (str[0] == '#') { if (str.Length == 7) { color.A = 255; color.R = System.Convert.ToByte(str.Substring(1, 2), 16); color.G = System.Convert.ToByte(str.Substring(3, 2), 16); color.B = System.Convert.ToByte(str.Substring(5, 2), 16); } else if (str.Length == 9) { color.A = System.Convert.ToByte(str.Substring(1, 2), 16); color.R = System.Convert.ToByte(str.Substring(3, 2), 16); color.G = System.Convert.ToByte(str.Substring(5, 2), 16); color.B = System.Convert.ToByte(str.Substring(7, 2), 16); } else { throw new ArgumentException("Wrong name of color"); } } else { throw new ArgumentException("Wrong name of color"); } return(new SolidColorBrush(color)); } else { double d = System.Convert.ToDouble(value, CultureInfo.InvariantCulture); if (palette.IsNormalized) { if (data.MinValue == data.MaxValue) { return(new SolidColorBrush(new Palette(false, new Range(data.MinValue - 0.5, data.MaxValue + 0.5), palette).GetColor(d))); } else { return(new SolidColorBrush(new Palette(false, new Range(data.MinValue, data.MaxValue), palette).GetColor(d))); } } return(new SolidColorBrush(palette.GetColor(d))); } } } } catch (Exception exc) { Debug.WriteLine("Cannot convert value: " + exc.Message); return(new SolidColorBrush(Colors.Black)); } }