private void Update(double L, double a, double b)
        {
            WriteableBitmap bitmap = new WriteableBitmap(new PixelSize(96, 96), new Vector(96, 96), Avalonia.Platform.PixelFormat.Rgba8888, Avalonia.Platform.AlphaFormat.Unpremul);

            int offset = -1;

            if (LabComponent == Lab.LabComponents.L)
            {
                int normL = Math.Max(0, Math.Min(128, (int)Math.Round(L * 128.0)));

                offset = 96 * 96 * 4 * normL;
            }
            else if (LabComponent == Lab.LabComponents.a)
            {
                int normA = Math.Max(0, Math.Min(128, 64 + (int)Math.Round(a * 64.0)));

                offset = 96 * 96 * 4 * normA + 96 * 96 * 129 * 4;
            }
            else if (LabComponent == Lab.LabComponents.b)
            {
                int normB = Math.Max(0, Math.Min(128, 64 + (int)Math.Round(b * 64.0)));

                offset = 96 * 96 * 4 * normB + 96 * 96 * 129 * 4 * 2;
            }

            using (var fb = bitmap.Lock())
            {
                unsafe
                {
                    byte *rgbaValues = (byte *)fb.Address;

                    for (int i = 0; i < 96 * 96 * 4; i++)
                    {
                        rgbaValues[i] = Lab.LabImageData[offset + i];
                    }
                }
            }

            LabImage.Source = bitmap;


            Lab.FromLab(L, a, b, out _, out _, out _, out byte A);

            if (A > 0)
            {
                (double x, double y, double w, double h) = Lab.GetEllipsePosition(L, a, b, LabComponent);

                double C = Math.Sqrt(a * a + b * b);
                double S = C / Math.Sqrt(C * C + L * L);
                PositionEllipse.Width           = w;
                PositionEllipse.Height          = h;
                PositionEllipse.Fill            = (S <0.5 && L> 0.5 ? Brushes.Black : Brushes.White);
                PositionEllipse.RenderTransform = new TranslateTransform(x - (w * 0.5), y - h * 0.5);
            }
            else
            {
                PositionEllipse.Fill = null;
            }
        }
Beispiel #2
0
        public override IObservable <Color> DoTransition(IObservable <double> progress, Color oldValue, Color newValue)
        {
            return(progress.Select(p =>
            {
                (double L1, double a1, double b1) = Lab.ToLab(oldValue);
                (double L2, double a2, double b2) = Lab.ToLab(newValue);

                double f = Easing.Ease(p);

                byte A = (byte)(oldValue.A + ((double)newValue.A - oldValue.A) * f);

                Lab.FromLab(L1 + (L2 - L1) * f, a1 + (a2 - a1) * f, b1 + (b2 - b1) * f, out byte R, out byte G, out byte B);

                return Color.FromArgb(A, R, G, B);
            }));
        }
Beispiel #3
0
        public override IObservable <IBrush> DoTransition(IObservable <double> progress, IBrush oldBrush, IBrush newBrush)
        {
            return(progress.Select(p =>
            {
                if (oldBrush is SolidColorBrush oldSolidBrush && newBrush is SolidColorBrush newSolidBrush)
                {
                    Color oldValue = oldSolidBrush.Color;
                    Color newValue = newSolidBrush.Color;


                    (double L1, double a1, double b1) = Lab.ToLab(oldValue);
                    (double L2, double a2, double b2) = Lab.ToLab(newValue);

                    double f = Easing.Ease(p);

                    byte A = (byte)(oldValue.A + ((double)newValue.A - oldValue.A) * f);

                    Lab.FromLab(L1 + (L2 - L1) * f, a1 + (a2 - a1) * f, b1 + (b2 - b1) * f, out byte R, out byte G, out byte B);

                    return new SolidColorBrush(Color.FromArgb(A, R, G, B));
                }
        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);
        }