diff --git a/coloraide/average.py b/coloraide/average.py index 6494f6c24..c86aa218d 100644 --- a/coloraide/average.py +++ b/coloraide/average.py @@ -28,11 +28,10 @@ def average( cs = obj.CS_MAP[space] if cs.is_polar(): hue_index = cs.hue_index() # type: ignore[attr-defined] - has_radial = hasattr(cs, 'radial_index') - is_hwb = not has_radial and isinstance(cs, HWBish) + is_hwb = isinstance(cs, HWBish) else: hue_index = 1 - has_radial = is_hwb = False + is_hwb = False channels = cs.channels chan_count = len(channels) alpha_index = chan_count - 1 @@ -92,10 +91,10 @@ def average( # Create the color and if polar and there is no defined hue, force an achromatic state. color = obj.update(space, sums[:-1], sums[-1]) if cs.is_polar(): - if has_radial and math.isnan(color[hue_index]): - color[cs.radial_index()] = 0 # type: ignore[attr-defined] - elif is_hwb and math.isnan(color[hue_index]): + if is_hwb and math.isnan(color[hue_index]): w, b = cs.indexes()[1:] # type: ignore[attr-defined] if color[w] + color[b] < 1: color[w] = 1 - color[b] + elif math.isnan(color[hue_index]): + color[cs.radial_index()] = 0 # type: ignore[attr-defined] return color diff --git a/tests/test_average.py b/tests/test_average.py index a76a1bd25..747ace2b7 100644 --- a/tests/test_average.py +++ b/tests/test_average.py @@ -122,6 +122,29 @@ def test_average_ignore_undefined_alpha_premultiplied(self): colors = [Color('darkgreen'), Color('color(srgb 0 0.50196 0 / none)'), Color('color(srgb 0 0 1)')] self.assertEqual(Color.average(colors, space='srgb').to_string(color=True), 'color(srgb 0 0.29804 0.33333)') + def test_evenly_distributed(self): + """Test evenly distributed colors.""" + + colors = ['red', 'green', 'blue'] + self.assertEqual( + Color.average(colors, space='hsl').to_string(color=True, none=True), + 'color(--hsl none 0 0.41699)' + ) + + def test_hwb_handling(self): + """Test HWB handling.""" + + colors = ['red', 'green', 'blue'] + self.assertEqual( + Color.average(colors, space='hwb').to_string(color=True, none=True), + 'color(--hwb none 0.83399 0.16601)' + ) + + self.assertEqual( + Color.average(['orange', 'purple', 'darkgreen'], space='hwb').to_string(color=True), + 'color(--hwb 38.824 0 0.36863)' + ) + def test_all_undefined_hue(self): """Test all hues undefined."""