Skip to content

Commit 9a1e327

Browse files
test: replace swallowed color assertions (#142)
Replace the three protected tests with direct API and value checks. Co-authored-by: Peter Corke <peter.i.corke@gmail.com>
1 parent ca5ee04 commit 9a1e327

1 file changed

Lines changed: 38 additions & 44 deletions

File tree

‎tests/base/test_base_color.py‎

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -90,58 +90,58 @@ def test_spectrumfunctions(self):
9090

9191
self.assertAlmostEqual(np.max(r), 1.0, delta=1e-3)
9292
self.assertAlmostEqual(np.min(r), 0.0, delta=1e-3)
93-
93+
9494
def test_cmfrgb(self):
9595
"""Test RGB color matching functions"""
9696
# Single wavelength
9797
rgb = color.cmfrgb(550e-9)
9898
self.assertEqual(len(rgb), 3)
99-
99+
100100
# Multiple wavelengths
101101
λ = np.array([450e-9, 550e-9, 650e-9])
102102
rgb = color.cmfrgb(λ)
103103
self.assertEqual(rgb.shape[0], 3)
104104
self.assertEqual(rgb.shape[1], 3)
105-
105+
106106
# With intensity spectrum
107107
e = np.array([1.0, 1.5, 1.0])
108108
rgb_weighted = color.cmfrgb(λ, e)
109109
self.assertIsNotNone(rgb_weighted)
110-
110+
111111
def test_lambda2rg(self):
112112
"""Test wavelength to rg chromaticity conversion"""
113113
# Single wavelength
114114
rg = color.lambda2rg(550e-9)
115115
self.assertEqual(len(rg), 2)
116-
116+
117117
# Multiple wavelengths
118118
rg_multi = color.lambda2rg([450e-9, 550e-9, 650e-9])
119119
self.assertEqual(rg_multi.shape[1], 2)
120-
120+
121121
def test_lambda2xy(self):
122122
"""Test wavelength to xy chromaticity conversion"""
123123
# Single wavelength
124124
xy = color.lambda2xy(550e-9)
125125
self.assertEqual(len(xy), 2)
126-
126+
127127
# Multiple wavelengths
128128
xy_multi = color.lambda2xy([450e-9, 550e-9, 650e-9])
129129
self.assertEqual(xy_multi.shape[1], 2)
130-
130+
131131
def test_name2color(self):
132132
"""Test named color lookup"""
133133
# Basic colors
134134
red = color.name2color('red')
135135
self.assertEqual(len(red), 3)
136136
self.assertEqual(red[0], 1)
137-
137+
138138
blue = color.name2color('blue')
139139
self.assertEqual(blue[2], 1)
140-
140+
141141
# Short names
142142
r = color.name2color('r')
143143
self.assertEqual(r[0], 1)
144-
144+
145145
# Test different color spaces
146146
g_xy = color.name2color('g', 'xy')
147147
self.assertEqual(len(g_xy), 2)
@@ -160,15 +160,11 @@ def test_name2color_colororder(self):
160160
nt.assert_array_almost_equal(red_bgr, [0, 0, 1])
161161

162162
def test_colorname(self):
163-
"""Test color to name conversion"""
164-
# Test basic lookup
165-
try:
163+
"""Check the deprecated lookup without hiding assertion failures."""
164+
with self.assertWarns(DeprecationWarning):
166165
name = color.colorname([1, 0, 0])
167-
self.assertIsInstance(name, str)
168-
except:
169-
# Function might not be implemented
170-
pass
171-
166+
self.assertEqual(name, "red")
167+
172168
def test_tristim2cc(self):
173169
"""Test tristimulus to chromaticity coordinates"""
174170
# Simple white
@@ -177,45 +173,43 @@ def test_tristim2cc(self):
177173
# White point should be around [1/3, 1/3]
178174
self.assertAlmostEqual(cc[0], 1/3, delta=0.01)
179175
self.assertAlmostEqual(cc[1], 1/3, delta=0.01)
180-
176+
181177
# Pure red
182178
red = np.array([1, 0, 0])
183179
cc_red = color.tristim2cc(red)
184180
self.assertEqual(cc_red[0], 1.0)
185181
self.assertEqual(cc_red[1], 0.0)
186-
182+
187183
# Image input
188184
im = np.ones((5, 5, 3))
189185
cc_im = color.tristim2cc(im)
190186
self.assertEqual(cc_im.shape, (5, 5, 2))
191-
187+
192188
def test_ccxyz(self):
193-
"""Test CIE XYZ color matching function"""
194-
try:
195-
xyz = color.ccxyz(color='r')
196-
self.assertEqual(len(xyz), 3)
197-
198-
xyz_green = color.ccxyz(color='g')
199-
self.assertEqual(len(xyz_green), 3)
200-
except:
201-
# Function might not be available
202-
pass
203-
189+
"""Check spectral chromaticities at two tabulated wavelengths."""
190+
wavelengths = np.array([500e-9, 600e-9])
191+
xyz = color.ccxyz(wavelengths)
192+
expected = np.array(
193+
[
194+
[0.008168028, 0.538423071, 0.453408901],
195+
[0.627036600, 0.372491145, 0.000472255],
196+
]
197+
)
198+
self.assertEqual(xyz.shape, (2, 3))
199+
nt.assert_allclose(xyz, expected, atol=1e-6, rtol=0)
200+
nt.assert_allclose(xyz.sum(axis=1), 1.0, atol=1e-12, rtol=0)
201+
nt.assert_allclose(color.ccxyz(500e-9), expected[:1], atol=1e-6, rtol=0)
202+
204203
def test_color_conversion_functions(self):
205-
"""Test various color space conversion functions"""
206-
# Test RGB to HSV and back
207-
try:
208-
rgb = np.array([0.5, 0.3, 0.8])
209-
# Many color conversion functions exist in OpenCV
210-
# Just verify basic functionality exists
211-
self.assertIsNotNone(rgb)
212-
except:
213-
pass
204+
"""Convert RGB to known HSV values and recover the input."""
205+
rgb = np.array([[[0.5, 0.3, 0.8]]], dtype=np.float32)
206+
hsv = color.colorspace_convert(rgb, "rgb", "hsv")
207+
nt.assert_allclose(hsv, [[[264.0, 0.625, 0.8]]], atol=1e-4, rtol=0)
208+
restored = color.colorspace_convert(hsv, "hsv", "rgb")
209+
nt.assert_allclose(restored, rgb, atol=1e-6, rtol=0)
214210

215211

216212
# ---------------------------------------------------------------------------#
217213
if __name__ == '__main__':
218214

219215
unittest.main()
220-
221-

0 commit comments

Comments
 (0)