Skip to content

Commit

Permalink
Add tests to cover new handling and fix related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Dec 4, 2024
1 parent ea4355e commit e229776
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 5 additions & 6 deletions coloraide/average.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
23 changes: 23 additions & 0 deletions tests/test_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down

0 comments on commit e229776

Please sign in to comment.