mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
color tests to 100% (#2078)
* color tests to 100% * tweak * remove overly complex code
This commit is contained in:
@@ -297,7 +297,7 @@ class Color(NamedTuple):
|
||||
A CSS hex-style color, e.g. "#46b3de"
|
||||
|
||||
"""
|
||||
r, g, b, a = self.clamped
|
||||
r, g, b, _a = self.clamped
|
||||
return f"#{r:02X}{g:02X}{b:02X}"
|
||||
|
||||
@property
|
||||
@@ -504,7 +504,9 @@ class Color(NamedTuple):
|
||||
a = clamp(float(a), 0.0, 1.0)
|
||||
color = Color.from_hsl(h, s, l).with_alpha(a)
|
||||
else:
|
||||
raise AssertionError("Can't get here if RE_COLOR matches")
|
||||
raise AssertionError( # pragma: no-cover
|
||||
"Can't get here if RE_COLOR matches"
|
||||
)
|
||||
return color
|
||||
|
||||
@lru_cache(maxsize=1024)
|
||||
@@ -545,10 +547,7 @@ class Color(NamedTuple):
|
||||
Returns:
|
||||
A new color, either an off-white or off-black
|
||||
"""
|
||||
brightness = self.brightness
|
||||
white_contrast = abs(brightness - WHITE.brightness)
|
||||
black_contrast = abs(brightness - BLACK.brightness)
|
||||
return (WHITE if white_contrast > black_contrast else BLACK).with_alpha(alpha)
|
||||
return (WHITE if self.brightness < 0.5 else BLACK).with_alpha(alpha)
|
||||
|
||||
|
||||
class Gradient:
|
||||
@@ -566,12 +565,12 @@ class Gradient:
|
||||
Raises:
|
||||
ValueError: If any stops are missing (must be at least a stop for 0 and 1).
|
||||
"""
|
||||
self.stops = sorted(stops)
|
||||
self._stops = sorted(stops)
|
||||
if len(stops) < 2:
|
||||
raise ValueError("At least 2 stops required.")
|
||||
if self.stops[0][0] != 0.0:
|
||||
if self._stops[0][0] != 0.0:
|
||||
raise ValueError("First stop must be 0.")
|
||||
if self.stops[-1][0] != 1.0:
|
||||
if self._stops[-1][0] != 1.0:
|
||||
raise ValueError("Last stop must be 1.")
|
||||
|
||||
def get_color(self, position: float) -> Color:
|
||||
@@ -588,13 +587,13 @@ class Gradient:
|
||||
"""
|
||||
# TODO: consider caching
|
||||
position = clamp(position, 0.0, 1.0)
|
||||
for (stop1, color1), (stop2, color2) in zip(self.stops, self.stops[1:]):
|
||||
for (stop1, color1), (stop2, color2) in zip(self._stops, self._stops[1:]):
|
||||
if stop2 >= position >= stop1:
|
||||
return color1.blend(
|
||||
color2,
|
||||
(position - stop1) / (stop2 - stop1),
|
||||
)
|
||||
return self.stops[-1][1]
|
||||
raise AssertionError("Can't get here if `_stops` is valid")
|
||||
|
||||
|
||||
# Color constants
|
||||
|
||||
@@ -42,7 +42,7 @@ def test_rgb():
|
||||
assert Color(10, 20, 30, 0.55).rgb == (10, 20, 30)
|
||||
|
||||
|
||||
def test_hls():
|
||||
def test_hsl():
|
||||
red = Color(200, 20, 32)
|
||||
print(red.hsl)
|
||||
assert red.hsl == pytest.approx(
|
||||
@@ -51,6 +51,7 @@ def test_hls():
|
||||
assert Color.from_hsl(
|
||||
0.9888888888888889, 0.818181818181818, 0.43137254901960786
|
||||
).normalized == pytest.approx(red.normalized, rel=1e-5)
|
||||
assert red.hsl.css == "hsl(356,81.8%,43.1%)"
|
||||
|
||||
|
||||
def test_color_brightness():
|
||||
@@ -65,6 +66,12 @@ def test_color_hex():
|
||||
assert Color(255, 0, 127, 0.5).hex == "#FF007F7F"
|
||||
|
||||
|
||||
def test_color_hex6():
|
||||
assert Color(0, 0, 0).hex6 == "#000000"
|
||||
assert Color(255, 255, 255, 0.25).hex6 == "#FFFFFF"
|
||||
assert Color(255, 0, 127, 0.5).hex6 == "#FF007F"
|
||||
|
||||
|
||||
def test_color_css():
|
||||
assert Color(255, 0, 127).css == "rgb(255,0,127)"
|
||||
assert Color(255, 0, 127, 0.5).css == "rgba(255,0,127,0.5)"
|
||||
@@ -74,6 +81,11 @@ def test_color_with_alpha():
|
||||
assert Color(255, 50, 100).with_alpha(0.25) == Color(255, 50, 100, 0.25)
|
||||
|
||||
|
||||
def test_multiply_alpha():
|
||||
assert Color(100, 100, 100).multiply_alpha(0.5) == Color(100, 100, 100, 0.5)
|
||||
assert Color(100, 100, 100, 0.5).multiply_alpha(0.5) == Color(100, 100, 100, 0.25)
|
||||
|
||||
|
||||
def test_color_blend():
|
||||
assert Color(0, 0, 0).blend(Color(255, 255, 255), 0) == Color(0, 0, 0)
|
||||
assert Color(0, 0, 0).blend(Color(255, 255, 255), 1.0) == Color(255, 255, 255)
|
||||
@@ -149,6 +161,7 @@ def test_color_parse_color():
|
||||
|
||||
def test_color_add():
|
||||
assert Color(50, 100, 200) + Color(10, 20, 30, 0.9) == Color(14, 28, 47)
|
||||
assert Color(50, 100, 200).__add__("foo") == NotImplemented
|
||||
|
||||
|
||||
# Computed with http://www.easyrgb.com/en/convert.php,
|
||||
@@ -220,6 +233,10 @@ def test_inverse():
|
||||
def test_gradient_errors():
|
||||
with pytest.raises(ValueError):
|
||||
Gradient()
|
||||
with pytest.raises(ValueError):
|
||||
Gradient((0.1, Color.parse("red")))
|
||||
with pytest.raises(ValueError):
|
||||
Gradient((0.1, Color.parse("red")), (1, Color.parse("blue")))
|
||||
with pytest.raises(ValueError):
|
||||
Gradient((0, Color.parse("red")))
|
||||
|
||||
@@ -243,3 +260,7 @@ def test_gradient():
|
||||
assert gradient.get_color(1.2) == Color(0, 255, 0)
|
||||
assert gradient.get_color(0.5) == Color(0, 0, 255)
|
||||
assert gradient.get_color(0.7) == Color(0, 101, 153)
|
||||
|
||||
gradient._stops.pop()
|
||||
with pytest.raises(AssertionError):
|
||||
gradient.get_color(1.0)
|
||||
|
||||
Reference in New Issue
Block a user