1
2
3
4
5 package moreland
6
7 import (
8 "image/color"
9 "testing"
10
11 "gonum.org/v1/gonum/floats/scalar"
12 )
13
14
15
16
17 func TestRgb_sRGBA(t *testing.T) {
18 testCases := []struct {
19 l rgb
20 s sRGBA
21 }{
22 {
23 rgb{R: 0.015299702, G: 0.015299702, B: 0.015299702},
24 sRGBA{R: 0.1298716701086684, G: 0.1298716701086684, B: 0.1298716701086684},
25 },
26 }
27 for i, tc := range testCases {
28 result := tc.l.sRGBA(0)
29 if !sRGBAEqualWithin(result, tc.s, 1.0e-14) {
30 t.Errorf("case %d: have %+v, want %+v", i, result, tc.s)
31 }
32 }
33 }
34
35 func sRGBAEqualWithin(a, b sRGBA, tol float64) bool {
36 return scalar.EqualWithinAbsOrRel(a.R, b.R, tol, tol) &&
37 scalar.EqualWithinAbsOrRel(a.G, b.G, tol, tol) &&
38 scalar.EqualWithinAbsOrRel(a.B, b.B, tol, tol) &&
39 scalar.EqualWithinAbsOrRel(a.A, b.A, tol, tol)
40 }
41
42
43
44
45 func TestSRGBA_rgb(t *testing.T) {
46 testCases := []struct {
47 s sRGBA
48 l rgb
49 }{
50 {
51 sRGBA{R: 0.735356983, G: 0.735356983, B: 0.735356983},
52 rgb{R: 0.499999999920366, G: 0.499999999920366, B: 0.499999999920366},
53 },
54 {
55 sRGBA{R: 0.01292, G: 0.01292, B: 0.01292},
56 rgb{R: 0.001, G: 0.001, B: 0.001},
57 },
58 {
59 sRGBA{R: 0.759704028, G: 0.162897038, B: 0.206033415},
60 rgb{R: 0.5377665307661512, G: 0.022698506403451876, B: 0.035015856125996676},
61 },
62 }
63 for i, tc := range testCases {
64 result := tc.s.rgb()
65 if result != tc.l {
66 t.Errorf("case %d: have %+v, want %+v", i, result, tc.l)
67 }
68 }
69 }
70
71
72
73
74
75 func TestCieXYZ_rgb(t *testing.T) {
76 xyz := cieXYZ{X: 0.128392403, Y: 0.128221351, Z: 0.408477452}
77 result := xyz.rgb()
78 want := rgb{R: 0.015299702837399953, G: 0.1330700251971, B: 0.4127549680071}
79 if result != want {
80 t.Errorf("have %+v, want %+v", result, want)
81 }
82
83 lrgb := rgb{R: 0.28909265477940005, G: 0.0663313933285, B: 0.0500602839142}
84 xyz = cieXYZ{X: 0.151975056, Y: 0.112509738, Z: 0.061066471}
85 if xyz.rgb() != lrgb {
86 t.Errorf("rgb: have %+v, want %+v", xyz.rgb(), lrgb)
87 }
88 xyz = cieXYZ{X: 0.1519777983318093, Y: 0.11251566341324888, Z: 0.061068490182446714}
89 if lrgb.cieXYZ() != xyz {
90 t.Errorf("xyz: have %+v, want %+v", lrgb.cieXYZ(), xyz)
91 }
92 }
93
94
95
96
97 func TestCieLAB_cieXYZ(t *testing.T) {
98 lab := cieLAB{L: 42.49401592, A: 4.416911613, B: -43.38526532}
99 result := lab.cieXYZ()
100 want := cieXYZ{X: 0.12838835051807143, Y: 0.12822135121812256, Z: 0.40841368569543157}
101 if result != want {
102 t.Errorf("have %+v, want %+v", result, want)
103 }
104 }
105
106
107
108
109 func TestMsh_cieLAB(t *testing.T) {
110 c := msh{M: 80, S: 1.08, H: -1.1}
111 result := c.cieLAB()
112 want := cieLAB{L: 37.7062691338992, A: 32.004211237121645, B: -62.88058310076059}
113 if result != want {
114 t.Errorf("have %+v, want %+v", result, want)
115 }
116 }
117
118
119
120
121 func TestCieLAB_sRGBA(t *testing.T) {
122 testCases := []struct {
123 l cieLAB
124 s sRGBA
125 }{
126 {
127 cieLAB{},
128 sRGBA{},
129 },
130 {
131 cieLAB{L: 43.22418447, A: 59.07682101, B: 32.27381441},
132 sRGBA{R: 0.7596910553350515, G: 0.16292472671190056, B: 0.20600836034382436},
133 },
134 }
135 for i, tc := range testCases {
136 result := tc.l.sRGBA(0)
137 if result != tc.s {
138 t.Errorf("case %d: have %+v, want %+v", i, result, tc.s)
139 }
140 }
141 }
142
143 func TestHueTwist(t *testing.T) {
144
145
146 if hueTwist(msh{M: 80, S: 1.08, H: -1.1}, 88) != -0.5611585624524025 {
147 t.Errorf("hueTwist(80, 1.08, -1.1), 88 should equal -0.561158562 but equals %g",
148 hueTwist(msh{M: 80, S: 1.08, H: -1.1}, 88))
149 }
150 }
151
152
153
154
155 func TestCieXYZ_cieLAB(t *testing.T) {
156 xyz := cieXYZ{X: 0.151975056, Y: 0.112509738, Z: 0.061066471}
157 lab := cieLAB{L: 40.00000000055783, A: 30.000000104296763, B: 19.99999996294335}
158 if xyz.cieLAB() != lab {
159 t.Errorf("lab: have %+v, want %+v", xyz.cieLAB(), lab)
160 }
161 xyz = cieXYZ{X: 0.15197025931227778, Y: 0.11250973800000005, Z: 0.06105693812573921}
162 if lab.cieXYZ() != xyz {
163 t.Errorf("xyz: have %+v, want %+v", lab.cieXYZ(), xyz)
164 }
165 }
166
167 func TestColorToRGB(t *testing.T) {
168 c := color.NRGBA{R: 194, G: 42, B: 53, A: 100}
169 rgb := sRGBA{R: 0.7607782101167315, G: 0.1646692607003891, B: 0.20782101167315176, A: 0.39215686274509803}
170 if colorTosRGBA(c) != rgb {
171 t.Errorf("rgb: have %+v, want %+v", colorTosRGBA(c), rgb)
172 }
173 }
174
175
176
177
178 func TestCieLAB_msh(t *testing.T) {
179 lab := cieLAB{L: 43.22418447, A: 59.07682101, B: 32.27381441}
180 mshVal := msh{M: 80.00000000197056, S: 1.0000000000076632, H: 0.5000000000023601}
181 if lab.MSH() != mshVal {
182 t.Errorf("msh: have %+v, want %+v", lab.MSH(), mshVal)
183 }
184 }
185
186 func TestColorToMSH(t *testing.T) {
187 c := color.NRGBA{B: 255, A: 255}
188 result := colorToMSH(c)
189
190
191 want := msh{M: 137.64998152940237, S: 1.333915268336423, H: -0.9374394027523394}
192 if result != want {
193 t.Errorf("want %+v but have %+v", want, result)
194 }
195 }
196
View as plain text