private static Color XyzToRgb(XyzColor color) { // Convert from XYZ to RGB. // Algorithm from https://www.easyrgb.com/en/math.php double tempX = color.X / 100.0; double tempY = color.Y / 100.0; double tempZ = color.Z / 100.0; double r = tempX * 3.2406 + tempY * -1.5372 + tempZ * -0.4986; double g = tempX * -0.9689 + tempY * 1.8758 + tempZ * 0.0415; double b = tempX * 0.0557 + tempY * -0.2040 + tempZ * 1.0570; r = r > 0.0031308 ? 1.055 * Math.Pow(r, 1 / 2.4) - 0.055 : 12.92 * r; g = g > 0.0031308 ? 1.055 * Math.Pow(g, 1 / 2.4) - 0.055 : 12.92 * g; b = b > 0.0031308 ? 1.055 * Math.Pow(b, 1 / 2.4) - 0.055 : 12.92 * b; r *= 255.0; g *= 255.0; b *= 255.0; return(Color.FromArgb((byte)r, (byte)g, (byte)b)); }
private static LabColor XyzToLab(XyzColor color) { // Convert from XYZ to LAB. // Algorithm from https://www.easyrgb.com/en/math.php // Using D65 observer values double x = color.X; double y = color.Y; double z = color.Z; x /= referenceX; y /= referenceY; z /= referenceZ; x = x > 0.008856 ? Math.Pow(x, 1 / 3.0) : (7.787 * x) + (16 / 116.0); y = y > 0.008856 ? Math.Pow(y, 1 / 3.0) : (7.787 * y) + (16 / 116.0); z = z > 0.008856 ? Math.Pow(z, 1 / 3.0) : (7.787 * z) + (16 / 116.0); double l = (116 * y) - 16; double a = 500 * (x - y); double b = 200 * (y - z); return(new LabColor(l, a, b)); }
public static LabColor FromXyz(XyzColor color) { return(FromXyz(color.X, color.Y, color.Z)); }
public static LabColor FromRgb(Color color) { return(XyzToLab(XyzColor.FromRgb(color))); }