def __rich_console__(self, console: Console, options: ConsoleOptions) -> RenderResult: for y in range(0, 5): for x in range(options.max_width): h = x / options.max_width l = 0.1 + ((y / 5) * 0.7) r1, g1, b1 = colorsys.hls_to_rgb(h, l, 1.0) r2, g2, b2 = colorsys.hls_to_rgb(h, l + 0.7 / 10, 1.0) bgcolor = Color.from_rgb(r1 * 255, g1 * 255, b1 * 255) color = Color.from_rgb(r2 * 255, g2 * 255, b2 * 255) yield Segment("▄", Style(color=color, bgcolor=bgcolor)) yield Segment.line()
def test_rich() -> None: color = Color.parse("red") as_text = color.__rich__() print(repr(as_text)) print(repr(as_text.spans)) assert as_text == Text("<color 'red' (standard)⬤ >", spans=[Span(23, 24, Style(color=color))])
def test_get_ansi_codes() -> None: 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", ) assert Color.parse("color(1)").get_ansi_codes() == ("31", ) assert Color.parse("color(1)").get_ansi_codes(False) == ("41", ) 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")
def test_truecolor() -> None: assert Color.parse("#ff0000").get_truecolor() == ColorTriplet(255, 0, 0) assert Color.parse("red").get_truecolor() == ColorTriplet(128, 0, 0) assert Color.parse("color(1)").get_truecolor() == ColorTriplet(128, 0, 0) assert Color.parse("color(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) assert Color("red", ColorType.WINDOWS, number=1).get_truecolor() == ColorTriplet(197, 15, 31)
def test_parse_error() -> None: with pytest.raises(ColorParseError): Color.parse("256") with pytest.raises(ColorParseError): Color.parse("color(256)") 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")
def test_default() -> None: assert Color.default() == Color("default", ColorType.DEFAULT, None, None)
def test_from_ansi() -> None: assert Color.from_ansi(1) == Color("color(1)", ColorType.STANDARD, 1)
def test_from_rgb() -> None: assert Color.from_rgb(0x10, 0x20, 0x30) == Color("#102030", ColorType.TRUECOLOR, None, ColorTriplet(0x10, 0x20, 0x30))
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) assert Color.parse("bright_red") == Color("bright_red", ColorType.STANDARD, 9, None) assert Color.parse("yellow4") == Color("yellow4", ColorType.EIGHT_BIT, 106, None) assert Color.parse("color(100)") == Color("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_system() -> None: assert Color.parse("default").system == ColorSystem.STANDARD assert Color.parse("red").system == ColorSystem.STANDARD assert Color.parse("#ff0000").system == ColorSystem.TRUECOLOR
def test_repr() -> None: assert repr(Color.parse("red")) == "<color 'red' 1 (standard)>"
def test_downgrade() -> None: assert Color.parse("color(9)").downgrade(0) == Color( "color(9)", ColorType.STANDARD, 9, None) assert Color.parse("#000000").downgrade(ColorSystem.EIGHT_BIT) == Color( "#000000", ColorType.EIGHT_BIT, 16, None) assert Color.parse("#ffffff").downgrade(ColorSystem.EIGHT_BIT) == Color( "#ffffff", ColorType.EIGHT_BIT, 231, None) 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) assert Color.parse("#ff0000").downgrade(ColorSystem.STANDARD) == Color( "#ff0000", ColorType.STANDARD, 1, None) assert Color.parse("color(9)").downgrade(ColorSystem.STANDARD) == Color( "color(9)", ColorType.STANDARD, 9, None) assert Color.parse("color(20)").downgrade(ColorSystem.STANDARD) == Color( "color(20)", ColorType.STANDARD, 4, None) 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, 9, None) assert Color.parse("#ff0000").downgrade(ColorSystem.WINDOWS) == Color( "#ff0000", ColorType.WINDOWS, 1, None) assert Color.parse("color(255)").downgrade(ColorSystem.WINDOWS) == Color( "color(255)", ColorType.WINDOWS, 15, None) assert Color.parse("#00ff00").downgrade(ColorSystem.STANDARD) == Color( "#00ff00", ColorType.STANDARD, 2, None)
def test_bgcolor_property(): assert Style(bgcolor="black").bgcolor == Color("black", ColorType.STANDARD, 0, None)
def test_color_property(): assert Style(color="red").color == Color("red", ColorType.STANDARD, 1, None)
import re from mudrich.text import Text from ..patches.style import MudStyle, ProtoStyle from mudrich.color import Color from typing import Union, List, Tuple LETTERS = { "x": Color.from_ansi(0), "r": Color.from_ansi(1), "g": Color.from_ansi(2), "y": Color.from_ansi(3), "b": Color.from_ansi(4), "m": Color.from_ansi(5), "c": Color.from_ansi(6), "w": Color.from_ansi(7), } EV_REGEX = { "fg_ansi_bold": re.compile(r"^(r|g|y|b|m|c|x|w)"), "fg_ansi_normal": re.compile(r"^(R|G|Y|B|M|C|X|W)"), "bg_ansi_bold": re.compile(r"^\[(r|g|y|b|m|c|x|w)"), "bg_ansi_normal": re.compile(r"^\[(R|G|Y|B|M|C|X|W)"), "fg_xterm": re.compile(r"^[0-5]{3}"), "bg_xterm": re.compile(r"^\[([0-5]{3})"), } def apply_fg_ansi_bold(proto: ProtoStyle, code): proto.bold = True
def test_windows() -> None: assert Color("red", ColorType.WINDOWS, number=1).get_ansi_codes() == ("31", )
def apply_color_rule(mark: ProtoStyle, rule_tuple): mode, g, data, original = rule_tuple if mode == "letters": for c in data: if c == "n": # ANSI reset mark.do_reset() continue if (bit := CHAR_MAP.get(c, None)): setattr(mark, bit, True) elif (bit := CHAR_MAP.get(c.lower(), None)): setattr(mark, bit, False) elif (code := BASE_COLOR_MAP.get(c, None)): setattr(mark, "color", Color.from_ansi(code)) elif (code := BASE_COLOR_MAP.get(c.lower(), None)): setattr(mark, "bgcolor", Color.from_ansi(code)) else: pass # I dunno what we got passed, but it ain't relevant. elif g == BgMode.FG: if mode == "numbers": setattr(mark, "color", Color.from_ansi(data)) elif mode == "name": if (found := COLORS.get(data)): setattr(mark, "color", Color.from_ansi(found["xterm"])) elif mode in ("rgb", "hex1", "hex2"): setattr(mark, "color", Color.from_rgb(data["red"], data["green"], data["blue"])) elif g == BgMode.BG:
""" Use Bar to renderer a sort-of circle. """ import math from mudrich.align import Align from mudrich.bar import Bar from mudrich.color import Color from mudrich import print SIZE = 40 for row in range(SIZE): y = (row / (SIZE - 1)) * 2 - 1 x = math.sqrt(1 - y * y) color = Color.from_rgb((1 + y) * 127.5, 0, 0) bar = Bar(2, width=SIZE * 2, begin=1 - x, end=1 + x, color=color) print(Align.center(bar))