namespace Chronos.Core.Mathematics;
/// A 3-vector in double precision (colour triplets, homogeneous 2-D points).
public readonly record struct Vector3D(double X, double Y, double Z);
/// Row-major 3×3 matrix in double precision for colour-space and planar projective work.
public readonly record struct Matrix3(
double M11, double M12, double M13,
double M21, double M22, double M23,
double M31, double M32, double M33)
{
public static Matrix3 Identity { get; } = new(1, 0, 0, 0, 1, 0, 0, 0, 1);
/// Bradford cone-response matrix (Lam 1985), XYZ → sharpened LMS.
public static Matrix3 Bradford { get; } = new(
0.8951, 0.2664, -0.1614,
-0.7502, 1.7135, 0.0367,
0.0389, -0.0685, 1.0296);
/// Linear Rec.709/sRGB (D65) → CIE XYZ, derived from the primaries.
public static Matrix3 Rec709ToXyz { get; } = RgbToXyz(new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), Chromaticity.D65);
/// CIE XYZ → linear Rec.709/sRGB (D65).
public static Matrix3 XyzToRec709 { get; } = Rec709ToXyz.Inverse();
public static Matrix3 Diagonal(double a, double b, double c) => new(a, 0, 0, 0, b, 0, 0, 0, c);
/// Matrix whose columns are , , .
public static Matrix3 FromColumns(Vector3D c1, Vector3D c2, Vector3D c3) => new(c1.X, c2.X, c3.X, c1.Y, c2.Y, c3.Y, c1.Z, c2.Z, c3.Z);
public static Matrix3 operator *(Matrix3 a, Matrix3 b) => new(
a.M11 * b.M11 + a.M12 * b.M21 + a.M13 * b.M31, a.M11 * b.M12 + a.M12 * b.M22 + a.M13 * b.M32, a.M11 * b.M13 + a.M12 * b.M23 + a.M13 * b.M33,
a.M21 * b.M11 + a.M22 * b.M21 + a.M23 * b.M31, a.M21 * b.M12 + a.M22 * b.M22 + a.M23 * b.M32, a.M21 * b.M13 + a.M22 * b.M23 + a.M23 * b.M33,
a.M31 * b.M11 + a.M32 * b.M21 + a.M33 * b.M31, a.M31 * b.M12 + a.M32 * b.M22 + a.M33 * b.M32, a.M31 * b.M13 + a.M32 * b.M23 + a.M33 * b.M33);
public static Vector3D operator *(Matrix3 m, Vector3D v) => new(
m.M11 * v.X + m.M12 * v.Y + m.M13 * v.Z,
m.M21 * v.X + m.M22 * v.Y + m.M23 * v.Z,
m.M31 * v.X + m.M32 * v.Y + m.M33 * v.Z);
public double Determinant =>
M11 * (M22 * M33 - M23 * M32) - M12 * (M21 * M33 - M23 * M31) + M13 * (M21 * M32 - M22 * M31);
public Matrix3 Transpose() => new(M11, M21, M31, M12, M22, M32, M13, M23, M33);
/// Inverse by the adjugate; false when the matrix is singular relative to its scale.
public bool TryInvert(out Matrix3 inverse)
{
var det = Determinant;
var scale = MaxAbs();
if (!(Math.Abs(det) > 1e-14 * scale * scale * scale) || !double.IsFinite(det))
{
inverse = default;
return false;
}
var r = 1 / det;
inverse = new Matrix3(
(M22 * M33 - M23 * M32) * r, (M13 * M32 - M12 * M33) * r, (M12 * M23 - M13 * M22) * r,
(M23 * M31 - M21 * M33) * r, (M11 * M33 - M13 * M31) * r, (M13 * M21 - M11 * M23) * r,
(M21 * M32 - M22 * M31) * r, (M12 * M31 - M11 * M32) * r, (M11 * M22 - M12 * M21) * r);
return true;
}
/// The matrix is singular.
public Matrix3 Inverse() => TryInvert(out var inverse) ? inverse : throw new InvalidOperationException("Matrix is singular.");
///
/// Bradford chromatic adaptation (von Kries scaling in the Bradford cone space) mapping colours seen under
/// to ; both whites as XYZ.
///
public static Matrix3 BradfordAdaptation(Vector3D sourceWhite, Vector3D destinationWhite)
{
var s = Bradford * sourceWhite;
var d = Bradford * destinationWhite;
if (!(s.X > 0 && s.Y > 0 && s.Z > 0)) throw new ArgumentOutOfRangeException(nameof(sourceWhite), "White point must have positive cone responses.");
return Bradford.Inverse() * Diagonal(d.X / s.X, d.Y / s.Y, d.Z / s.Z) * Bradford;
}
/// RGB → XYZ matrix of an additive colour space from its primaries and white point (white maps to Y = 1).
public static Matrix3 RgbToXyz(Chromaticity red, Chromaticity green, Chromaticity blue, Chromaticity white)
{
var primaries = FromColumns(red.ToXyz(), green.ToXyz(), blue.ToXyz());
var s = primaries.Inverse() * white.ToXyz();
return primaries * Diagonal(s.X, s.Y, s.Z);
}
private double MaxAbs()
{
var m = Math.Max(Math.Max(Math.Abs(M11), Math.Abs(M12)), Math.Abs(M13));
m = Math.Max(m, Math.Max(Math.Max(Math.Abs(M21), Math.Abs(M22)), Math.Abs(M23)));
return Math.Max(m, Math.Max(Math.Max(Math.Abs(M31), Math.Abs(M32)), Math.Abs(M33)));
}
}