Esempio n. 1
0
        public BCanvas(byte R, byte G, byte B, Bitmap colourBitmap) : base()
        {
            (double _, double S, double V) = HSB.RGBToHSB(R / 255.0, G / 255.0, B / 255.0);

            this.Width  = 128;
            this.Height = 96;

            Point v1 = new Point(51.673, 0);
            Point v2 = new Point(104, 15.955);
            Point v3 = new Point(76.327, 46.125);
            Point v4 = new Point(24, 30.170);

            Point v5 = new Point(51.673, 49.875);
            Point v6 = new Point(104, 65.830);
            Point v7 = new Point(76.327, 96);
            Point v8 = new Point(24, 80.045);

            Point v15 = v1 * (B / 255.0) + v5 * (1 - B / 255.0);
            Point v26 = v2 * (B / 255.0) + v6 * (1 - B / 255.0);
            Point v37 = v3 * (B / 255.0) + v7 * (1 - B / 255.0);
            Point v48 = v4 * (B / 255.0) + v8 * (1 - B / 255.0);

            Fill1 = new AnimatablePath4Points(v48, v8, v7, v37)
            {
                Fill = RGB.MiddleCubeBrush
            };
            this.Children.Add(Fill1.Path);

            Fill2 = new AnimatablePath4Points(v37, v7, v6, v26)
            {
                Fill = RGB.DarkCubeBrush
            };
            this.Children.Add(Fill2.Path);

            ColourCanvas = new Canvas()
            {
                Width = 256, Height = 256, ClipToBounds = false
            };

            ColourImage = new Image()
            {
                Source = colourBitmap
            };
            ColourCanvas.Children.Add(ColourImage);

            PathGeometry border2Geometry = new PathGeometry();
            PathFigure   border2Figure   = new PathFigure()
            {
                StartPoint = new Point(0, 256), IsClosed = false
            };

            border2Figure.Segments.Add(new LineSegment()
            {
                Point = new Point(256, 256)
            });
            border2Geometry.Figures.Add(border2Figure);
            ColourCanvas.Children.Add(new Path()
            {
                Data = border2Geometry, StrokeThickness = 4, Stroke = RGB.MiddleCubeBrush
            });

            PathGeometry border3Geometry = new PathGeometry();
            PathFigure   border3Figure   = new PathFigure()
            {
                StartPoint = new Point(256, 256), IsClosed = false
            };

            border3Figure.Segments.Add(new LineSegment()
            {
                Point = new Point(256, 0)
            });
            border3Geometry.Figures.Add(border3Figure);
            ColourCanvas.Children.Add(new Path()
            {
                Data = border3Geometry, StrokeThickness = 4, Stroke = RGB.DarkCubeBrush
            });

            PositionEllipse = new Ellipse()
            {
                Width = 48, Height = 48, Fill = (S <0.5 && V> 0.5 ? Brushes.Black : Brushes.White)
            };
            PositionEllipse.RenderTransform = ColorPicker.GetTranslateTransform(G - 24, R - 24);
            ColourCanvas.Children.Add(PositionEllipse);

            double m11 = (v26.X - v15.X) / 256;
            double m12 = (v37.X - v26.X) / 256;
            double m21 = (v26.Y - v15.Y) / 256;
            double m22 = (v37.Y - v26.Y) / 256;

            ColourCanvasTransform = new AnimatableTransform(new Matrix(m11, m21, m12, m22, v15.X, v15.Y));

            ColourCanvas.RenderTransform       = ColourCanvasTransform.MatrixTransform;
            ColourCanvas.RenderTransformOrigin = new RelativePoint(0, 0, RelativeUnit.Absolute);
            this.Children.Add(ColourCanvas);
        }
        private void Update(ColorPicker.ColorSpaces colorSpace)
        {
            WriteableBitmap canvas2Dimage = null;
            WriteableBitmap canvas1Dimage = null;

            (byte R, byte G, byte B) = (RGB.R, RGB.G, RGB.B);

            (double H, double S, double V) = (HSV.H, HSV.S, HSV.V);
            (double L, double a, double b) = (LAB.L, LAB.a, LAB.b);

            switch (colorSpace)
            {
            case ColorPicker.ColorSpaces.RGB:
                switch (ColorComponent)
                {
                case ColorPicker.ColorComponents.R:
                    canvas1Dimage = AvaloniaColorPicker.RGB.GetR(G, B);
                    canvas2Dimage = AvaloniaColorPicker.RGB.GetGB(R);
                    break;

                case ColorPicker.ColorComponents.G:
                    canvas1Dimage = AvaloniaColorPicker.RGB.GetG(R, B);
                    canvas2Dimage = AvaloniaColorPicker.RGB.GetRB(G);
                    break;

                case ColorPicker.ColorComponents.B:
                    canvas1Dimage = AvaloniaColorPicker.RGB.GetB(R, G);
                    canvas2Dimage = AvaloniaColorPicker.RGB.GetRG(B);
                    break;
                }
                break;

            case ColorPicker.ColorSpaces.HSB:
                HSB.HSVToRGB(H, S, V, out R, out G, out B);
                switch (ColorComponent)
                {
                case ColorPicker.ColorComponents.H:
                    canvas1Dimage = HSB.GetH();
                    canvas2Dimage = HSB.GetSB(H);
                    break;

                case ColorPicker.ColorComponents.S:
                    canvas1Dimage = HSB.GetS(H, V);
                    canvas2Dimage = HSB.GetHB(S);
                    break;

                case ColorPicker.ColorComponents.B:
                    canvas1Dimage = HSB.GetB(H, S);
                    canvas2Dimage = HSB.GetHS(V);
                    break;
                }
                break;

            case ColorPicker.ColorSpaces.LAB:
                Lab.FromLab(L, a, b, out R, out G, out B);
                switch (ColorComponent)
                {
                case ColorPicker.ColorComponents.L:
                    canvas1Dimage = Lab.GetL(a, b);
                    canvas2Dimage = Lab.GetAB(L);
                    break;

                case ColorPicker.ColorComponents.A:
                    canvas1Dimage = Lab.GetA(L, b);
                    canvas2Dimage = Lab.GetLB(a);
                    break;

                case ColorPicker.ColorComponents.B:
                    canvas1Dimage = Lab.GetB(L, a);
                    canvas2Dimage = Lab.GetLA(b);
                    break;
                }
                break;
            }

            Canvas2D.Source    = canvas2Dimage;
            Canvas1D.Source    = canvas1Dimage;
            AlphaCanvas.Source = AvaloniaColorPicker.RGB.GetA(R, G, B);
        }
Esempio n. 3
0
        protected override void OnPropertyChanged <T>(AvaloniaPropertyChangedEventArgs <T> change)
        {
            base.OnPropertyChanged(change);

            if (change.Property == HSVProperty)
            {
                HSV    hsv = (change.NewValue.Value as HSV?).Value;
                double H   = hsv.H;
                double S   = hsv.S;
                double V   = hsv.V;

                ColourImage.Source = HSB.GetShiftedS(this.ColourBitmap, 20 * S, H < 0.5);

                Point v23 = new Point(128 - 64 * (1 - S), 20);
                Point v14 = new Point(64 * (1 - S), 20);
                Point v67 = new Point(128 - 64 * (1 - S), 76);

                double m11 = (v23.X - v14.X) / 128;
                double m12 = (v67.X - v23.X) / 128;
                double m21 = (v23.Y - v14.Y) / 256;
                double m22 = (v67.Y - v23.Y) / 256;

                if (S > 0)
                {
                    PositionEllipse.Width  = 56.0 / 256 * 48 / S * Math.Abs(Math.Sin(H * 2 * Math.PI));
                    PositionEllipse.Height = 48;
                    PositionEllipse.Fill   = (S <0.5 && V> 0.5 ? Brushes.Black : Brushes.White);

                    if (H < 0.5)
                    {
                        PositionEllipse.RenderTransform = new TranslateTransform(64 - (56.0 / 256 * 48 / S) * 0.5 * Math.Abs(Math.Sin(H * 2 * Math.PI)) + Math.Cos(H * 2 * Math.PI) * 64, 256 - 256 * V - 24 + 256.0 / 56.0 * 20 * S * Math.Sin(H * 2 * Math.PI));
                    }
                    else
                    {
                        PositionEllipse.RenderTransform = new TranslateTransform(64 - (56.0 / 256 * 48 / S) * 0.5 * Math.Abs(Math.Sin(H * 2 * Math.PI)) + Math.Cos(H * 2 * Math.PI) * 64, 256 + 20 * S * 256 / 56 - 256 * V - 24 + 256.0 / 56.0 * 20 * S * Math.Sin(H * 2 * Math.PI));
                    }
                }
                else
                {
                    PositionEllipse.Fill = null;
                }

                ColourCanvasTransform.Matrix = new Matrix(m11, m21, m12, m22, v14.X, v14.Y - (H < 0.5 ? 0 : (20 * S)));

                TopEllipse.Width           = 128 * S;
                TopEllipse.Height          = 40 * S;
                TopEllipse.RenderTransform = new TranslateTransform(64 - 64 * S, 20 - 20 * S);

                BottomEllipse.Width           = 128 * S;
                BottomEllipse.Height          = 40 * S;
                BottomEllipse.RenderTransform = new TranslateTransform(64 - 64 * S, 76 - 20 * S);

                if (H < 0.5)
                {
                    TopEllipse.Fill    = HSB.LightCylinderBrush;
                    BottomEllipse.Fill = null;
                }
                else
                {
                    BottomEllipse.Fill = HSB.DarkCylinderBrush;
                    TopEllipse.Fill    = null;
                }
            }
        }