Ejemplo n.º 1
0
        private static XyzColor LabToXyz(LabColor color)
        {
            // Convert from LAB to XYZ.
            // Algorithm from https://www.easyrgb.com/en/math.php

            double y = (color.L + 16) / 116.0;
            double x = (color.A / 500.0) + y;
            double z = y - color.B / 200.0f;

            x = Math.Pow(x, 3) > 0.008856 ? Math.Pow(x, 3) : (x - 16 / 116.0) / 7.787;
            y = Math.Pow(y, 3) > 0.008856 ? Math.Pow(y, 3) : (y - 16 / 116.0) / 7.787;
            z = Math.Pow(z, 3) > 0.008856 ? Math.Pow(z, 3) : (z - 16 / 116.0) / 7.787;

            x *= 0.95047;
            y *= 1.00000;
            z *= 1.08883;

            return(new XyzColor(x, y, z));
        }
        public double GetDistance(Color first, Color second, bool normalize)
        {
            // Algorithm from https://www.easyrgb.com/en/math.php

            LabColor labFirst  = first.ToLab();
            LabColor labSecond = second.ToLab();

            double deltaE = Math.Sqrt(
                Math.Pow(labFirst.L - labSecond.L, 2) +
                Math.Pow(labFirst.A - labSecond.A, 2) +
                Math.Pow(labFirst.B - labSecond.B, 2));

            if (normalize)
            {
                return(Math.Min(deltaE / 100.0, 1.0));
            }
            else
            {
                return(deltaE);
            }
        }