-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCoordinateSpaceTests.cs
242 lines (210 loc) · 10.3 KB
/
CoordinateSpaceTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
using NUnit.Framework;
using Wacton.Unicolour.Tests.Utils;
namespace Wacton.Unicolour.Tests;
/*
* Conversions that are just translations to/from cylindrical coordinate spaces
* should have input values clamped to the range of the source coordinate space
* otherwise they will be mapped to incorrect values
* e.g. RGB with negative -> HSB should produce the same value as RGB with zero -> HSB
*/
public class CoordinateSpaceTests
{
private const double HueUpperOutRange = 360.00001;
private const double HueUpperInRange = 0.00001;
private const double HueLowerOutRange = -0.00001;
private const double HueLowerInRange = 359.99999;
private readonly ColourTriplet hsxUpperOutRange = new(HueUpperOutRange, 100, double.PositiveInfinity);
private readonly ColourTriplet hsxUpperInRange = new(HueUpperInRange, 1, 1);
private readonly ColourTriplet hsxLowerOutRange = new(HueLowerOutRange, -100, double.NegativeInfinity);
private readonly ColourTriplet hsxLowerInRange = new(HueLowerInRange, 0, 0);
private readonly ColourTriplet rgbUpperOutRange = new(1.00001, 100, double.PositiveInfinity);
private readonly ColourTriplet rgbUpperInRange = new(1, 1, 1);
private readonly ColourTriplet rgbLowerOutRange = new(-0.00001, -100, double.NegativeInfinity);
private readonly ColourTriplet rgbLowerInRange = new(0, 0, 0);
[Test]
public void RectangularRgbToCylindricalHsb()
{
AssertRgbToHsb(rgbUpperInRange, rgbUpperOutRange);
AssertRgbToHsb(rgbLowerInRange, rgbLowerOutRange);
}
[Test]
public void CylindricalHsbToRectangularRgb()
{
AssertHsbToRgb(hsxUpperInRange, hsxUpperOutRange);
AssertHsbToRgb(hsxLowerInRange, hsxLowerOutRange);
}
[Test]
public void CylindricalHsbToCylindricalHsl()
{
AssertHsbToHsl(hsxUpperInRange, hsxUpperOutRange);
AssertHsbToHsl(hsxLowerInRange, hsxLowerOutRange);
}
[Test]
public void CylindricalHslToCylindricalHsb()
{
AssertHslToHsb(hsxUpperInRange, hsxUpperOutRange);
AssertHslToHsb(hsxLowerInRange, hsxLowerOutRange);
}
[Test]
public void CylindricalHsbToCylindricalHwb()
{
AssertHsbToHwb(hsxUpperInRange, hsxUpperOutRange);
AssertHsbToHwb(hsxLowerInRange, hsxLowerOutRange);
}
[Test]
public void CylindricalHwbToCylindricalHsb()
{
AssertHwbToHsb(hsxUpperInRange, hsxUpperOutRange);
AssertHwbToHsb(hsxLowerInRange, hsxLowerOutRange);
}
[Test] // NOTE: HSI is not bound to RGB gamut like other cylindrical forms of RGB, so can only assert hue
public void CylindricalHsiRectangularRgb()
{
AssertHsiToRgb(HueUpperInRange, HueUpperOutRange);
AssertHsiToRgb(HueLowerInRange, HueLowerOutRange);
}
[Test]
public void CylindricalLchabRectangularLab()
{
AssertLchabToLab(HueUpperInRange, HueUpperOutRange);
AssertLchabToLab(HueLowerInRange, HueLowerOutRange);
}
[Test]
public void CylindricalLchuvRectangularLuv()
{
AssertLchuvToLuv(HueUpperInRange, HueUpperOutRange);
AssertLchuvToLuv(HueLowerInRange, HueLowerOutRange);
}
[Test] // NOTE: TSL is not bound to RGB gamut like other cylindrical forms of RGB, so can only assert hue
public void CylindricalTslRectangularRgb()
{
AssertHsiToRgb(HueUpperInRange, HueUpperOutRange);
AssertHsiToRgb(HueLowerInRange, HueLowerOutRange);
}
[Test]
public void CylindricalJzczhzRectangularJzazbz()
{
AssertJzczhzToJzazbz(HueUpperInRange, HueUpperOutRange);
AssertJzczhzToJzazbz(HueLowerInRange, HueLowerOutRange);
}
[Test]
public void CylindricalOklchRectangularOklab()
{
AssertOklchToOklab(HueUpperInRange, HueUpperOutRange);
AssertOklchToOklab(HueLowerInRange, HueLowerOutRange);
}
private static void AssertRgbToHsb(ColourTriplet inRange, ColourTriplet outRange)
{
var rgbInRange = new Rgb(inRange.First, inRange.Second, inRange.Third);
var rgbOutRange = new Rgb(outRange.First, outRange.Second, outRange.Third);
var hsbFromInRange = Hsb.FromRgb(rgbInRange);
var hsbFromOutRange = Hsb.FromRgb(rgbOutRange);
AssertSourceTriplets(AsTriplets(rgbInRange), AsTriplets(rgbOutRange));
AssertDestinationTriplets(AsTriplets(hsbFromInRange), AsTriplets(hsbFromOutRange));
}
private static void AssertHsiToRgb(double hueInRange, double hueOutRange)
{
var hsiInRange = new Hsi(hueInRange, 1, 1 / 3.0);
var hsiOutRange = new Hsi(hueOutRange, 1, 1 / 3.0);
var rgbFromInRange = Hsi.ToRgb(hsiInRange);
var rgbFromOutRange = Hsi.ToRgb(hsiOutRange);
AssertSourceTriplets(AsTriplets(hsiInRange), AsTriplets(hsiOutRange));
AssertTriplet(rgbFromOutRange.Triplet, rgbFromInRange.Triplet);
}
private static void AssertHsbToRgb(ColourTriplet inRange, ColourTriplet outRange)
{
var hsbInRange = new Hsb(inRange.First, inRange.Second, inRange.Third);
var hsbOutRange = new Hsb(outRange.First, outRange.Second, outRange.Third);
var rgbFromInRange = Hsb.ToRgb(hsbInRange);
var rgbFromOutRange = Hsb.ToRgb(hsbOutRange);
AssertSourceTriplets(AsTriplets(hsbInRange), AsTriplets(hsbOutRange));
AssertDestinationTriplets(AsTriplets(rgbFromInRange), AsTriplets(rgbFromOutRange));
}
private static void AssertHsbToHsl(ColourTriplet inRange, ColourTriplet outRange)
{
var hsbInRange = new Hsb(inRange.First, inRange.Second, inRange.Third);
var hsbOutRange = new Hsb(outRange.First, outRange.Second, outRange.Third);
var hslFromInRange = Hsl.FromHsb(hsbInRange);
var hslFromOutRange = Hsl.FromHsb(hsbOutRange);
AssertSourceTriplets(AsTriplets(hsbInRange), AsTriplets(hsbOutRange));
AssertDestinationTriplets(AsTriplets(hslFromInRange), AsTriplets(hslFromOutRange));
}
private static void AssertHslToHsb(ColourTriplet inRange, ColourTriplet outRange)
{
var hslInRange = new Hsl(inRange.First, inRange.Second, inRange.Third);
var hslOutRange = new Hsl(outRange.First, outRange.Second, outRange.Third);
var hsbFromInRange = Hsl.ToHsb(hslInRange);
var hsbFromOutRange = Hsl.ToHsb(hslOutRange);
AssertSourceTriplets(AsTriplets(hslInRange), AsTriplets(hslOutRange));
AssertDestinationTriplets(AsTriplets(hsbFromInRange), AsTriplets(hsbFromOutRange));
}
private static void AssertHsbToHwb(ColourTriplet inRange, ColourTriplet outRange)
{
var hsbInRange = new Hsb(inRange.First, inRange.Second, inRange.Third);
var hsbOutRange = new Hsb(outRange.First, outRange.Second, outRange.Third);
var hwbFromInRange = Hwb.FromHsb(hsbInRange);
var hwbFromOutRange = Hwb.FromHsb(hsbOutRange);
AssertSourceTriplets(AsTriplets(hsbInRange), AsTriplets(hsbOutRange));
AssertDestinationTriplets(AsTriplets(hwbFromInRange), AsTriplets(hwbFromOutRange));
}
private static void AssertHwbToHsb(ColourTriplet inRange, ColourTriplet outRange)
{
var hwbInRange = new Hwb(inRange.First, inRange.Second, inRange.Third);
var hwbOutRange = new Hwb(outRange.First, outRange.Second, outRange.Third);
var hsbFromInRange = Hwb.ToHsb(hwbInRange);
var hsbFromOutRange = Hwb.ToHsb(hwbOutRange);
AssertSourceTriplets(AsTriplets(hwbInRange), AsTriplets(hwbOutRange));
AssertDestinationTriplets(AsTriplets(hsbFromInRange), AsTriplets(hsbFromOutRange));
}
private static void AssertLchabToLab(double hueInRange, double hueOutRange)
{
var lchabInRange = new Lchab(50, 100, hueInRange);
var lchabOutRange = new Lchab(50, 100, hueOutRange);
var labFromInRange = Lchab.ToLab(lchabInRange);
var labFromOutRange = Lchab.ToLab(lchabOutRange);
AssertSourceTriplets(AsTriplets(lchabInRange), AsTriplets(lchabOutRange));
AssertTriplet(labFromOutRange.Triplet, labFromInRange.Triplet);
}
private static void AssertLchuvToLuv(double hueInRange, double hueOutRange)
{
var lchuvInRange = new Lchuv(50, 100, hueInRange);
var lchuvOutRange = new Lchuv(50, 100, hueOutRange);
var luvFromInRange = Lchuv.ToLuv(lchuvInRange);
var luvFromOutRange = Lchuv.ToLuv(lchuvOutRange);
AssertSourceTriplets(AsTriplets(lchuvInRange), AsTriplets(lchuvOutRange));
AssertTriplet(luvFromOutRange.Triplet, luvFromInRange.Triplet);
}
private static void AssertJzczhzToJzazbz(double hueInRange, double hueOutRange)
{
var jzczhzInRange = new Jzczhz(0.5, 0.25, hueInRange);
var jzczhzOutRange = new Jzczhz(0.5, 0.25, hueOutRange);
var jzazbzFromInRange = Jzczhz.ToJzazbz(jzczhzInRange);
var jzazbzFromOutRange = Jzczhz.ToJzazbz(jzczhzOutRange);
AssertSourceTriplets(AsTriplets(jzczhzInRange), AsTriplets(jzczhzOutRange));
AssertTriplet(jzazbzFromOutRange.Triplet, jzazbzFromInRange.Triplet);
}
private static void AssertOklchToOklab(double hueInRange, double hueOutRange)
{
var oklchInRange = new Oklch(0.5, 1, hueInRange);
var oklchOutRange = new Oklch(0.5, 1, hueOutRange);
var oklabFromInRange = Oklch.ToOklab(oklchInRange);
var oklabFromOutRange = Oklch.ToOklab(oklchOutRange);
AssertSourceTriplets(AsTriplets(oklchInRange), AsTriplets(oklchOutRange));
AssertTriplet(oklabFromOutRange.Triplet, oklabFromInRange.Triplet);
}
private static void AssertSourceTriplets(Triplets sourceInRange, Triplets sourceOutRange)
{
AssertTriplet(sourceOutRange.Constrained, sourceInRange.Unconstrained);
}
private static void AssertDestinationTriplets(Triplets destinationFromInRange, Triplets destinationFromOutRange)
{
AssertTriplet(destinationFromOutRange.Unconstrained, destinationFromInRange.Unconstrained);
AssertTriplet(destinationFromOutRange.Constrained, destinationFromInRange.Unconstrained);
}
private static void AssertTriplet(ColourTriplet actual, ColourTriplet expected)
{
TestUtils.AssertTriplet(actual, expected, 0.00001);
}
private static Triplets AsTriplets(ColourRepresentation representation) => new(representation.Triplet, representation.ConstrainedTriplet);
private record Triplets(ColourTriplet Unconstrained, ColourTriplet Constrained);
}