mirror of https://github.com/SixLabors/ImageSharp
8 changed files with 224 additions and 16 deletions
@ -0,0 +1,108 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Numerics; |
|||
using SixLabors.ImageSharp.ColorProfiles.Companding; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests.ColorProfiles; |
|||
|
|||
/// <summary>
|
|||
/// Tests various companding algorithms. Expanded numbers are hand calculated from formulas online.
|
|||
/// </summary>
|
|||
public class CompandingTests |
|||
{ |
|||
private static readonly ApproximateFloatComparer Comparer = new(.000001F); |
|||
|
|||
[Fact] |
|||
public void Rec2020Companding_IsCorrect() |
|||
{ |
|||
Vector4 input = new(.667F); |
|||
Vector4 e = Rec2020Companding.Expand(input); |
|||
Vector4 c = Rec2020Companding.Compress(e); |
|||
CompandingIsCorrectImpl(e, c, .44847462F, input); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Rec709Companding_IsCorrect() |
|||
{ |
|||
Vector4 input = new(.667F); |
|||
Vector4 e = Rec709Companding.Expand(input); |
|||
Vector4 c = Rec709Companding.Compress(e); |
|||
CompandingIsCorrectImpl(e, c, .4483577F, input); |
|||
} |
|||
|
|||
[Fact] |
|||
public void SRgbCompanding_IsCorrect() |
|||
{ |
|||
Vector4 input = new(.667F); |
|||
Vector4 e = SRgbCompanding.Expand(input); |
|||
Vector4 c = SRgbCompanding.Compress(e); |
|||
CompandingIsCorrectImpl(e, c, .40242353F, input); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(0)] |
|||
[InlineData(1)] |
|||
[InlineData(30)] |
|||
public void SRgbCompanding_Expand_VectorSpan(int length) |
|||
{ |
|||
Random rnd = new(42); |
|||
Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1); |
|||
Vector4[] expected = new Vector4[source.Length]; |
|||
|
|||
for (int i = 0; i < source.Length; i++) |
|||
{ |
|||
expected[i] = SRgbCompanding.Expand(source[i]); |
|||
} |
|||
|
|||
SRgbCompanding.Expand(source); |
|||
|
|||
Assert.Equal(expected, source, Comparer); |
|||
} |
|||
|
|||
[Theory] |
|||
[InlineData(0)] |
|||
[InlineData(1)] |
|||
[InlineData(30)] |
|||
public void SRgbCompanding_Compress_VectorSpan(int length) |
|||
{ |
|||
Random rnd = new(42); |
|||
Vector4[] source = rnd.GenerateRandomVectorArray(length, 0, 1); |
|||
Vector4[] expected = new Vector4[source.Length]; |
|||
|
|||
for (int i = 0; i < source.Length; i++) |
|||
{ |
|||
expected[i] = SRgbCompanding.Compress(source[i]); |
|||
} |
|||
|
|||
SRgbCompanding.Compress(source); |
|||
|
|||
Assert.Equal(expected, source, Comparer); |
|||
} |
|||
|
|||
[Fact] |
|||
public void GammaCompanding_IsCorrect() |
|||
{ |
|||
const double gamma = 2.2; |
|||
Vector4 input = new(.667F); |
|||
Vector4 e = GammaCompanding.Expand(input, gamma); |
|||
Vector4 c = GammaCompanding.Compress(e, gamma); |
|||
CompandingIsCorrectImpl(e, c, .41027668F, input); |
|||
} |
|||
|
|||
[Fact] |
|||
public void LCompanding_IsCorrect() |
|||
{ |
|||
Vector4 input = new(.667F); |
|||
Vector4 e = LCompanding.Expand(input); |
|||
Vector4 c = LCompanding.Compress(e); |
|||
CompandingIsCorrectImpl(e, c, .36236193F, input); |
|||
} |
|||
|
|||
private static void CompandingIsCorrectImpl(Vector4 e, Vector4 c, float expanded, Vector4 compressed) |
|||
{ |
|||
// W (alpha) is already the linear representation of the color.
|
|||
Assert.Equal(new Vector4(expanded, expanded, expanded, e.W), e, Comparer); |
|||
Assert.Equal(compressed, c, Comparer); |
|||
} |
|||
} |
|||
@ -0,0 +1,60 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Numerics; |
|||
using SixLabors.ImageSharp.ColorProfiles; |
|||
|
|||
namespace SixLabors.ImageSharp.Tests.ColorProfiles; |
|||
|
|||
public class StringRepresentationTests |
|||
{ |
|||
private static readonly Vector3 One = new(1); |
|||
private static readonly Vector3 Zero = new(0); |
|||
private static readonly Vector3 Random = new(42.4F, 94.5F, 83.4F); |
|||
|
|||
public static readonly TheoryData<object, string> TestData = new() |
|||
{ |
|||
{ new CieLab(Zero), "CieLab(0, 0, 0)" }, |
|||
{ new CieLch(Zero), "CieLch(0, 0, 0)" }, |
|||
{ new CieLchuv(Zero), "CieLchuv(0, 0, 0)" }, |
|||
{ new CieLuv(Zero), "CieLuv(0, 0, 0)" }, |
|||
{ new CieXyz(Zero), "CieXyz(0, 0, 0)" }, |
|||
{ new CieXyy(Zero), "CieXyy(0, 0, 0)" }, |
|||
{ new HunterLab(Zero), "HunterLab(0, 0, 0)" }, |
|||
{ new Lms(Zero), "Lms(0, 0, 0)" }, |
|||
{ new Rgb(Zero), "Rgb(0, 0, 0)" }, |
|||
{ new Hsl(Zero), "Hsl(0, 0, 0)" }, |
|||
{ new Hsv(Zero), "Hsv(0, 0, 0)" }, |
|||
{ new YCbCr(Zero), "YCbCr(0, 0, 0)" }, |
|||
{ new CieLab(One), "CieLab(1, 1, 1)" }, |
|||
{ new CieLch(One), "CieLch(1, 1, 1)" }, |
|||
{ new CieLchuv(One), "CieLchuv(1, 1, 1)" }, |
|||
{ new CieLuv(One), "CieLuv(1, 1, 1)" }, |
|||
{ new CieXyz(One), "CieXyz(1, 1, 1)" }, |
|||
{ new CieXyy(One), "CieXyy(1, 1, 1)" }, |
|||
{ new HunterLab(One), "HunterLab(1, 1, 1)" }, |
|||
{ new Lms(One), "Lms(1, 1, 1)" }, |
|||
{ new Rgb(One), "Rgb(1, 1, 1)" }, |
|||
{ new Hsl(One), "Hsl(1, 1, 1)" }, |
|||
{ new Hsv(One), "Hsv(1, 1, 1)" }, |
|||
{ new YCbCr(One), "YCbCr(1, 1, 1)" }, |
|||
{ new CieXyChromaticityCoordinates(1, 1), "CieXyChromaticityCoordinates(1, 1)" }, |
|||
{ new CieLab(Random), "CieLab(42.4, 94.5, 83.4)" }, |
|||
{ new CieLch(Random), "CieLch(42.4, 94.5, 83.4)" }, |
|||
{ new CieLchuv(Random), "CieLchuv(42.4, 94.5, 83.4)" }, |
|||
{ new CieLuv(Random), "CieLuv(42.4, 94.5, 83.4)" }, |
|||
{ new CieXyz(Random), "CieXyz(42.4, 94.5, 83.4)" }, |
|||
{ new CieXyy(Random), "CieXyy(42.4, 94.5, 83.4)" }, |
|||
{ new HunterLab(Random), "HunterLab(42.4, 94.5, 83.4)" }, |
|||
{ new Lms(Random), "Lms(42.4, 94.5, 83.4)" }, |
|||
{ new Rgb(Random), "Rgb(42.4, 94.5, 83.4)" }, |
|||
{ Rgb.Clamp(new Rgb(Random)), "Rgb(1, 1, 1)" }, |
|||
{ new Hsl(Random), "Hsl(42.4, 1, 1)" }, // clamping to 1 is expected
|
|||
{ new Hsv(Random), "Hsv(42.4, 1, 1)" }, // clamping to 1 is expected
|
|||
{ new YCbCr(Random), "YCbCr(42.4, 94.5, 83.4)" }, |
|||
}; |
|||
|
|||
[Theory] |
|||
[MemberData(nameof(TestData))] |
|||
public void StringRepresentationsAreCorrect(object color, string text) => Assert.Equal(text, color.ToString()); |
|||
} |
|||
Loading…
Reference in new issue