Files
Chronos/src/Titano.Core/Math/Chromaticity.cs
T

40 lines
1.6 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 Titano.Core.Mathematics;
/// <summary>CIE 1931 chromaticity coordinates (lower-case x, y in the literature).</summary>
public readonly record struct Chromaticity(double X, double Y)
{
/// <summary>CIE standard illuminant D65 (2° observer).</summary>
public static Chromaticity D65 { get; } = new(0.3127, 0.3290);
/// <summary>CIE standard illuminant D50 (2° observer), the ICC/DNG profile connection white.</summary>
public static Chromaticity D50 { get; } = new(0.3457, 0.3585);
/// <summary>Tristimulus values with luminance <paramref name="luminance"/>: X = x·Y/y, Z = (1 − x − y)·Y/y.</summary>
public Vector3D ToXyz(double luminance = 1)
{
if (!(Y > 0)) throw new InvalidOperationException("Chromaticity y must be positive.");
return new Vector3D(X * luminance / Y, luminance, (1 - X - Y) * luminance / Y);
}
/// <summary>Chromaticity of tristimulus values; NaN coordinates when X + Y + Z is not positive.</summary>
public static Chromaticity FromXyz(Vector3D xyz)
{
var sum = xyz.X + xyz.Y + xyz.Z;
return sum > 0 ? new Chromaticity(xyz.X / sum, xyz.Y / sum) : new Chromaticity(double.NaN, double.NaN);
}
/// <summary>CIE 1960 UCS coordinates u = 4x/(−2x + 12y + 3), v = 6y/(−2x + 12y + 3).</summary>
public (double U, double V) ToUv1960()
{
var d = -2 * X + 12 * Y + 3;
return (4 * X / d, 6 * Y / d);
}
/// <summary>Chromaticity from CIE 1960 UCS coordinates.</summary>
public static Chromaticity FromUv1960(double u, double v)
{
var d = 2 * u - 8 * v + 4;
return new Chromaticity(3 * u / d, 2 * v / d);
}
}