Skip to content

Commit 2fbabe8

Browse files
Complete HunterLab and add more tests
1 parent e7a2445 commit 2fbabe8

5 files changed

Lines changed: 228 additions & 3 deletions

File tree

src/ImageSharp/ColorProfiles/HunterLab.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public CieXyz ToProfileConnectingSpace(ColorConversionOptions options)
132132
float l = this.L, a = this.A, b = this.B;
133133
float xn = whitePoint.X, yn = whitePoint.Y, zn = whitePoint.Z;
134134

135-
float ka = ComputeKa(whitePoint);
136-
float kb = ComputeKb(whitePoint);
135+
float ka = ComputeKa(in whitePoint);
136+
float kb = ComputeKb(in whitePoint);
137137

138138
float pow = Numerics.Pow2(l / 100F);
139139
float sqrtPow = MathF.Sqrt(pow);

tests/ImageSharp.Tests/ColorProfiles/ApproximateColorProfileComparer.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
1919
IEqualityComparer<CieLchuv>,
2020
IEqualityComparer<CieLuv>,
2121
IEqualityComparer<CieXyy>,
22-
IEqualityComparer<Cmyk>
22+
IEqualityComparer<Cmyk>,
23+
IEqualityComparer<Hsl>,
24+
IEqualityComparer<Hsv>,
25+
IEqualityComparer<HunterLab>
2326
{
2427
private readonly float epsilon;
2528

@@ -49,6 +52,12 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
4952

5053
public bool Equals(Cmyk x, Cmyk y) => this.Equals(x.C, y.C) && this.Equals(x.M, y.M) && this.Equals(x.Y, y.Y) && this.Equals(x.K, y.K);
5154

55+
public bool Equals(Hsl x, Hsl y) => this.Equals(x.H, y.H) && this.Equals(x.S, y.S) && this.Equals(x.L, y.L);
56+
57+
public bool Equals(Hsv x, Hsv y) => this.Equals(x.H, y.H) && this.Equals(x.S, y.S) && this.Equals(x.V, y.V);
58+
59+
public bool Equals(HunterLab x, HunterLab y) => this.Equals(x.L, y.L) && this.Equals(x.A, y.A) && this.Equals(x.B, y.B);
60+
5261
public int GetHashCode([DisallowNull] CieLab obj) => obj.GetHashCode();
5362

5463
public int GetHashCode([DisallowNull] CieXyz obj) => obj.GetHashCode();
@@ -69,6 +78,12 @@ namespace SixLabors.ImageSharp.Tests.ColorProfiles;
6978

7079
public int GetHashCode([DisallowNull] Cmyk obj) => obj.GetHashCode();
7180

81+
public int GetHashCode([DisallowNull] Hsl obj) => obj.GetHashCode();
82+
83+
public int GetHashCode([DisallowNull] Hsv obj) => obj.GetHashCode();
84+
85+
public int GetHashCode([DisallowNull] HunterLab obj) => obj.GetHashCode();
86+
7287
private bool Equals(float x, float y)
7388
{
7489
float d = x - y;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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="CieLab"/>-<see cref="Hsl"/> conversions.
10+
/// </summary>
11+
public class CieLabAndHslConversionTests
12+
{
13+
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
14+
15+
[Theory]
16+
[InlineData(0, 0, 0, 0, 0, 0)]
17+
[InlineData(336.9393, 1, 0.5, 55.063, 82.54868, 23.16508)]
18+
public void Convert_Hsl_to_CieLab(float h, float s, float ll, float l, float a, float b)
19+
{
20+
// Arrange
21+
Hsl input = new(h, s, ll);
22+
CieLab expected = new(l, a, b);
23+
ColorProfileConverter converter = new();
24+
25+
Span<Hsl> inputSpan = new Hsl[5];
26+
inputSpan.Fill(input);
27+
28+
Span<CieLab> actualSpan = new CieLab[5];
29+
30+
// Act
31+
CieLab actual = converter.Convert<Hsl, CieLab>(input);
32+
converter.Convert<Hsl, CieLab>(inputSpan, actualSpan);
33+
34+
// Assert
35+
Assert.Equal(expected, actual, Comparer);
36+
37+
for (int i = 0; i < actualSpan.Length; i++)
38+
{
39+
Assert.Equal(expected, actualSpan[i], Comparer);
40+
}
41+
}
42+
43+
[Theory]
44+
[InlineData(0, 0, 0, 0, 0, 0)]
45+
[InlineData(55.063, 82.54868, 23.16508, 336.9393, 1, 0.5)]
46+
public void Convert_CieLab_to_Hsl(float l, float a, float b, float h, float s, float ll)
47+
{
48+
// Arrange
49+
CieLab input = new(l, a, b);
50+
Hsl expected = new(h, s, ll);
51+
ColorProfileConverter converter = new();
52+
53+
Span<CieLab> inputSpan = new CieLab[5];
54+
inputSpan.Fill(input);
55+
56+
Span<Hsl> actualSpan = new Hsl[5];
57+
58+
// Act
59+
Hsl actual = converter.Convert<CieLab, Hsl>(input);
60+
converter.Convert<CieLab, Hsl>(inputSpan, actualSpan);
61+
62+
// Assert
63+
Assert.Equal(expected, actual, Comparer);
64+
65+
for (int i = 0; i < actualSpan.Length; i++)
66+
{
67+
Assert.Equal(expected, actualSpan[i], Comparer);
68+
}
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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="CieLab"/>-<see cref="Hsv"/> conversions.
10+
/// </summary>
11+
public class CieLabAndHsvConversionTests
12+
{
13+
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
14+
15+
[Theory]
16+
[InlineData(0, 0, 0, 0, 0, 0)]
17+
[InlineData(336.9393, 1, 0.9999999, 55.063, 82.54871, 23.16504)]
18+
public void Convert_Hsv_to_CieLab(float h, float s, float v, float l, float a, float b)
19+
{
20+
// Arrange
21+
Hsv input = new(h, s, v);
22+
CieLab expected = new(l, a, b);
23+
ColorProfileConverter converter = new();
24+
25+
Span<Hsv> inputSpan = new Hsv[5];
26+
inputSpan.Fill(input);
27+
28+
Span<CieLab> actualSpan = new CieLab[5];
29+
30+
// Act
31+
CieLab actual = converter.Convert<Hsv, CieLab>(input);
32+
converter.Convert<Hsv, CieLab>(inputSpan, actualSpan);
33+
34+
// Assert
35+
Assert.Equal(expected, actual, Comparer);
36+
37+
for (int i = 0; i < actualSpan.Length; i++)
38+
{
39+
Assert.Equal(expected, actualSpan[i], Comparer);
40+
}
41+
}
42+
43+
[Theory]
44+
[InlineData(0, 0, 0, 0, 0, 0)]
45+
[InlineData(55.063, 82.54871, 23.16504, 336.9393, 1, 0.9999999)]
46+
public void Convert_CieLab_to_Hsv(float l, float a, float b, float h, float s, float v)
47+
{
48+
// Arrange
49+
CieLab input = new(l, a, b);
50+
Hsv expected = new(h, s, v);
51+
ColorProfileConverter converter = new();
52+
53+
Span<CieLab> inputSpan = new CieLab[5];
54+
inputSpan.Fill(input);
55+
56+
Span<Hsv> actualSpan = new Hsv[5];
57+
58+
// Act
59+
Hsv actual = converter.Convert<CieLab, Hsv>(input);
60+
converter.Convert<CieLab, Hsv>(inputSpan, actualSpan);
61+
62+
// Assert
63+
Assert.Equal(expected, actual, Comparer);
64+
65+
for (int i = 0; i < actualSpan.Length; i++)
66+
{
67+
Assert.Equal(expected, actualSpan[i], Comparer);
68+
}
69+
}
70+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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="CieLab"/>-<see cref="HunterLab"/> conversions.
10+
/// </summary>
11+
public class CieLabAndHunterLabConversionTests
12+
{
13+
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
14+
15+
[Theory]
16+
[InlineData(0, 0, 0, 0, 0, 0)]
17+
[InlineData(27.51646, 556.9392, -0.03974226, 33.074177, 281.48329, -0.06948)]
18+
public void Convert_HunterLab_to_CieLab(float l2, float a2, float b2, float l, float a, float b)
19+
{
20+
// Arrange
21+
HunterLab input = new(l2, a2, b2);
22+
CieLab expected = new(l, a, b);
23+
ColorProfileConverter converter = new();
24+
25+
Span<HunterLab> inputSpan = new HunterLab[5];
26+
inputSpan.Fill(input);
27+
28+
Span<CieLab> actualSpan = new CieLab[5];
29+
30+
// Act
31+
CieLab actual = converter.Convert<HunterLab, CieLab>(input);
32+
converter.Convert<HunterLab, CieLab>(inputSpan, actualSpan);
33+
34+
// Assert
35+
Assert.Equal(expected, actual, Comparer);
36+
37+
for (int i = 0; i < actualSpan.Length; i++)
38+
{
39+
Assert.Equal(expected, actualSpan[i], Comparer);
40+
}
41+
}
42+
43+
[Theory]
44+
[InlineData(0, 0, 0, 0, 0, 0)]
45+
[InlineData(33.074177, 281.48329, -0.06948, 27.51646, 556.9392, -0.03974226)]
46+
public void Convert_CieLab_to_HunterLab(float l, float a, float b, float l2, float a2, float b2)
47+
{
48+
// Arrange
49+
CieLab input = new(l, a, b);
50+
HunterLab expected = new(l2, a2, b2);
51+
ColorProfileConverter converter = new();
52+
53+
Span<CieLab> inputSpan = new CieLab[5];
54+
inputSpan.Fill(input);
55+
56+
Span<HunterLab> actualSpan = new HunterLab[5];
57+
58+
// Act
59+
HunterLab actual = converter.Convert<CieLab, HunterLab>(input);
60+
converter.Convert<CieLab, HunterLab>(inputSpan, actualSpan);
61+
62+
// Assert
63+
Assert.Equal(expected, actual, Comparer);
64+
65+
for (int i = 0; i < actualSpan.Length; i++)
66+
{
67+
Assert.Equal(expected, actualSpan[i], Comparer);
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)