|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | +using SixLabors.ImageSharp.ColorProfiles; |
| 6 | +using SixLabors.ImageSharp.ColorProfiles.WorkingSpaces; |
| 7 | + |
| 8 | +namespace SixLabors.ImageSharp.Tests.ColorProfiles; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Tests the <see cref="RgbWorkingSpace"/> class. |
| 12 | +/// </summary> |
| 13 | +[Trait("Color", "Conversion")] |
| 14 | +public class RgbWorkingSpaceTests |
| 15 | +{ |
| 16 | + private static readonly ApproximateFloatComparer TolerantComparer = new(1e-5F); |
| 17 | + |
| 18 | + public static readonly TheoryData<RgbWorkingSpace> WorkingSpaces = new() |
| 19 | + { |
| 20 | + KnownRgbWorkingSpaces.SRgb, |
| 21 | + KnownRgbWorkingSpaces.SRgbSimplified, |
| 22 | + KnownRgbWorkingSpaces.Rec709, |
| 23 | + KnownRgbWorkingSpaces.Rec2020, |
| 24 | + KnownRgbWorkingSpaces.ECIRgbv2, |
| 25 | + KnownRgbWorkingSpaces.AdobeRgb1998, |
| 26 | + KnownRgbWorkingSpaces.ApplesRgb, |
| 27 | + KnownRgbWorkingSpaces.BestRgb, |
| 28 | + KnownRgbWorkingSpaces.BetaRgb, |
| 29 | + KnownRgbWorkingSpaces.BruceRgb, |
| 30 | + KnownRgbWorkingSpaces.CIERgb, |
| 31 | + KnownRgbWorkingSpaces.ColorMatchRgb, |
| 32 | + KnownRgbWorkingSpaces.DonRgb4, |
| 33 | + KnownRgbWorkingSpaces.EktaSpacePS5, |
| 34 | + KnownRgbWorkingSpaces.NTSCRgb, |
| 35 | + KnownRgbWorkingSpaces.PALSECAMRgb, |
| 36 | + KnownRgbWorkingSpaces.ProPhotoRgb, |
| 37 | + KnownRgbWorkingSpaces.SMPTECRgb, |
| 38 | + KnownRgbWorkingSpaces.WideGamutRgb |
| 39 | + }; |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void RgbWorkingSpaceEqualityRequiresSameConcreteType() |
| 43 | + { |
| 44 | + RgbWorkingSpace sRgb = KnownRgbWorkingSpaces.SRgb; |
| 45 | + RgbWorkingSpace sRgbSimplified = KnownRgbWorkingSpaces.SRgbSimplified; |
| 46 | + RgbWorkingSpace rec709 = KnownRgbWorkingSpaces.Rec709; |
| 47 | + |
| 48 | + Assert.False(sRgb.Equals(sRgbSimplified)); |
| 49 | + Assert.False(sRgbSimplified.Equals(sRgb)); |
| 50 | + Assert.False(sRgb.Equals(rec709)); |
| 51 | + Assert.False(rec709.Equals(sRgb)); |
| 52 | + |
| 53 | + Assert.NotEqual(sRgb.GetHashCode(), sRgbSimplified.GetHashCode()); |
| 54 | + Assert.NotEqual(sRgb.GetHashCode(), rec709.GetHashCode()); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void RgbWorkingSpaceEqualityMatchesSameConcreteTypeValues() |
| 59 | + { |
| 60 | + RgbWorkingSpace x = new SRgbWorkingSpace( |
| 61 | + KnownRgbWorkingSpaces.SRgb.WhitePoint, |
| 62 | + KnownRgbWorkingSpaces.SRgb.ChromaticityCoordinates); |
| 63 | + |
| 64 | + RgbWorkingSpace y = new SRgbWorkingSpace( |
| 65 | + KnownRgbWorkingSpaces.SRgb.WhitePoint, |
| 66 | + KnownRgbWorkingSpaces.SRgb.ChromaticityCoordinates); |
| 67 | + |
| 68 | + Assert.Equal(x, y); |
| 69 | + Assert.Equal(x.GetHashCode(), y.GetHashCode()); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void GammaWorkingSpaceEqualityIncludesGamma() |
| 74 | + { |
| 75 | + GammaWorkingSpace x = new( |
| 76 | + 2.2F, |
| 77 | + KnownRgbWorkingSpaces.SRgbSimplified.WhitePoint, |
| 78 | + KnownRgbWorkingSpaces.SRgbSimplified.ChromaticityCoordinates); |
| 79 | + |
| 80 | + GammaWorkingSpace y = new( |
| 81 | + 1.8F, |
| 82 | + KnownRgbWorkingSpaces.SRgbSimplified.WhitePoint, |
| 83 | + KnownRgbWorkingSpaces.SRgbSimplified.ChromaticityCoordinates); |
| 84 | + |
| 85 | + Assert.NotEqual(x, y); |
| 86 | + Assert.NotEqual(x.GetHashCode(), y.GetHashCode()); |
| 87 | + } |
| 88 | + |
| 89 | + [Theory] |
| 90 | + [MemberData(nameof(WorkingSpaces))] |
| 91 | + public void CompressAndExpand_RoundTripsWithTolerance(RgbWorkingSpace workingSpace) |
| 92 | + { |
| 93 | + Vector4[] linear = |
| 94 | + [ |
| 95 | + new(0F, .001F, .18F, 1F), // Endpoint, below the sRGB breakpoint, common middle gray, and opaque alpha. |
| 96 | + new(.0031308F, .25F, .5F, .75F), // sRGB linear/gamma breakpoint with interior values and partial alpha. |
| 97 | + new(.75F, .5F, .25F, .5F), // Reversed interior values ensure channel order does not hide errors. |
| 98 | + new(1F, .8F, .2F, .25F) // Upper endpoint, high/low interior values, and low alpha. |
| 99 | + ]; |
| 100 | + |
| 101 | + Vector4[] expectedCompressed = new Vector4[linear.Length]; |
| 102 | + |
| 103 | + for (int i = 0; i < linear.Length; i++) |
| 104 | + { |
| 105 | + Vector4 compressed = workingSpace.Compress(linear[i]); |
| 106 | + Vector4 expanded = workingSpace.Expand(compressed); |
| 107 | + |
| 108 | + expectedCompressed[i] = compressed; |
| 109 | + |
| 110 | + Assert.Equal(linear[i], expanded, TolerantComparer); |
| 111 | + } |
| 112 | + |
| 113 | + Vector4[] actualCompressed = linear.ToArray(); |
| 114 | + workingSpace.Compress(actualCompressed); |
| 115 | + |
| 116 | + Assert.Equal(expectedCompressed, actualCompressed, TolerantComparer); |
| 117 | + |
| 118 | + workingSpace.Expand(actualCompressed); |
| 119 | + |
| 120 | + Assert.Equal(linear, actualCompressed, TolerantComparer); |
| 121 | + } |
| 122 | +} |
0 commit comments