Skip to content

Commit e67a671

Browse files
Add some XYZ tests
1 parent ba066f9 commit e67a671

File tree

3 files changed

+160
-5
lines changed

3 files changed

+160
-5
lines changed

src/ImageSharp/ColorProfiles/CieLuv.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static CieLuv FromProfileConnectingSpace(ColorConversionOptions options,
9494
? ((116 * Math.Pow(yr, e)) - 16d)
9595
: (CieConstants.Kappa * yr);
9696

97-
if (double.IsNaN(l))
97+
if (double.IsNaN(l) || l == -0d)
9898
{
9999
l = 0;
100100
}
@@ -107,7 +107,7 @@ public static CieLuv FromProfileConnectingSpace(ColorConversionOptions options,
107107
u = 0;
108108
}
109109

110-
if (double.IsNaN(v) || u == -0d)
110+
if (double.IsNaN(v) || v == -0d)
111111
{
112112
v = 0;
113113
}
@@ -151,17 +151,17 @@ public CieXyz ToProfileConnectingSpace(ColorConversionOptions options)
151151
double x = (d - b) / (a - c);
152152
double z = (x * a) + b;
153153

154-
if (double.IsNaN(x))
154+
if (double.IsNaN(x) || x == -0d)
155155
{
156156
x = 0;
157157
}
158158

159-
if (double.IsNaN(y))
159+
if (double.IsNaN(y) || y == -0d)
160160
{
161161
y = 0;
162162
}
163163

164-
if (double.IsNaN(z))
164+
if (double.IsNaN(z) || z == -0d)
165165
{
166166
z = 0;
167167
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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;
7+
8+
/// <summary>
9+
/// Tests <see cref="CieXyz"/>-<see cref="CieLchuv"/> conversions.
10+
/// </summary>
11+
public class CieXyzAndCieLchuvConversionTests
12+
{
13+
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
14+
15+
[Theory]
16+
[InlineData(0.360555, 0.936901, 0.1001514, 97.50697, 177.345169, 142.601547)]
17+
public void Convert_CieXyz_to_CieLchuv(float x, float y, float yl, float l, float c, float h)
18+
{
19+
// Arrange
20+
CieXyz input = new(x, y, yl);
21+
CieLchuv expected = new(l, c, h);
22+
ColorProfileConverter converter = new();
23+
24+
Span<CieXyz> inputSpan = new CieXyz[5];
25+
inputSpan.Fill(input);
26+
27+
Span<CieLchuv> actualSpan = new CieLchuv[5];
28+
29+
// Act
30+
CieLchuv actual = converter.Convert<CieXyz, CieLchuv>(input);
31+
converter.Convert<CieXyz, CieLchuv>(inputSpan, actualSpan);
32+
33+
// Assert
34+
Assert.Equal(expected, actual, Comparer);
35+
36+
for (int i = 0; i < actualSpan.Length; i++)
37+
{
38+
Assert.Equal(expected, actualSpan[i], Comparer);
39+
}
40+
}
41+
42+
[Theory]
43+
[InlineData(0, 0, 0, 0, 0, 0)]
44+
[InlineData(97.50697, 177.345169, 142.601547, 0.360555, 0.936901, 0.1001514)]
45+
public void Convert_CieLchuv_to_CieXyz(float l, float c, float h, float x, float y, float yl)
46+
{
47+
// Arrange
48+
CieLchuv input = new(l, c, h);
49+
CieXyz expected = new(x, y, yl);
50+
ColorProfileConverter converter = new();
51+
52+
Span<CieLchuv> inputSpan = new CieLchuv[5];
53+
inputSpan.Fill(input);
54+
55+
Span<CieXyz> actualSpan = new CieXyz[5];
56+
57+
// Act
58+
CieXyz actual = converter.Convert<CieLchuv, CieXyz>(input);
59+
converter.Convert<CieLchuv, CieXyz>(inputSpan, actualSpan);
60+
61+
// Assert
62+
Assert.Equal(expected, actual, Comparer);
63+
64+
for (int i = 0; i < actualSpan.Length; i++)
65+
{
66+
Assert.Equal(expected, actualSpan[i], Comparer);
67+
}
68+
}
69+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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;
7+
8+
/// <summary>
9+
/// Tests <see cref="CieXyz"/>-<see cref="CieLuv"/> 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 CieXyzAndCieLuvConversionTest
16+
{
17+
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
18+
19+
[Theory]
20+
[InlineData(0, 0, 0, 0, 0, 0)]
21+
[InlineData(0, 100, 50, 0, 0, 0)]
22+
[InlineData(0.1, 100, 50, 0.000493, 0.000111, -0.000709)]
23+
[InlineData(70.0000, 86.3525, 2.8240, 0.569310, 0.407494, 0.365843)]
24+
[InlineData(10.0000, -1.2345, -10.0000, 0.012191, 0.011260, 0.025939)]
25+
[InlineData(100, 0, 0, 0.950470, 1.000000, 1.088830)]
26+
[InlineData(1, 1, 1, 0.001255, 0.001107, 0.000137)]
27+
public void Convert_Luv_to_Xyz(float l, float u, float v, float x, float y, float z)
28+
{
29+
// Arrange
30+
CieLuv input = new(l, u, v);
31+
CieXyz expected = new(x, y, z);
32+
33+
ColorConversionOptions options = new() { WhitePoint = Illuminants.D65, TargetWhitePoint = Illuminants.D65 };
34+
ColorProfileConverter converter = new(options);
35+
36+
Span<CieLuv> inputSpan = new CieLuv[5];
37+
inputSpan.Fill(input);
38+
39+
Span<CieXyz> actualSpan = new CieXyz[5];
40+
41+
// Act
42+
CieXyz actual = converter.Convert<CieLuv, CieXyz>(input);
43+
converter.Convert<CieLuv, CieXyz>(inputSpan, actualSpan);
44+
45+
// Assert
46+
Assert.Equal(expected, actual, Comparer);
47+
48+
for (int i = 0; i < actualSpan.Length; i++)
49+
{
50+
Assert.Equal(expected, actualSpan[i], Comparer);
51+
}
52+
}
53+
54+
[Theory]
55+
[InlineData(0, 0, 0, 0, 0, 0)]
56+
[InlineData(0.000493, 0.000111, 0, 0.1003, 0.9332, -0.0070)]
57+
[InlineData(0.569310, 0.407494, 0.365843, 70.0000, 86.3524, 2.8240)]
58+
[InlineData(0.012191, 0.011260, 0.025939, 9.9998, -1.2343, -9.9999)]
59+
[InlineData(0.950470, 1.000000, 1.088830, 100, 0, 0)]
60+
[InlineData(0.001255, 0.001107, 0.000137, 0.9999, 0.9998, 1.0004)]
61+
public void Convert_Xyz_to_Luv(float x, float y, float z, float l, float u, float v)
62+
{
63+
// Arrange
64+
CieXyz input = new(x, y, z);
65+
CieLuv expected = new(l, u, v);
66+
67+
ColorConversionOptions options = new() { WhitePoint = Illuminants.D65, TargetWhitePoint = Illuminants.D65 };
68+
ColorProfileConverter converter = new(options);
69+
70+
Span<CieXyz> inputSpan = new CieXyz[5];
71+
inputSpan.Fill(input);
72+
73+
Span<CieLuv> actualSpan = new CieLuv[5];
74+
75+
// Act
76+
CieLuv actual = converter.Convert<CieXyz, CieLuv>(input);
77+
78+
// Assert
79+
Assert.Equal(expected, actual, Comparer);
80+
81+
for (int i = 0; i < actualSpan.Length; i++)
82+
{
83+
Assert.Equal(expected, actualSpan[i], Comparer);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)