tests for color

This commit is contained in:
Will McGugan
2022-04-06 13:32:34 +01:00
parent f3ff9c61c7
commit 608d8b83d7
2 changed files with 44 additions and 22 deletions

View File

@@ -281,7 +281,7 @@ class Color(NamedTuple):
"""
l, a, b = rgb_to_lab(self)
l -= amount * 100
return lab_to_rgb(Lab(l, a, b))
return lab_to_rgb(Lab(l, a, b)).clamped
def lighten(self, amount: float) -> Color:
"""Lighten the color by a given amount.

View File

@@ -6,27 +6,6 @@ from rich.text import Text
from textual.color import Color, ColorPair, Lab, lab_to_rgb, rgb_to_lab
@pytest.mark.parametrize(
"text,expected",
[
("#000000", Color(0, 0, 0, 1.0)),
("#ffffff", Color(255, 255, 255, 1.0)),
("#FFFFFF", Color(255, 255, 255, 1.0)),
("#020304ff", Color(2, 3, 4, 1.0)),
("#02030400", Color(2, 3, 4, 0.0)),
("#0203040f", Color(2, 3, 4, 0.058823529411764705)),
("rgb(0,0,0)", Color(0, 0, 0, 1.0)),
("rgb(255,255,255)", Color(255, 255, 255, 1.0)),
("rgba(255,255,255,1)", Color(255, 255, 255, 1.0)),
("rgb(2,3,4)", Color(2, 3, 4, 1.0)),
("rgba(2,3,4,1.0)", Color(2, 3, 4, 1.0)),
("rgba(2,3,4,0.058823529411764705)", Color(2, 3, 4, 0.058823529411764705)),
],
)
def test_parse(text, expected):
assert Color.parse(text) == expected
def test_rich_color():
"""Check conversion to Rich color."""
assert Color(10, 20, 30, 1.0).rich_color == RichColor.from_rgb(10, 20, 30)
@@ -108,6 +87,31 @@ def test_color_blend():
assert Color(0, 0, 0).blend(Color(255, 255, 255), 0.5) == Color(127, 127, 127)
@pytest.mark.parametrize(
"text,expected",
[
("#000000", Color(0, 0, 0, 1.0)),
("#ffffff", Color(255, 255, 255, 1.0)),
("#FFFFFF", Color(255, 255, 255, 1.0)),
("#020304ff", Color(2, 3, 4, 1.0)),
("#02030400", Color(2, 3, 4, 0.0)),
("#0203040f", Color(2, 3, 4, 0.058823529411764705)),
("rgb(0,0,0)", Color(0, 0, 0, 1.0)),
("rgb(255,255,255)", Color(255, 255, 255, 1.0)),
("rgba(255,255,255,1)", Color(255, 255, 255, 1.0)),
("rgb(2,3,4)", Color(2, 3, 4, 1.0)),
("rgba(2,3,4,1.0)", Color(2, 3, 4, 1.0)),
("rgba(2,3,4,0.058823529411764705)", Color(2, 3, 4, 0.058823529411764705)),
],
)
def test_color_parse(text, expected):
assert Color.parse(text) == expected
def test_color_add():
assert Color(50, 100, 200) + Color(10, 20, 30, 0.9) == Color(14, 28, 47)
# Computed with http://www.easyrgb.com/en/convert.php,
# (r, g, b) values in sRGB to (L*, a*, b*) values in CIE-L*ab.
RGB_LAB_DATA = [
@@ -118,6 +122,19 @@ RGB_LAB_DATA = [
]
def test_color_darken():
assert Color(200, 210, 220).darken(1) == Color(0, 0, 0)
assert Color(200, 210, 220).darken(-1) == Color(255, 255, 255)
assert Color(200, 210, 220).darken(0.1) == Color(172, 182, 192)
assert Color(200, 210, 220).darken(0.5) == Color(71, 80, 88)
def test_color_lighten():
assert Color(200, 210, 220).lighten(1) == Color(255, 255, 255)
assert Color(200, 210, 220).lighten(-1) == Color(0, 0, 0)
assert Color(200, 210, 220).lighten(0.1) == Color(228, 238, 248)
@pytest.mark.parametrize(
"r, g, b, L_, a_, b_",
RGB_LAB_DATA,
@@ -155,3 +172,8 @@ def test_rgb_lab_rgb_roundtrip():
assert c_.r == pytest.approx(r, abs=1)
assert c_.g == pytest.approx(g, abs=1)
assert c_.b == pytest.approx(b, abs=1)
def test_color_pair_style():
pair = ColorPair(Color(220, 220, 220), Color(10, 20, 30))
assert str(pair.style) == "#dcdcdc on #0a141e"