color tests

This commit is contained in:
Will McGugan
2022-04-06 11:59:58 +01:00
parent 5f40f45797
commit f3ff9c61c7
2 changed files with 31 additions and 22 deletions

View File

@@ -100,22 +100,7 @@ class Color(NamedTuple):
Color: A new color.
"""
r, g, b = hls_to_rgb(h, l, s)
return cls(int(r * 255), int(g * 255), int(b * 255))
@classmethod
def from_hsv(cls, h: float, s: float, v: float) -> Color:
"""Create a color from HSV components.
Args:
h (float): Hue
s (float): Saturation
v (float): Value
Returns:
Color: A new Color.
"""
r, g, b = hsv_to_rgb(h, s, v)
return cls(int(r * 255), int(g * 255), int(b * 255))
return cls(int(r * 255 + 0.5), int(g * 255 + 0.5), int(b * 255 + 0.5))
def __rich__(self) -> Text:
"""A Rich method to show the color."""
@@ -158,12 +143,6 @@ class Color(NamedTuple):
r, g, b = self.normalized
return HLS(*rgb_to_hls(r, g, b))
@property
def hsv(self) -> HSV:
"""Get the color as HSV."""
r, g, b = self.normalized
return HSV(*rgb_to_hsv(r, g, b))
@property
def brightness(self) -> float:
"""Get the human perceptual brightness."""

View File

@@ -76,6 +76,36 @@ def test_hls():
assert red.hls == pytest.approx(
(0.9888888888888889, 0.43137254901960786, 0.818181818181818)
)
assert Color.from_hls(
0.9888888888888889, 0.43137254901960786, 0.818181818181818
).normalized == pytest.approx(red.normalized, rel=1e-5)
def test_color_brightness():
assert Color(255, 255, 255).brightness == 1
assert Color(0, 0, 0).brightness == 0
assert Color(127, 127, 127).brightness == pytest.approx(0.49803921568627446)
assert Color(255, 127, 64).brightness == pytest.approx(0.6199607843137255)
def test_color_hex():
assert Color(255, 0, 127).hex == "#FF007F"
assert Color(255, 0, 127, 0.5).hex == "#FF007F7F"
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)"
def test_color_with_alpha():
assert Color(255, 50, 100).with_alpha(0.25) == Color(255, 50, 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)
assert Color(0, 0, 0).blend(Color(255, 255, 255), 0.5) == Color(127, 127, 127)
# Computed with http://www.easyrgb.com/en/convert.php,