/// /// <summary> /// Draw color schema tool (Monochromatic, Analogous ...)</summary> /// public void DrawPointers(bool colorOnly = false) { double width = canvasSpectrum.ActualWidth; double height = canvasSpectrum.ActualHeight; var p = Gradient; double diam = 16; if (Gradient != null) { var stops = Gradient.InteriorStops.ToArray(); if (m_samplesb == null || m_samplesb.Length != stops.Length) { if (m_samplesb != null) { canvasSpectrum.Children.Clear(); CurrentStop = null; } m_samplesb = new GradientStopPinpoint[stops.Length]; for (int i = 0; i < m_samplesb.Length; ++i) { m_samplesb[i] = new GradientStopPinpoint() { Opacity = 0.8, IsHitTestVisible = true, Width = diam, Height = diam, CurrentColor = stops[i].Color, Tag = i, Cursor = Cursors.Hand, PaletteColor = stops[i] }; canvasSpectrum.Children.Add(m_samplesb[i]); SetPointEvents(m_samplesb[i]); m_samplesb[i].SetValue(Canvas.LeftProperty, width * stops[i].Offset); m_samplesb[i].SetValue(Canvas.TopProperty, height / 2 - radius / 2); } } for (int i = 0; i < m_samplesb.Length; ++i) { m_samplesb[i].SetValue(Canvas.LeftProperty, width * stops[i].Offset); m_samplesb[i].SetValue(Canvas.TopProperty, height / 2 - radius / 2); } } }
private void SetPointEvents( GradientStopPinpoint e ) { var drag = false; Point shift = new Point(); e.MouseRightButtonDown += (s, ev) => { var pinpoint = (GradientStopPinpoint)s; Gradient.Stops.Remove(pinpoint.PaletteColor); CurrentStop = null; }; e.MouseLeftButtonDown += (s, ev) => { drag = true; shift = ev.GetPosition(s as FrameworkElement); SelectColor(s as GradientStopPinpoint); //selected = Gradient.Colors[(int)(s as FrameworkElement).Tag]; e.CaptureMouse(); }; e.MouseLeftButtonUp += (s, ev) => { drag = false; e.ReleaseMouseCapture(); bool outOfOrder = false; for (int i = 0; i < m_samplesb.Length - 1; i++) { if (m_samplesb[i].PaletteColor.Offset > m_samplesb[i + 1].PaletteColor.Offset) { outOfOrder = true; break; } } if (outOfOrder) { var interiorColors = Gradient.InteriorStops.ToArray(); for (int i = 0; i < m_samplesb.Length; i++) { m_samplesb[i].PaletteColor = interiorColors[i]; m_samplesb[i].CurrentColor = interiorColors[i].Color; } } }; e.MouseMove += (s, ev) => { if (Gradient == null) { return; } if (drag) { double width = canvasSpectrum.ActualWidth; double height = canvasSpectrum.ActualHeight; var pinpoint = (GradientStopPinpoint)s; double diam = pinpoint.ActualWidth; if (width <= 0) { return; } Point p = ev.GetPosition(canvasSpectrum); p.X = p.X - shift.X + diam / 2; p.Y = p.Y - shift.Y + diam / 2; var newOffset = p.X / width; pinpoint.PaletteColor.Offset = NormalizeOffset(newOffset); } }; }
private void SelectColor(GradientStopPinpoint pp) { CurrentStop = pp.PaletteColor; }