diff --git a/src/textual/color.py b/src/textual/color.py index 269539a2e..70c33632b 100644 --- a/src/textual/color.py +++ b/src/textual/color.py @@ -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.""" diff --git a/tests/test_color.py b/tests/test_color.py index d9bfe3ca4..d07990c92 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -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,