rich/tests/test_color.py

159 lines
5.2 KiB
Python
Raw Normal View History

2019-12-07 21:16:49 +00:00
from rich.color import (
blend_rgb,
parse_rgb_hex,
Color,
ColorParseError,
ColorSystem,
ColorType,
ColorTriplet,
)
2020-02-14 16:09:53 +00:00
from rich import terminal_theme
2019-12-07 21:16:49 +00:00
import pytest
def test_str() -> None:
assert str(Color.parse("red")) == "\x1b[31m⬤ \x1b[0m<color 'red' (standard)>"
def test_repr() -> None:
assert repr(Color.parse("red")) == "<color 'red' (standard)>"
def test_system() -> None:
2019-12-07 21:54:49 +00:00
assert Color.parse("default").system == ColorSystem.STANDARD
2019-12-07 21:16:49 +00:00
assert Color.parse("red").system == ColorSystem.STANDARD
assert Color.parse("#ff0000").system == ColorSystem.TRUECOLOR
2020-07-27 16:21:03 +00:00
def test_windows() -> None:
assert Color("red", ColorType.WINDOWS, number=1).get_ansi_codes() == ("31",)
2019-12-07 21:16:49 +00:00
def test_truecolor() -> None:
2020-02-14 16:09:53 +00:00
assert Color.parse("#ff0000").get_truecolor() == ColorTriplet(255, 0, 0)
assert Color.parse("red").get_truecolor() == ColorTriplet(128, 0, 0)
assert Color.parse("1").get_truecolor() == ColorTriplet(128, 0, 0)
assert Color.parse("17").get_truecolor() == ColorTriplet(0, 0, 95)
assert Color.parse("default").get_truecolor() == ColorTriplet(0, 0, 0)
assert Color.parse("default").get_truecolor(foreground=False) == ColorTriplet(
255, 255, 255
2019-12-07 21:16:49 +00:00
)
2020-05-14 19:15:31 +00:00
assert Color("red", ColorType.WINDOWS, number=1).get_truecolor() == ColorTriplet(
2020-07-27 16:21:03 +00:00
170, 0, 0
2020-05-14 19:15:31 +00:00
)
2019-12-07 21:16:49 +00:00
def test_parse_success() -> None:
assert Color.parse("default") == Color("default", ColorType.DEFAULT, None, None)
assert Color.parse("red") == Color("red", ColorType.STANDARD, 1, None)
2020-07-27 16:21:03 +00:00
assert Color.parse("bright_red") == Color("bright_red", ColorType.STANDARD, 9, None)
2019-12-09 11:10:03 +00:00
assert Color.parse("yellow4") == Color("yellow4", ColorType.EIGHT_BIT, 106, None)
2019-12-07 21:16:49 +00:00
assert Color.parse("100") == Color("100", ColorType.EIGHT_BIT, 100, None)
assert Color.parse("#112233") == Color(
"#112233", ColorType.TRUECOLOR, None, ColorTriplet(0x11, 0x22, 0x33)
)
assert Color.parse("rgb(90,100,110)") == Color(
"rgb(90,100,110)", ColorType.TRUECOLOR, None, ColorTriplet(90, 100, 110)
)
def test_from_triplet() -> None:
assert Color.from_triplet(ColorTriplet(0x10, 0x20, 0x30)) == Color(
"#102030", ColorType.TRUECOLOR, None, ColorTriplet(0x10, 0x20, 0x30)
)
2019-12-07 21:54:49 +00:00
def test_default() -> None:
assert Color.default() == Color("default", ColorType.DEFAULT, None, None)
2019-12-07 21:16:49 +00:00
def test_parse_error() -> None:
2019-12-07 21:54:49 +00:00
with pytest.raises(ColorParseError):
Color.parse("256")
2019-12-07 21:16:49 +00:00
with pytest.raises(ColorParseError):
Color.parse("rgb(999,0,0)")
with pytest.raises(ColorParseError):
Color.parse("rgb(0,0)")
with pytest.raises(ColorParseError):
Color.parse("rgb(0,0,0,0)")
with pytest.raises(ColorParseError):
Color.parse("nosuchcolor")
with pytest.raises(ColorParseError):
Color.parse("#xxyyzz")
2019-12-07 21:54:49 +00:00
def test_get_ansi_codes() -> None:
2020-05-21 16:15:08 +00:00
assert Color.parse("default").get_ansi_codes() == ("39",)
assert Color.parse("default").get_ansi_codes(False) == ("49",)
assert Color.parse("red").get_ansi_codes() == ("31",)
assert Color.parse("red").get_ansi_codes(False) == ("41",)
2020-07-27 16:21:03 +00:00
assert Color.parse("1").get_ansi_codes() == ("31",)
assert Color.parse("1").get_ansi_codes(False) == ("41",)
2020-05-21 16:15:08 +00:00
assert Color.parse("#ff0000").get_ansi_codes() == ("38", "2", "255", "0", "0")
assert Color.parse("#ff0000").get_ansi_codes(False) == ("48", "2", "255", "0", "0")
2019-12-07 21:54:49 +00:00
def test_downgrade() -> None:
2020-07-27 16:21:03 +00:00
assert Color.parse("9").downgrade(0) == Color("9", ColorType.STANDARD, 9, None)
2019-12-07 21:54:49 +00:00
assert Color.parse("#000000").downgrade(ColorSystem.EIGHT_BIT) == Color(
2019-12-09 22:02:46 +00:00
"#000000", ColorType.EIGHT_BIT, 16, None
2019-12-07 21:54:49 +00:00
)
assert Color.parse("#ffffff").downgrade(ColorSystem.EIGHT_BIT) == Color(
2019-12-09 22:02:46 +00:00
"#ffffff", ColorType.EIGHT_BIT, 231, None
2019-12-07 21:54:49 +00:00
)
assert Color.parse("#404142").downgrade(ColorSystem.EIGHT_BIT) == Color(
"#404142", ColorType.EIGHT_BIT, 237, None
)
assert Color.parse("#ff0000").downgrade(ColorSystem.EIGHT_BIT) == Color(
"#ff0000", ColorType.EIGHT_BIT, 196, None
)
2020-05-14 19:15:31 +00:00
2019-12-07 21:54:49 +00:00
assert Color.parse("#ff0000").downgrade(ColorSystem.STANDARD) == Color(
"#ff0000", ColorType.STANDARD, 1, None
)
assert Color.parse("9").downgrade(ColorSystem.STANDARD) == Color(
2020-07-27 16:21:03 +00:00
"9", ColorType.STANDARD, 9, None
)
assert Color.parse("20").downgrade(ColorSystem.STANDARD) == Color(
"20", ColorType.STANDARD, 4, None
2019-12-07 21:54:49 +00:00
)
2020-05-14 19:15:31 +00:00
assert Color.parse("red").downgrade(ColorSystem.WINDOWS) == Color(
"red", ColorType.WINDOWS, 1, None
)
assert Color.parse("bright_red").downgrade(ColorSystem.WINDOWS) == Color(
"bright_red", ColorType.WINDOWS, 1, None
)
assert Color.parse("#ff0000").downgrade(ColorSystem.WINDOWS) == Color(
"#ff0000", ColorType.WINDOWS, 1, None
)
assert Color.parse("255").downgrade(ColorSystem.WINDOWS) == Color(
"255", ColorType.WINDOWS, 7, None
)
2020-07-27 16:21:03 +00:00
assert Color.parse("#00ff00").downgrade(ColorSystem.STANDARD) == Color(
"#00ff00", ColorType.STANDARD, 2, None
)
2019-12-07 21:54:49 +00:00
def test_parse_rgb_hex() -> None:
2019-12-07 21:16:49 +00:00
assert parse_rgb_hex("aabbcc") == ColorTriplet(0xAA, 0xBB, 0xCC)
def test_blend_rgb() -> None:
assert blend_rgb(
ColorTriplet(10, 20, 30), ColorTriplet(30, 40, 50)
) == ColorTriplet(20, 30, 40)