/// <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)); } }
private void UpdateBitmap() { if (PaletteOrientation == Orientation.Horizontal && (Width == 0 || Double.IsNaN(Width))) { image.Source = null; return; } if (PaletteOrientation == Orientation.Vertical && (Height == 0 || Double.IsNaN(Height))) { image.Source = null; return; } if (Palette == null) { image.Source = null; return; } int width = 0; int height = 0; if (PaletteOrientation == Orientation.Horizontal) { width = (int)Width; height = (int)PaletteHeight; } else { width = (int)Height; height = (int)PaletteHeight; } WriteableBitmap bmp2 = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null); WriteableBitmap bmp = bmp2.Clone(); bmp.Lock(); unsafe { byte * pixels = (byte *)bmp.BackBuffer; int stride = bmp.BackBufferStride; int pixelWidth = bmp.PixelWidth; double min = Palette.Range.Min; double coeff = (Palette.Range.Max - min) / bmp.PixelWidth; for (int i = 0; i < pixelWidth; i++) { double ratio = i * coeff + min; Color color = Palette.GetColor(i * coeff + min); for (int j = 0; j < height; j++) { pixels[(i << 2) + 3 + j * stride] = color.A; pixels[(i << 2) + 2 + j * stride] = color.R; pixels[(i << 2) + 1 + j * stride] = color.G; pixels[(i << 2) + j * stride] = color.B; } } } bmp.Unlock(); if (PaletteOrientation == Orientation.Vertical) { //SaveBMP("paleteControl_0.bmp", bmp); image.Source = WriteableBitmapreomBitmap(rotateImage90(BitmapFromWriteableBitmap(bmp))); } else { image.Source = bmp; } image.Width = image.Source.Width; image.Height = image.Source.Height; //Thickness marginThickness = this.Margin; //Width = (axis.ActualWidth + image.Width + 10) - marginThickness.Left - marginThickness.Left; //Height = Math.Max(image.Height , axis.Height) - marginThickness.Top - marginThickness.Bottom; }