Files
Chronos/src/Chronos.Core/Math/Matrix3.cs
T
Alby96andClaude Opus 5.5 f2b58522b9
CI / Build and test (push) Successful in 8m14s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 13m19s
CI / PostgreSQL 16 integration (push) Successful in 4m49s
CI / PostgreSQL 17 integration (push) Successful in 5m28s
CI / Docker image (push) Successful in 53s
Release / Server image (push) Failing after 10s
refactor!: rename Titano to Chronos and move CI and releases to Gitea (#6)
The owner renamed the product and moved the repository to the self-hosted
Gitea (https://gitea.encke-hake.ts.net/BalboSoft/Chronos) for more control,
and asked for Actions and a first automatic release with the server package
and the Windows exe (docs/requests/2026-10-23-chronos-gitea.md).

- Chronos everywhere: a byte-exact rename of contents and paths, then a
  per-language review of the forms the old name's ending had dictated.
- Gitea instead of GitHub: the update check, licence, issue and support
  links read ChronosReleasesUrl; the update check takes the highest stable
  version, offers a pre-release build its final release and reports a
  repository it cannot see as a failed check. EULA, security policy and
  Gitea issue forms live at the repository root.
- Gitea Actions for the Linux act_runner (.gitea/workflows): CI, release,
  nightly image and performance gates in the .NET SDK container with git
  checkout and the system SQLite. The release cross-compiles the Windows
  programs, checks their icon, version and DPI manifest, packs installers
  and zips, publishes the Gitea release and pushes the server image
  (REGISTRY_TOKEN). Windows-only tests run in an opt-in job on a Windows
  runner (WINDOWS_RUNNER).
- New Chronos.Tools commands: zip, gitea-release, postgres-ci (replaces the
  docker-exec script, which Gitea's service containers do not allow; the
  throw-away server runs without durability) and pe-check.
- Timed tests take their budget from TestTiming (CHRONOS_TEST_TIME_FACTOR,
  3 on the shared runner; the weekly performance gates keep theirs).

BREAKING CHANGE: programs, settings, data folders, file types, environment
variables and the Docker image use the new name; nothing is migrated from
Titano, which never had a release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-10-23 12:45:18 +02:00

94 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace Chronos.Core.Mathematics;
/// <summary>A 3-vector in double precision (colour triplets, homogeneous 2-D points).</summary>
public readonly record struct Vector3D(double X, double Y, double Z);
/// <summary>Row-major 3×3 matrix in double precision for colour-space and planar projective work.</summary>
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);
/// <summary>Bradford cone-response matrix (Lam 1985), XYZ → sharpened LMS.</summary>
public static Matrix3 Bradford { get; } = new(
0.8951, 0.2664, -0.1614,
-0.7502, 1.7135, 0.0367,
0.0389, -0.0685, 1.0296);
/// <summary>Linear Rec.709/sRGB (D65) → CIE XYZ, derived from the primaries.</summary>
public static Matrix3 Rec709ToXyz { get; } = RgbToXyz(new(0.64, 0.33), new(0.30, 0.60), new(0.15, 0.06), Chromaticity.D65);
/// <summary>CIE XYZ → linear Rec.709/sRGB (D65).</summary>
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);
/// <summary>Matrix whose columns are <paramref name="c1"/>, <paramref name="c2"/>, <paramref name="c3"/>.</summary>
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);
/// <summary>Inverse by the adjugate; false when the matrix is singular relative to its scale.</summary>
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;
}
/// <exception cref="InvalidOperationException">The matrix is singular.</exception>
public Matrix3 Inverse() => TryInvert(out var inverse) ? inverse : throw new InvalidOperationException("Matrix is singular.");
/// <summary>
/// Bradford chromatic adaptation (von Kries scaling in the Bradford cone space) mapping colours seen under
/// <paramref name="sourceWhite"/> to <paramref name="destinationWhite"/>; both whites as XYZ.
/// </summary>
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;
}
/// <summary>RGB → XYZ matrix of an additive colour space from its primaries and white point (white maps to Y = 1).</summary>
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)));
}
}