public void Draw( ) { AHSL hsl; int width = (int)imgBorder.ActualWidth; int height = (int)imgBorder.ActualHeight; #if !SILVERLIGHT int[] pixels = new int[width * height]; #endif var bitmap = Compat.CreateBitmap(width, height); for (int x = 0; x < width; ++x) { for (int y = 0; y < height; ++y) { hsl = new AHSL(x / (width / 360.0), Saturation, 1.0 - (((double)y) / height), 1.0); #if !SILVERLIGHT pixels[y * width + x] = hsl.Double().ToARGB32(); #else bitmap.Pixels[y * width + x] = hsl.Double().ToARGB32(); #endif } } #if !SILVERLIGHT bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, Compat.GetBitmapStride(width), 0); #endif spectrum.Height = height; spectrum.Width = width; spectrum.Source = bitmap; }
private void SetPointEvents( ColorPinpoint e ) { var drag = false; PaletteColor selected = null; Point? prev = null; Point shift = new Point(); e.MouseLeftButtonDown += (s, ev) => { drag = true; prev = null; shift = ev.GetPosition(s as FrameworkElement); SelectColor(s as ColorPinpoint); selected = Palette.Colors[(int)(s as FrameworkElement).Tag]; e.CaptureMouse(); }; e.MouseLeftButtonUp += (s, ev) => { drag = false; e.ReleaseMouseCapture(); }; e.MouseMove += (s, ev) => { if (drag) { double width = spectrum.Width; double height = spectrum.Height; double diam = (s as ColorPinpoint).ActualWidth; Point p = ev.GetPosition(canvasSpectrum); p.X = p.X - shift.X + diam / 2; p.Y = p.Y - shift.Y + diam / 2; AHSL hsl = selected.DoubleColor.ToAHSL(); double x = Math.Min(Math.Max(p.X, 0), width); double y = Math.Min(Math.Max(p.Y, 0), height); hsl.Luminance = 1 - (y / height); hsl.HueDegree = 360 * (x / width); hsl.Saturation = Saturation; if (prev != null) { selected.DoubleColor = hsl.Double(); if (ColorsUpdated != null) { ColorsUpdated(this, EventArgs.Empty); } } prev = p; } }; }