Exemplo 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;
                }
            };
        }
        protected DoubleColor GetColorFromPoint(
            AHSB orgColor,
            Point pprev,
            Point pnew
            )
        {
            double      y2   = -pnew.Y + Radius;
            double      x2   = pnew.X - Radius;
            double      coff = Math.Min(1.0, Math.Max(0.0, Math.Sqrt(y2 * y2 + x2 * x2) / ValueRadius));
            DoubleColor val;
            AHSL        hsl;

            switch (PaintMethod)
            {
            case WheelPaintMethod.Brightness:
                val = orgColor.Alter(null, 1.0, coff).Double();
                break;

            case WheelPaintMethod.Luminance:
                coff = 1 - coff;
                goto case WheelPaintMethod.InverseLuminance;

            case WheelPaintMethod.InverseLuminance:
                hsl           = orgColor.Double().ToAHSL();
                hsl.Luminance = coff;
                val           = hsl.Double();
                break;

            case WheelPaintMethod.Saturation:
                if (orgColor.Brightness == 0)
                {
                    // orgColor.Brightness = 1 / 1000.0;
                }
                val = orgColor.Alter(null, coff).Double();
                break;

            default:
                throw new NotImplementedException();
            }

            return(val);
        }
Exemplo n.º 3
0
        public virtual Result Warp(WarpRequest request)
        {
            if (request == default)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var avg = 0.0f;
            var min = float.MaxValue;
            var max = float.MinValue;

            var index = 0;

            var result = new ARGB[request.Width * request.Height];
            var noise  = new float[request.Width * request.Height * 3];

            for (var y = -request.Height / 2; y < request.Height / 2; y++)
            {
                for (var x = -request.Width / 2; x < request.Width / 2; x++)
                {
                    var xyz = request.Is3D
                                ? request.Warp.Get(new XYZ(x, y, request.Z))
                                : new XYZ(request.Warp.Get(new XY(x, y)));

                    xyz.X -= x;
                    xyz.Y -= y;
                    xyz.Z -= request.Z;

                    avg += xyz.X + xyz.Y;
                    max  = Math.Max(max, Math.Max(xyz.X, xyz.Y));
                    min  = Math.Min(min, Math.Min(xyz.X, xyz.Y));

                    noise[index++] = xyz.X;
                    noise[index++] = xyz.Y;

                    if (request.Is3D)
                    {
                        avg           += xyz.Z;
                        max            = Math.Max(max, xyz.Z);
                        min            = Math.Min(min, xyz.Z);
                        noise[index++] = xyz.Z;
                    }
                }
            }

            avg = request.Is3D ? (avg / ((index - 1) * 3)) : (avg / ((index - 1) * 2));

            index = 0;
            var scale = 1.0f / (max - min);

            for (var i = 0; i < result.Length; i++)
            {
                if (request.Is3D)
                {
                    result[i] = new ARGB(255, (int)(255 * (noise[index++] - min) * scale), (int)(255 * (noise[index++] - min) * scale), (int)(255 * (noise[index++] - min) * scale));
                }
                else
                {
                    var vx = (noise[index++] - min) / (max - min) - 0.5f;
                    var vy = (noise[index++] - min) / (max - min) - 0.5f;

                    result[i] = new AHSB(Math.Atan2(vy, vx) * (180 / Math.PI) + 180, 0.9f, Math.Min(1.0f, Math.Sqrt(vx * vx + vy * vy) * 2));
                }

                if (request.IsInverted)
                {
                    var color = result[i];
                    result[i] = new ARGB(color.A, 255 - color.R, 255 - color.G, 255 - color.B);
                }
            }

            return(new Result()
            {
                Image = result,
                Statistics = new Statistics()
                {
                    Average = avg,
                    Minimum = min,
                    Maximum = max
                }
            });
        }
        ///
        /// <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;
                }
            }
        }