Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
facelessuser committed Oct 6, 2023
1 parent 886e9bf commit eabaebc
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 4 deletions.
11 changes: 7 additions & 4 deletions coloraide/interpolate/css_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ def normalize_hue(
c1 = color1[index]
c2 = color2[index]

if math.isnan(c1):
is_nan1 = math.isnan(c1)
is_nan2 = math.isnan(c2)

if is_nan1 and is_nan2:
return
elif is_nan1:
c1 = c2
if math.isnan(c2):
elif is_nan2:
c2 = c1
if math.isnan(c1):
return

if hue == "specified":
return
Expand Down
224 changes: 224 additions & 0 deletions tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,118 @@ def test_mix_hue_nan(self):
Color("hsl(120, 25%, 57.5%)")
)

def test_css_hue_shorter_cases(self):
"""Cover shorter hue cases."""

# c2 - c1 > 180
c1 = Color('lch(75% 50 40)')
c2 = Color('lch(30% 30 350)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="shorter", space="lch", method='css-linear'),
Color("lch(75% 50 375)")
)

# c2 - c1 < -180
c1 = Color('lch(30% 30 350)')
c2 = Color('lch(75% 50 40)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="shorter", space="lch", method='css-linear'),
Color("lch(30% 30 375)")
)

def test_css_mix_hue_adjust(self):
"""Test hue adjusting."""

c1 = Color('rebeccapurple')
c2 = Color('lch(85% 100 805)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="shorter", space="lch", method='css-linear'),
Color("lch(32.393 61.244 342.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="longer", space="lch", method='css-linear'),
Color("lch(32.393 61.244 252.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="increasing", space="lch", method='css-linear'),
Color("lch(32.393 61.244 342.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="decreasing", space="lch", method='css-linear'),
Color("lch(32.393 61.244 252.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="specified", space="lch", method='css-linear'),
Color("lch(32.393 61.244 432.89)")
)

def test_css_hue_longer_cases(self):
"""Cover longer hue cases."""

# 0 < (c2 - c1) < 180
c1 = Color('lch(75% 50 40)')
c2 = Color('lch(30% 30 60)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="longer", space="lch", method='css-linear'),
Color("lch(75% 50 230)")
)

# -180 < (c2 - c1) < 0
c1 = Color('lch(30% 30 60)')
c2 = Color('lch(75% 50 40)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="longer", space="lch", method='css-linear'),
Color("lch(30% 30 230)")
)

def test_css_hue_increasing_cases(self):
"""Cover increasing hue cases."""

# c2 < c1
c1 = Color('lch(75% 50 60)')
c2 = Color('lch(30% 30 40)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="increasing", space="lch", method='css-linear'),
Color("lch(75% 50 230)")
)

def test_css_hue_decreasing_cases(self):
"""Cover decreasing hue cases."""

# c1 < c2
c1 = Color('lch(75% 50 40)')
c2 = Color('lch(30% 30 60)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="decreasing", space="lch", method='css-linear'),
Color("lch(75% 50 230)")
)

def test_css_mix_hue_adjust_bad(self):
"""Test hue adjusting."""

c1 = Color('rebeccapurple')
c2 = Color('lch(85% 100 805)')
with self.assertRaises(ValueError):
c1.mix(c2.mask("hue", invert=True), 0.25, hue="bad", space="lch", method='css-linear')

def test_css_mix_hue_nan(self):
"""Test mix hue with `NaN`."""

self.assertColorEqual(
Color('hsl', [NaN, 0, 0.25]).mix(Color('hsl', [NaN, 0, 0.9]), 0.50, space="hsl", method='css-linear'),
Color("hsl(0, 0%, 57.5%)")
)

self.assertColorEqual(
Color('hsl', [NaN, 0, 0.25]).mix(Color('hsl', [120, 0.5, 0.9]), 0.50, space="hsl", method='css-linear'),
Color("hsl(120, 25%, 57.5%)")
)

self.assertColorEqual(
Color('hsl', [120, 0.5, 0.25]).mix(Color('hsl', [NaN, 0, 0.9]), 0.50, space="hsl", method='css-linear'),
Color("hsl(120, 25%, 57.5%)")
)

def test_mix_progress(self):
"""Test custom progress."""

Expand Down Expand Up @@ -1164,6 +1276,118 @@ def test_too_few_colors_linear(self):
with self.assertRaises(ValueError):
Color.interpolate(['green', lambda t: t * 3])

def test_continuos_hue_shorter_cases(self):
"""Cover shorter hue cases."""

# c2 - c1 > 180
c1 = Color('lch(75% 50 40)')
c2 = Color('lch(30% 30 350)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="shorter", space="lch", method='continuous'),
Color("lch(75% 50 15)")
)

# c2 - c1 < -180
c1 = Color('lch(30% 30 350)')
c2 = Color('lch(75% 50 40)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="shorter", space="lch", method='continuous'),
Color("lch(30% 30 375)")
)

def test_continuos_mix_hue_adjust(self):
"""Test hue adjusting."""

c1 = Color('rebeccapurple')
c2 = Color('lch(85% 100 805)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="shorter", space="lch", method='continuous'),
Color("lch(32.393 61.244 342.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="longer", space="lch", method='continuous'),
Color("lch(32.393 61.244 252.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="increasing", space="lch", method='continuous'),
Color("lch(32.393 61.244 342.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="decreasing", space="lch", method='continuous'),
Color("lch(32.393 61.244 252.89)")
)
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.25, hue="specified", space="lch", method='continuous'),
Color("lch(32.393 61.244 432.89)")
)

def test_continuos_hue_longer_cases(self):
"""Cover longer hue cases."""

# 0 < (c2 - c1) < 180
c1 = Color('lch(75% 50 40)')
c2 = Color('lch(30% 30 60)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="longer", space="lch", method='continuous'),
Color("lch(75% 50 -130)")
)

# -180 < (c2 - c1) < 0
c1 = Color('lch(30% 30 60)')
c2 = Color('lch(75% 50 40)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="longer", space="lch", method='continuous'),
Color("lch(30% 30 230)")
)

def test_continuos_hue_increasing_cases(self):
"""Cover increasing hue cases."""

# c2 < c1
c1 = Color('lch(75% 50 60)')
c2 = Color('lch(30% 30 40)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="increasing", space="lch", method='continuous'),
Color("lch(75% 50 230)")
)

def test_continuos_hue_decreasing_cases(self):
"""Cover decreasing hue cases."""

# c1 < c2
c1 = Color('lch(75% 50 40)')
c2 = Color('lch(30% 30 60)')
self.assertColorEqual(
c1.mix(c2.mask("hue", invert=True), 0.50, hue="decreasing", space="lch", method='continuous'),
Color("lch(75% 50 -130)")
)

def test_continuos_mix_hue_adjust_bad(self):
"""Test hue adjusting."""

c1 = Color('rebeccapurple')
c2 = Color('lch(85% 100 805)')
with self.assertRaises(ValueError):
c1.mix(c2.mask("hue", invert=True), 0.25, hue="bad", space="lch", method='continuous')

def test_continuos_mix_hue_nan(self):
"""Test mix hue with `NaN`."""

self.assertColorEqual(
Color('hsl', [NaN, 0, 0.25]).mix(Color('hsl', [NaN, 0, 0.9]), 0.50, space="hsl", method='continuous'),
Color("hsl(0, 0%, 57.5%)")
)

self.assertColorEqual(
Color('hsl', [NaN, 0, 0.25]).mix(Color('hsl', [120, 0.5, 0.9]), 0.50, space="hsl", method='continuous'),
Color("hsl(120, 25%, 57.5%)")
)

self.assertColorEqual(
Color('hsl', [120, 0.5, 0.25]).mix(Color('hsl', [NaN, 0, 0.9]), 0.50, space="hsl", method='continuous'),
Color("hsl(120, 25%, 57.5%)")
)

def test_continuous_undefined_middle(self):
"""Test continuous with undefined middle."""

Expand Down

0 comments on commit eabaebc

Please sign in to comment.