Skip to content

Commit 7da1cae

Browse files
Add XYY
1 parent e24146b commit 7da1cae

3 files changed

Lines changed: 238 additions & 1 deletion

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using System.Numerics;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace SixLabors.ImageSharp.ColorProfiles;
8+
9+
/// <summary>
10+
/// Represents an CIE xyY 1931 color
11+
/// <see href="https://en.wikipedia.org/wiki/CIE_1931_color_space#CIE_xy_chromaticity_diagram_and_the_CIE_xyY_color_space"/>
12+
/// </summary>
13+
public readonly struct CieXyy : IColorProfile<CieXyy, CieXyz>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="CieXyy"/> struct.
17+
/// </summary>
18+
/// <param name="x">The x chroma component.</param>
19+
/// <param name="y">The y chroma component.</param>
20+
/// <param name="yl">The y luminance component.</param>
21+
[MethodImpl(InliningOptions.ShortMethod)]
22+
public CieXyy(float x, float y, float yl)
23+
{
24+
// Not clamping as documentation about this space only indicates "usual" ranges
25+
this.X = x;
26+
this.Y = y;
27+
this.Yl = yl;
28+
}
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="CieXyy"/> struct.
32+
/// </summary>
33+
/// <param name="vector">The vector representing the x, y, Y components.</param>
34+
[MethodImpl(InliningOptions.ShortMethod)]
35+
public CieXyy(Vector3 vector)
36+
: this()
37+
{
38+
// Not clamping as documentation about this space only indicates "usual" ranges
39+
this.X = vector.X;
40+
this.Y = vector.Y;
41+
this.Yl = vector.Z;
42+
}
43+
44+
/// <summary>
45+
/// Gets the X chrominance component.
46+
/// <remarks>A value usually ranging between 0 and 1.</remarks>
47+
/// </summary>
48+
public readonly float X { get; }
49+
50+
/// <summary>
51+
/// Gets the Y chrominance component.
52+
/// <remarks>A value usually ranging between 0 and 1.</remarks>
53+
/// </summary>
54+
public readonly float Y { get; }
55+
56+
/// <summary>
57+
/// Gets the Y luminance component.
58+
/// <remarks>A value usually ranging between 0 and 1.</remarks>
59+
/// </summary>
60+
public readonly float Yl { get; }
61+
62+
/// <summary>
63+
/// Compares two <see cref="CieXyy"/> objects for equality.
64+
/// </summary>
65+
/// <param name="left">The <see cref="CieXyy"/> on the left side of the operand.</param>
66+
/// <param name="right">The <see cref="CieXyy"/> on the right side of the operand.</param>
67+
/// <returns>
68+
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
69+
/// </returns>
70+
[MethodImpl(InliningOptions.ShortMethod)]
71+
public static bool operator ==(CieXyy left, CieXyy right) => left.Equals(right);
72+
73+
/// <summary>
74+
/// Compares two <see cref="CieXyy"/> objects for inequality.
75+
/// </summary>
76+
/// <param name="left">The <see cref="CieXyy"/> on the left side of the operand.</param>
77+
/// <param name="right">The <see cref="CieXyy"/> on the right side of the operand.</param>
78+
/// <returns>
79+
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
80+
/// </returns>
81+
[MethodImpl(InliningOptions.ShortMethod)]
82+
public static bool operator !=(CieXyy left, CieXyy right) => !left.Equals(right);
83+
84+
/// <inheritdoc/>
85+
public static CieXyy FromProfileConnectingSpace(ColorConversionOptions options, in CieXyz source)
86+
{
87+
float x = source.X / (source.X + source.Y + source.Z);
88+
float y = source.Y / (source.X + source.Y + source.Z);
89+
90+
if (float.IsNaN(x) || float.IsNaN(y))
91+
{
92+
return new CieXyy(0, 0, source.Y);
93+
}
94+
95+
return new CieXyy(x, y, source.Y);
96+
}
97+
98+
/// <inheritdoc/>
99+
public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieXyz> source, Span<CieXyy> destination)
100+
{
101+
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
102+
for (int i = 0; i < source.Length; i++)
103+
{
104+
CieXyz xyz = source[i];
105+
destination[i] = FromProfileConnectingSpace(options, in xyz);
106+
}
107+
}
108+
109+
/// <inheritdoc/>
110+
public CieXyz ToProfileConnectingSpace(ColorConversionOptions options)
111+
{
112+
if (MathF.Abs(this.Y) < Constants.Epsilon)
113+
{
114+
return new CieXyz(0, 0, this.Yl);
115+
}
116+
117+
float x = (this.X * this.Yl) / this.Y;
118+
float y = this.Yl;
119+
float z = ((1 - this.X - this.Y) * y) / this.Y;
120+
121+
return new CieXyz(x, y, z);
122+
}
123+
124+
/// <inheritdoc/>
125+
public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<CieXyy> source, Span<CieXyz> destination)
126+
{
127+
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));
128+
for (int i = 0; i < source.Length; i++)
129+
{
130+
CieXyy xyz = source[i];
131+
destination[i] = xyz.ToProfileConnectingSpace(options);
132+
}
133+
}
134+
135+
/// <inheritdoc/>
136+
public static ChromaticAdaptionWhitePointSource GetChromaticAdaptionWhitePointSource()
137+
=> ChromaticAdaptionWhitePointSource.WhitePoint;
138+
139+
/// <inheritdoc/>
140+
public override int GetHashCode()
141+
=> HashCode.Combine(this.X, this.Y, this.Yl);
142+
143+
/// <inheritdoc/>
144+
public override string ToString()
145+
=> FormattableString.Invariant($"CieXyy({this.X:#0.##}, {this.Y:#0.##}, {this.Yl:#0.##})");
146+
147+
/// <inheritdoc/>
148+
public override bool Equals(object? obj) => obj is CieXyy other && this.Equals(other);
149+
150+
/// <inheritdoc/>
151+
[MethodImpl(InliningOptions.ShortMethod)]
152+
public bool Equals(CieXyy other)
153+
=> this.X.Equals(other.X)
154+
&& this.Y.Equals(other.Y)
155+
&& this.Yl.Equals(other.Yl);
156+
}

tests/ImageSharp.Tests/ColorProfiles/ApproximateColorProfileComparer.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
1717
IEqualityComparer<Rgb>,
1818
IEqualityComparer<YCbCr>,
1919
IEqualityComparer<CieLchuv>,
20-
IEqualityComparer<CieLuv>
20+
IEqualityComparer<CieLuv>,
21+
IEqualityComparer<CieXyy>
2122
{
2223
private readonly float epsilon;
2324

@@ -43,6 +44,8 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
4344

4445
public bool Equals(CieLuv x, CieLuv y) => this.Equals(x.L, y.L) && this.Equals(x.U, y.U) && this.Equals(x.V, y.V);
4546

47+
public bool Equals(CieXyy x, CieXyy y) => this.Equals(x.X, y.X) && this.Equals(x.Y, y.Y) && this.Equals(x.Yl, y.Yl);
48+
4649
public int GetHashCode([DisallowNull] CieLab obj) => obj.GetHashCode();
4750

4851
public int GetHashCode([DisallowNull] CieXyz obj) => obj.GetHashCode();
@@ -59,6 +62,8 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
5962

6063
public int GetHashCode([DisallowNull] CieLuv obj) => obj.GetHashCode();
6164

65+
public int GetHashCode([DisallowNull] CieXyy obj) => obj.GetHashCode();
66+
6267
private bool Equals(float x, float y)
6368
{
6469
float d = x - y;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Six Labors Split License.
3+
4+
using SixLabors.ImageSharp.ColorProfiles;
5+
6+
namespace SixLabors.ImageSharp.Tests.ColorProfiles.Conversion;
7+
8+
/// <summary>
9+
/// Tests <see cref="CieXyz"/>-<see cref="CieXyy"/> conversions.
10+
/// </summary>
11+
/// <remarks>
12+
/// Test data generated using:
13+
/// <see href="http://www.brucelindbloom.com/index.html?ColorCalculator.html"/>
14+
/// </remarks>
15+
public class CieXyzAndCieXyyConversionTest
16+
{
17+
private static readonly ApproximateColorProfileComparer Comparer = new(.0001F);
18+
19+
[Theory]
20+
[InlineData(0.436075, 0.222504, 0.013932, 0.648427, 0.330856, 0.222504)]
21+
[InlineData(0.964220, 1.000000, 0.825210, 0.345669, 0.358496, 1.000000)]
22+
[InlineData(0.434119, 0.356820, 0.369447, 0.374116, 0.307501, 0.356820)]
23+
[InlineData(0, 0, 0, 0.538842, 0.000000, 0.000000)]
24+
public void Convert_Xyy_To_Xyz(float xyzX, float xyzY, float xyzZ, float x, float y, float yl)
25+
{
26+
CieXyy input = new(x, y, yl);
27+
CieXyz expected = new(xyzX, xyzY, xyzZ);
28+
ColorProfileConverter converter = new();
29+
30+
Span<CieXyy> inputSpan = new CieXyy[5];
31+
inputSpan.Fill(input);
32+
33+
Span<CieXyz> actualSpan = new CieXyz[5];
34+
35+
// Act
36+
CieXyz actual = converter.Convert<CieXyy, CieXyz>(input);
37+
converter.Convert<CieXyy, CieXyz>(inputSpan, actualSpan);
38+
39+
// Assert
40+
Assert.Equal(expected, actual, Comparer);
41+
42+
for (int i = 0; i < actualSpan.Length; i++)
43+
{
44+
Assert.Equal(expected, actualSpan[i], Comparer);
45+
}
46+
}
47+
48+
[Theory]
49+
[InlineData(0.436075, 0.222504, 0.013932, 0.648427, 0.330856, 0.222504)]
50+
[InlineData(0.964220, 1.000000, 0.825210, 0.345669, 0.358496, 1.000000)]
51+
[InlineData(0.434119, 0.356820, 0.369447, 0.374116, 0.307501, 0.356820)]
52+
[InlineData(0.231809, 0, 0.077528, 0.749374, 0.000000, 0.000000)]
53+
public void Convert_Xyz_to_Xyy(float xyzX, float xyzY, float xyzZ, float x, float y, float yl)
54+
{
55+
CieXyz input = new(xyzX, xyzY, xyzZ);
56+
CieXyy expected = new(x, y, yl);
57+
ColorProfileConverter converter = new();
58+
59+
Span<CieXyz> inputSpan = new CieXyz[5];
60+
inputSpan.Fill(input);
61+
62+
Span<CieXyy> actualSpan = new CieXyy[5];
63+
64+
// Act
65+
CieXyy actual = converter.Convert<CieXyz, CieXyy>(input);
66+
converter.Convert<CieXyz, CieXyy>(inputSpan, actualSpan);
67+
68+
// Assert
69+
Assert.Equal(expected, actual, Comparer);
70+
71+
for (int i = 0; i < actualSpan.Length; i++)
72+
{
73+
Assert.Equal(expected, actualSpan[i], Comparer);
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)