def main() -> int: step = 32 print( "bg_color {color} {bold} {dim} {italic} {underline} {invert} {strike}" .format( color="color", bold=tcolor("bold", styles=["bold"]), dim=tcolor("dim", styles=["dim"]), italic=tcolor("italic", styles=["italic"]), underline=tcolor("underline", styles=["underline"]), invert=tcolor("invert", styles=["invert"]), strike=tcolor("strike", styles=["strike"]), )) for i in range(0, 255, step): print_truecolor(Color((255, i, 0))) for i in range(0, 255, step): print_truecolor(Color((255 - i, 255, 0))) for i in range(0, 255, step): print_truecolor(Color((0, 255, i))) for i in range(0, 255, step): print_truecolor(Color((0, 255 - i, 255))) return 0
def test_normal(self): black_rgb = Color("#000000") red_rgb = Color("#ff0000") black_name = Color("black") assert black_rgb == black_rgb assert black_name == black_name assert black_rgb != red_rgb assert black_rgb != black_name
def __update_color(self, initialize: bool, **kwargs) -> None: fg_color = kwargs.get("color") or kwargs.get("fg_color") if fg_color: self.__fg_color: Optional[Color] = Color(fg_color) elif initialize: self.__fg_color = None bg_color = kwargs.get("bg_color") if bg_color: self.__bg_color: Optional[Color] = Color(bg_color) elif initialize: self.__bg_color = None
def _calc_other_ground_color(color: Color) -> Color: invert_threshold = 385 if (color.red + color.green + color.blue) > invert_threshold: return Color("black") return color
def test_normal(self, value, expected_red, expected_blue, expected_green, is_color_code_src): color = Color(value) assert color.red == expected_red assert color.green == expected_blue assert color.blue == expected_green assert color.is_color_code_src == is_color_code_src
def main() -> int: string = " " step = 8 line = "" for i in range(0, 255, step): line += tcolor(string, bg_color=Color((255, i, 0))) for i in range(0, 255, step): line += tcolor(string, bg_color=Color((255 - i, 255, 0))) for i in range(0, 255, step): line += tcolor(string, bg_color=Color((0, 255, i))) for i in range(0, 255, step): line += tcolor(string, bg_color=Color((0, 255 - i, 255))) for r in range(16): print(line) return 0
class Test_truecolor_bg_color: @pytest.mark.parametrize( ["string", "color", "expected"], [ ["test", "#ff8822", "\x1b[48;2;255;136;34mtest\x1b[0m"], ["test", "ff8822", "\x1b[48;2;255;136;34mtest\x1b[0m"], ["test", "red", "\x1b[41mtest\x1b[0m"], ["test", Color("red"), "\x1b[41mtest\x1b[0m"], ["test", "light-red", "\x1b[101mtest\x1b[0m"], ["test", (255, 136, 34), "\x1b[48;2;255;136;34mtest\x1b[0m"], ["test", Color("#ff8822"), "\x1b[48;2;255;136;34mtest\x1b[0m"], [ "test", Color((255, 136, 34)), "\x1b[48;2;255;136;34mtest\x1b[0m" ], ["test", None, "test"], ], ) def test_normal_bg_color(self, string, color, expected): assert tcolor(string, bg_color=color) == expected
class Test_truecolor_color: @pytest.mark.parametrize( ["string", "color", "expected"], [ ["test", "#ff8822", "\x1b[38;2;255;136;34mtest\x1b[0m"], ["test", "ff8822", "\x1b[38;2;255;136;34mtest\x1b[0m"], ["test", "red", "\x1b[31mtest\x1b[0m"], ["test", Color("red"), "\x1b[31mtest\x1b[0m"], ["test", (255, 136, 34), "\x1b[38;2;255;136;34mtest\x1b[0m"], ["test", Color("#ff8822"), "\x1b[38;2;255;136;34mtest\x1b[0m"], [ "test", Color((255, 136, 34)), "\x1b[38;2;255;136;34mtest\x1b[0m" ], ["test", None, "test"], ], ) def test_normal(self, string, color, expected): assert tcolor(string, color=color) == expected def test_smoke(self): string = "test" for color in AnsiFGColor: assert tcolor(string, color=color) != string for color in AnsiBGColor: assert tcolor(string, bg_color=color) != string @pytest.mark.parametrize( ["value", "expected"], [ ["#fffff", ValueError], ["#GGGGGG", ValueError], ], ) def test_exception(self, value, expected): with pytest.raises(expected): tcolor("test", color=value) == expected
def style_filter(cell: Cell, **kwargs: Dict[str, Any]) -> Optional[Style]: fg_color: Union[Color, str, None] = None bg_color: Union[Color, str, None] = None color = Color(kwargs.get("color", DEFAULT_COLOR)) if cell.row % 2 == 0: fg_color = _calc_other_ground_color(color) bg_color = color else: fg_color = color if fg_color or bg_color: return Style(color=fg_color, bg_color=bg_color) return None
def style_filter(cell: Cell, **kwargs) -> Optional[Style]: fg_color = None # type: Union[Color, str, None] bg_color = None # type: Union[Color, str, None] dtrs = kwargs["dtrs"] now = kwargs["now"] if cell.row < 0: return None is_end = dtrs[cell.row].end_datetime <= now ddays = max(0, dtrs[cell.row].end_datetime.day - now.day) n = max(0, 255 - ddays * 32) bg_color = Color((n, n, n)) fg_color = "black" if is_end: fg_color = GRAY if fg_color or bg_color: return Style(color=fg_color, bg_color=bg_color) return None
def __update(self, initialize: bool, **kwargs) -> None: fg_color = kwargs.get("color") if fg_color: self.__fg_color: Optional[Color] = Color(fg_color) elif initialize: self.__fg_color = None bg_color = kwargs.get("bg_color") if bg_color: self.__bg_color: Optional[Color] = Color(bg_color) elif initialize: self.__bg_color = None padding = kwargs.get("padding") if padding is not None: self.__padding = padding elif initialize: self.__padding = None align = kwargs.get("align") if align: self.__align = normalize_enum(align, Align, default=Align.AUTO) elif initialize: self.__align = Align.AUTO valign = kwargs.get("vertical_align") if valign: self.__valign = normalize_enum(valign, VerticalAlign, default=VerticalAlign.BASELINE) elif initialize: self.__valign = VerticalAlign.BASELINE decoration_line = kwargs.get("decoration_line") if decoration_line: self.__decoration_line = normalize_enum( decoration_line, DecorationLine, default=DecorationLine.NONE) elif initialize: self.__decoration_line = DecorationLine.NONE font_size = kwargs.get("font_size") if font_size: self.__font_size = normalize_enum(kwargs.get("font_size"), FontSize, validate=False, default=FontSize.NONE) elif initialize: self.__font_size = FontSize.NONE self.__validate_attr("font_size", (FontSize, str)) font_style = kwargs.get("font_style") if font_style: self.__font_style = normalize_enum(font_style, FontStyle, default=FontStyle.NORMAL) elif initialize: self.__font_style = FontStyle.NORMAL font_weight = kwargs.get("font_weight") if font_weight: self.__font_weight = normalize_enum(font_weight, FontWeight, default=FontWeight.NORMAL) elif initialize: self.__font_weight = FontWeight.NORMAL thousand_separator = kwargs.get("thousand_separator") if thousand_separator: self.__thousand_separator = _normalize_thousand_separator( normalize_enum( thousand_separator, ThousandSeparator, default=ThousandSeparator.NONE, validate=False, )) elif initialize: self.__thousand_separator = ThousandSeparator.NONE self.__validate_attr("thousand_separator", ThousandSeparator)
from typing import Union # noqa from typing import Optional from pytablewriter.style import Cell, Style from tcolorpy import Color GRAY = Color("#bfbfbf") def _calc_other_ground_color(color: Color) -> Color: invert_threshold = 385 if (color.red + color.green + color.blue) > invert_threshold: return Color("black") return color def col_separator_style_filter(lcell: Cell, rcell: Cell, **kwargs) -> Optional[Style]: fg_color = None # type: Union[Color, str, None] bg_color = None # type: Union[Color, str, None] row = lcell.row if lcell else rcell.row col = lcell.col if lcell else rcell.col dtrs = kwargs["dtrs"] now = kwargs["now"] if row < 0: return None is_end = dtrs[row].end_datetime <= now
def test_normal(self, value, expected): assert Color(value).hsv == expected
def test_exception_msg(self, value, expected): with pytest.raises(ValueError) as e: Color(value) assert expected in str(e.value)
def test_exception(self, value, expected): with pytest.raises(expected): Color(value)