Browse Source

Complete HunterLab and add more tests

pull/2739/head
James Jackson-South 2 years ago
parent
commit
2fbabe828c
  1. 4
      src/ImageSharp/ColorProfiles/HunterLab.cs
  2. 17
      tests/ImageSharp.Tests/ColorProfiles/ApproximateColorProfileComparer.cs
  3. 70
      tests/ImageSharp.Tests/ColorProfiles/CieLabAndHslConversionTests.cs
  4. 70
      tests/ImageSharp.Tests/ColorProfiles/CieLabAndHsvConversionTests.cs
  5. 70
      tests/ImageSharp.Tests/ColorProfiles/CieLabAndHunterLabConversionTests.cs

4
src/ImageSharp/ColorProfiles/HunterLab.cs

@ -132,8 +132,8 @@ public readonly struct HunterLab : IColorProfile<HunterLab, CieXyz>
float l = this.L, a = this.A, b = this.B;
float xn = whitePoint.X, yn = whitePoint.Y, zn = whitePoint.Z;
float ka = ComputeKa(whitePoint);
float kb = ComputeKb(whitePoint);
float ka = ComputeKa(in whitePoint);
float kb = ComputeKb(in whitePoint);
float pow = Numerics.Pow2(l / 100F);
float sqrtPow = MathF.Sqrt(pow);

17
tests/ImageSharp.Tests/ColorProfiles/ApproximateColorProfileComparer.cs

@ -19,7 +19,10 @@ internal readonly struct ApproximateColorProfileComparer :
IEqualityComparer<CieLchuv>,
IEqualityComparer<CieLuv>,
IEqualityComparer<CieXyy>,
IEqualityComparer<Cmyk>
IEqualityComparer<Cmyk>,
IEqualityComparer<Hsl>,
IEqualityComparer<Hsv>,
IEqualityComparer<HunterLab>
{
private readonly float epsilon;
@ -49,6 +52,12 @@ internal readonly struct ApproximateColorProfileComparer :
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);
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);
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);
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);
public int GetHashCode([DisallowNull] CieLab obj) => obj.GetHashCode();
public int GetHashCode([DisallowNull] CieXyz obj) => obj.GetHashCode();
@ -69,6 +78,12 @@ internal readonly struct ApproximateColorProfileComparer :
public int GetHashCode([DisallowNull] Cmyk obj) => obj.GetHashCode();
public int GetHashCode([DisallowNull] Hsl obj) => obj.GetHashCode();
public int GetHashCode([DisallowNull] Hsv obj) => obj.GetHashCode();
public int GetHashCode([DisallowNull] HunterLab obj) => obj.GetHashCode();
private bool Equals(float x, float y)
{
float d = x - y;

70
tests/ImageSharp.Tests/ColorProfiles/CieLabAndHslConversionTests.cs

@ -0,0 +1,70 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.ColorProfiles;
namespace SixLabors.ImageSharp.Tests.ColorProfiles;
/// <summary>
/// Tests <see cref="CieLab"/>-<see cref="Hsl"/> conversions.
/// </summary>
public class CieLabAndHslConversionTests
{
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
[Theory]
[InlineData(0, 0, 0, 0, 0, 0)]
[InlineData(336.9393, 1, 0.5, 55.063, 82.54868, 23.16508)]
public void Convert_Hsl_to_CieLab(float h, float s, float ll, float l, float a, float b)
{
// Arrange
Hsl input = new(h, s, ll);
CieLab expected = new(l, a, b);
ColorProfileConverter converter = new();
Span<Hsl> inputSpan = new Hsl[5];
inputSpan.Fill(input);
Span<CieLab> actualSpan = new CieLab[5];
// Act
CieLab actual = converter.Convert<Hsl, CieLab>(input);
converter.Convert<Hsl, CieLab>(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
[Theory]
[InlineData(0, 0, 0, 0, 0, 0)]
[InlineData(55.063, 82.54868, 23.16508, 336.9393, 1, 0.5)]
public void Convert_CieLab_to_Hsl(float l, float a, float b, float h, float s, float ll)
{
// Arrange
CieLab input = new(l, a, b);
Hsl expected = new(h, s, ll);
ColorProfileConverter converter = new();
Span<CieLab> inputSpan = new CieLab[5];
inputSpan.Fill(input);
Span<Hsl> actualSpan = new Hsl[5];
// Act
Hsl actual = converter.Convert<CieLab, Hsl>(input);
converter.Convert<CieLab, Hsl>(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
}

70
tests/ImageSharp.Tests/ColorProfiles/CieLabAndHsvConversionTests.cs

@ -0,0 +1,70 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.ColorProfiles;
namespace SixLabors.ImageSharp.Tests.ColorProfiles;
/// <summary>
/// Tests <see cref="CieLab"/>-<see cref="Hsv"/> conversions.
/// </summary>
public class CieLabAndHsvConversionTests
{
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
[Theory]
[InlineData(0, 0, 0, 0, 0, 0)]
[InlineData(336.9393, 1, 0.9999999, 55.063, 82.54871, 23.16504)]
public void Convert_Hsv_to_CieLab(float h, float s, float v, float l, float a, float b)
{
// Arrange
Hsv input = new(h, s, v);
CieLab expected = new(l, a, b);
ColorProfileConverter converter = new();
Span<Hsv> inputSpan = new Hsv[5];
inputSpan.Fill(input);
Span<CieLab> actualSpan = new CieLab[5];
// Act
CieLab actual = converter.Convert<Hsv, CieLab>(input);
converter.Convert<Hsv, CieLab>(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
[Theory]
[InlineData(0, 0, 0, 0, 0, 0)]
[InlineData(55.063, 82.54871, 23.16504, 336.9393, 1, 0.9999999)]
public void Convert_CieLab_to_Hsv(float l, float a, float b, float h, float s, float v)
{
// Arrange
CieLab input = new(l, a, b);
Hsv expected = new(h, s, v);
ColorProfileConverter converter = new();
Span<CieLab> inputSpan = new CieLab[5];
inputSpan.Fill(input);
Span<Hsv> actualSpan = new Hsv[5];
// Act
Hsv actual = converter.Convert<CieLab, Hsv>(input);
converter.Convert<CieLab, Hsv>(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
}

70
tests/ImageSharp.Tests/ColorProfiles/CieLabAndHunterLabConversionTests.cs

@ -0,0 +1,70 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.ColorProfiles;
namespace SixLabors.ImageSharp.Tests.ColorProfiles;
/// <summary>
/// Tests <see cref="CieLab"/>-<see cref="HunterLab"/> conversions.
/// </summary>
public class CieLabAndHunterLabConversionTests
{
private static readonly ApproximateColorProfileComparer Comparer = new(.0002f);
[Theory]
[InlineData(0, 0, 0, 0, 0, 0)]
[InlineData(27.51646, 556.9392, -0.03974226, 33.074177, 281.48329, -0.06948)]
public void Convert_HunterLab_to_CieLab(float l2, float a2, float b2, float l, float a, float b)
{
// Arrange
HunterLab input = new(l2, a2, b2);
CieLab expected = new(l, a, b);
ColorProfileConverter converter = new();
Span<HunterLab> inputSpan = new HunterLab[5];
inputSpan.Fill(input);
Span<CieLab> actualSpan = new CieLab[5];
// Act
CieLab actual = converter.Convert<HunterLab, CieLab>(input);
converter.Convert<HunterLab, CieLab>(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
[Theory]
[InlineData(0, 0, 0, 0, 0, 0)]
[InlineData(33.074177, 281.48329, -0.06948, 27.51646, 556.9392, -0.03974226)]
public void Convert_CieLab_to_HunterLab(float l, float a, float b, float l2, float a2, float b2)
{
// Arrange
CieLab input = new(l, a, b);
HunterLab expected = new(l2, a2, b2);
ColorProfileConverter converter = new();
Span<CieLab> inputSpan = new CieLab[5];
inputSpan.Fill(input);
Span<HunterLab> actualSpan = new HunterLab[5];
// Act
HunterLab actual = converter.Convert<CieLab, HunterLab>(input);
converter.Convert<CieLab, HunterLab>(inputSpan, actualSpan);
// Assert
Assert.Equal(expected, actual, Comparer);
for (int i = 0; i < actualSpan.Length; i++)
{
Assert.Equal(expected, actualSpan[i], Comparer);
}
}
}
Loading…
Cancel
Save