Esempio n. 1
0
        private void SetPointEvents(
            ColorPinpoint e
            )
        {
            var          drag     = false;
            PaletteColor selected = null;
            Point?       prev     = null;

            e.MouseLeftButtonDown += (s, ev) =>
            {
                drag = true;
                prev = null;

                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  = brisat.Width;
                    double height = brisat.Height;
                    double diam   = (s as ColorPinpoint).ActualWidth;

                    Point p   = ev.GetPosition(canvasBriSat);
                    AHSB  hsb = selected.Color;

                    hsb.Saturation = p.X / width;
                    hsb.Brightness = 1 - p.Y / height;

                    if (prev != null)
                    {
                        selected.Color = hsb;

                        double x = (hsb.Saturation * width) - diam / 2;
                        double y = (height - (hsb.Brightness * height)) - diam / 2;

                        (s as FrameworkElement).SetValue(Canvas.LeftProperty, x);
                        (s as FrameworkElement).SetValue(Canvas.TopProperty, y);

                        DrawPointers();

                        if (ColorsUpdated != null)
                        {
                            ColorsUpdated(this, EventArgs.Empty);
                        }
                    }
                    prev = p;
                }
            };
        }
        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;
                }
            };
        }
        private void SelectColor(
            ColorPinpoint pp
            )
        {
            for (int i = 0; i < Palette.Colors.Count; ++i)
            {
                Palette.Colors[i].IsSelected = (pp == m_samplesb[i]);
            }

            if (SelectColored != null)
            {
                SelectColored(this, new EventArg <int>()
                {
                    Value = (int)pp.Tag
                });
            }
        }
        private void SelectColor(
            ColorPinpoint pp
            )
        {
            for (int i = 0; i < Palette.Colors.Count; ++i)
            {
                Palette.Colors[i].IsSelected = (pp == m_schemaElements[i]);
            }

            if (ColorSelected != null)
            {
                ColorSelected(this, new EventArg <int>()
                {
                    Value = (int)((pp).Tag)
                });
            }
        }
        ///
        /// <summary>
        /// Draw color schema tool (Monochromatic, Analogous ...)</summary>
        ///
        public void DrawPointers(
            bool colorOnly = false
            )
        {
            double  width  = imgBorder.ActualWidth;
            double  height = imgBorder.ActualHeight;
            Palette p      = Palette;
            double  diam   = 16;
            AHSL    hsl;

            if (Palette != null)
            {
                if (m_samplesb == null || m_samplesb.Length != p.Colors.Count)
                {
                    if (m_samplesb != null)
                    {
                        canvasSpectrum.Children.Clear();
                    }

                    m_samplesb = new ColorPinpoint[p.Colors.Count];

                    for (int i = 0; i < m_samplesb.Length; ++i)
                    {
                        m_samplesb[i] = new ColorPinpoint()
                        {
                            Opacity          = 0.8,
                            IsHitTestVisible = true,
                            Width            = diam,
                            Height           = diam,
                            CurrentColor     = p.Colors[i].DoubleColor.ToColor(),
                            Tag          = i,
                            Cursor       = Cursors.Hand,
                            PaletteColor = Palette.Colors[i]
                        };

                        m_samplesb[i].SetValue(ToolTipService.ToolTipProperty, p.Colors[i].Name);
                        canvasSpectrum.Children.Add(m_samplesb[i]);
                        SetPointEvents(m_samplesb[i]);
                    }

                    Debug.WriteLine("ColorPaletteControl.InvalidateColors - recreate all color pointers");
                }

                for (int i = 0; i < m_samplesb.Length; ++i)
                {
                    hsl = p.Colors[i].DoubleColor.ToAHSL();

                    double x;
                    double y = height * (1 - hsl.Luminance);

                    if (hsl.Luminance == 0 || hsl.Luminance == 1)
                    {
                        x = (double)m_samplesb[i].GetValue(Canvas.LeftProperty);
                    }
                    else
                    {
                        x = (width / 360.0) * hsl.HueDegree;
                    }

                    m_samplesb[i].CurrentColor = p.Colors[i].DoubleColor.ToColor();

                    x -= diam / 2;
                    y -= diam / 2;

                    if (!colorOnly)
                    {
                        m_samplesb[i].SetValue(Canvas.LeftProperty, x);
                        m_samplesb[i].SetValue(Canvas.TopProperty, y);
                    }
                }
            }
            Debug.WriteLine("ColorPaletteControl.InvalidateColors");
        }
Esempio n. 6
0
        ///
        /// <summary>
        /// Draw color schema tool (Monochromatic, Analogous ...)</summary>
        ///
        public void DrawPointers(
            bool colorOnly = false
            )
        {
            double  width  = imgBorder.ActualWidth;
            double  height = imgBorder.ActualHeight;
            Palette p      = Palette;
            double  diam   = 16;
            AHSB    hsb;

            if (Palette != null)
            {
                if (m_samplesb == null || m_samplesb.Length != p.Colors.Count)
                {
                    if (m_samplesb != null)
                    {
                        canvasBriSat.Children.Clear();
                    }

                    m_samplesb = new ColorPinpoint[p.Colors.Count];

                    for (int i = 0; i < m_samplesb.Length; ++i)
                    {
                        m_samplesb[i] = new ColorPinpoint()
                        {
                            Opacity          = 0.8,
                            IsHitTestVisible = true,
                            Width            = diam,
                            Height           = diam,
                            CurrentColor     = p.Colors[i].DoubleColor.ToColor(),
                            Tag          = i,
                            Cursor       = Cursors.Hand,
                            PaletteColor = Palette.Colors[i],
                            IsMain       = i == 0
                        };

                        m_samplesb[i].SetValue(ToolTipService.ToolTipProperty, p.Colors[i].Name);
                        canvasBriSat.Children.Add(m_samplesb[i]);
                        SetPointEvents(m_samplesb[i]);
                    }

                    Debug.WriteLine("ColorPaletteControl.InvalidateColors - recreate all color pointers");
                }

                for (int i = 0; i < m_samplesb.Length; ++i)
                {
                    hsb = p.Colors[i].Color;

                    double x = (hsb.Saturation * width) - diam / 2;
                    double y = (height - (hsb.Brightness * height)) - diam / 2;

                    m_samplesb[i].CurrentColor = p.Colors[i].DoubleColor.ToColor();

                    if (!colorOnly)
                    {
                        m_samplesb[i].SetValue(Canvas.LeftProperty, x);
                        m_samplesb[i].SetValue(Canvas.TopProperty, y);
                    }
                }
            }
            Debug.WriteLine("ColorPaletteControl.InvalidateColors");
        }
        ///
        /// <summary>
        /// Main color pointer event handle initialize</summary>
        ///
        private void SetMainPointEvents(
            ColorPinpoint e
            )
        {
            var   drag = false;
            Point?prev = null;

            e.MouseEnter += (s, ev) =>
            {
                ShowLines(true);

                (s as UIElement).SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER_SELECTED);
                m_main.SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER_SELECTED + 1);
            };

            e.MouseLeave += (s, ev) =>
            {
                if (!drag)
                {
                    ShowLines(false);

                    (s as UIElement).SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER);
                    m_main.SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER);
                }
            };

            e.MouseLeftButtonDown += (s, ev) =>
            {
                drag = true;
                e.CaptureMouse();

                e.Cursor = Cursors.Hand;
                ShowLines(true);
                SelectColor(m_main);
            };

            e.MouseLeftButtonUp += (s, ev) =>
            {
                drag = false;
                prev = null;

                e.ReleaseMouseCapture();
                e.Cursor = Cursors.Arrow;
                ShowLines(false);

                (s as UIElement).SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER);
                m_main.SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER);
            };

            e.MouseMove += (s, ev) =>
            {
                if (drag)
                {
                    Point p = ev.GetPosition(wheel);
                    if (prev != null)
                    {
                        ChangePrimaryColorForPointer(prev.Value, p);
                    }
                    prev = p;
                }
            };
        }
        ///
        /// <summary>
        /// Main color pointer event handle initializer</summary>
        ///
        private void SetSecondaryPointEvents(
            ColorPinpoint e
            )
        {
            var   drag = false;
            Point?prev = null;

            e.MouseEnter += (s, ev) =>
            {
                ShowLines(true);
                (s as UIElement).SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER_SELECTED);
            };

            e.MouseLeave += (s, ev) =>
            {
                if (!drag)
                {
                    ShowLines(false);
                    (s as UIElement).SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER);
                }
            };

            e.MouseLeftButtonDown += (s, ev) =>
            {
                if (ev.ClickCount == 2)
                {
                    int id = (int)(s as FrameworkElement).Tag;

                    if (Palette.MaxVectorIndex > 0)
                    {
                        Palette.Colors[id].VectorIndex++;
                        Palette.Colors[id].VectorIndex %= Palette.MaxVectorIndex;
                        Palette.Refresh();
                    }
                }
                else
                {
                    drag = true;
                    prev = null;

                    e.CaptureMouse();
                    e.Cursor = Cursors.Hand;

                    ShowLines(true);
                    SelectColor(s as ColorPinpoint);
                }
            };

            e.MouseLeftButtonUp += (s, ev) =>
            {
                drag = false;
                prev = null;

                e.ReleaseMouseCapture();
                ShowLines(false);

                (s as UIElement).SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER);
            };

            e.MouseMove += (s, ev) =>
            {
                double diam = (s as ColorPinpoint).ActualWidth;
                if (drag)
                {
                    Point p = ev.GetPosition(wheel);
                    if (prev != null)
                    {
                        ChangeSecondaryColorsForPointer((int)((s as FrameworkElement).Tag), prev.Value, p);
                    }
                    prev = p;
                }
            };
        }
        ///
        /// <summary>
        /// Draw all color pointers for palette</summary>
        ///
        protected void DrawPointers(
            )
        {
            int    radius    = (int)Radius;
            double ewidth    = COLOR_POINTER_DIAMETER;
            double pinradius = ewidth / 2;

            if (Palette != null)
            {
                double   gradRadius = ValueRadius;
                double[] angles     = new double[Palette.Colors.Count];

                double[]      radCoff     = new double[Palette.Colors.Count];
                DoubleColor[] wheelColors = new DoubleColor[Palette.Colors.Count];

                for (int i = 0; i < angles.Length; ++i)
                {
                    PaletteColor pc   = Palette.Colors[i];
                    double       coff = 1;

                    switch (PaintMethod)
                    {
                    case WheelPaintMethod.Brightness:
                        coff = pc.DoubleColor.ToAHSB().Brightness;
                        var c = pc.DoubleColor.ToAHSB();
                        c.Saturation   = 1.0;
                        wheelColors[i] = c.Double();
                        break;

                    case WheelPaintMethod.InverseLuminance:

                        coff = pc.DoubleColor.ToAHSL().Luminance;
                        var c1 = pc.DoubleColor.ToAHSL();
                        c1.Saturation  = 1.0;
                        wheelColors[i] = c1.Double();
                        break;

                    case WheelPaintMethod.Saturation:
                        coff = pc.DoubleColor.Saturation;
                        var c2 = pc.DoubleColor.ToAHSB();
                        c2.Brightness  = 1.0;
                        wheelColors[i] = c2.Double();
                        break;

                    case WheelPaintMethod.Luminance:
                        coff = 1.0 - pc.DoubleColor.ToAHSL().Luminance;
                        var c3 = pc.DoubleColor.ToAHSL();
                        c3.Saturation  = 1.0;
                        wheelColors[i] = c3.Double();
                        break;

                    default:
                        throw new NotImplementedException();
                    }

                    angles[i]  = Wheel.GetAngle(pc.DoubleColor);
                    radCoff[i] = coff;
                }

                if (m_schemaElements == null || m_schemaElements.Length != angles.Length)
                {
                    if (m_schemaElements != null)
                    {
                        foreach (Line l in m_lines)
                        {
                            canvas.Children.Remove(l);
                        }

                        foreach (ColorPinpoint e in m_schemaElements)
                        {
                            canvas.Children.Remove(e);
                        }
                    }

                    m_schemaElements = new ColorPinpoint[angles.Length];
                    m_lines          = new Line[angles.Length];

                    for (int i = 0; i < m_schemaElements.Length; ++i)
                    {
                        m_schemaElements[i] = new ColorPinpoint()
                        {
                            Opacity          = 0.9,
                            IsHitTestVisible = true,
                            // Width            = ewidth,
                            Height       = ewidth,
                            CurrentColor = Colors.Black,
                            Tag          = i,
                            PaletteColor = Palette.Colors[i]
                        };

                        m_schemaElements[i].SetValue(Canvas.ZIndexProperty, COLOR_POINTER_ZORDER);
                        m_schemaElements[i].SetValue(ToolTipService.ToolTipProperty, Palette.Colors[i].Name);

                        canvas.Children.Add(m_schemaElements[i]);

                        m_lines[i] = new Line()
                        {
                            Opacity         = 0.0,
                            StrokeThickness = 2,
                            Stroke          = Colors.Black.ToBrush()
                        };

                        m_lines[i].SetValue(Canvas.ZIndexProperty, COLOR_LINE_ZORDER);
                        canvas.Children.Add(m_lines[i]);

                        if (i == 0)
                        {
                            m_main        = m_schemaElements[i];
                            m_main.IsMain = true;
                            SetMainPointEvents(m_main);
                        }
                        else
                        {
                            SetSecondaryPointEvents(m_schemaElements[i]);
                        }
                    }
                }

                for (int i = 0; i < m_schemaElements.Length; ++i)
                {
                    double angle = Wheel.FixAngle(angles[i] - m_angleShift);
                    double rad   = MathEx.ToRadians(angle);
                    double coff  = radCoff[i];

                    double x = Math.Cos(rad) * (gradRadius - pinradius) * coff + radius;
                    double y = -Math.Sin(rad) * (gradRadius - pinradius) * coff + radius;

                    AHSB hsb = Palette.Colors[i].Color;

                    x -= pinradius;
                    y -= pinradius;

                    m_schemaElements[i].SetValue(Canvas.LeftProperty, x);
                    m_schemaElements[i].SetValue(Canvas.TopProperty, y);
                    m_schemaElements[i].CurrentColor = hsb.Double().ToColor();

                    m_lines[i].X1 = radius;
                    m_lines[i].Y1 = radius;

                    m_lines[i].X2 = x + pinradius;
                    m_lines[i].Y2 = y + pinradius;
                }
            }
        }