diff --git a/src/textual_webterm/svg_exporter.py b/src/textual_webterm/svg_exporter.py index 4ad85e9..ed4d85b 100644 --- a/src/textual_webterm/svg_exporter.py +++ b/src/textual_webterm/svg_exporter.py @@ -68,10 +68,15 @@ def _color_to_hex(color: str, is_foreground: bool = True) -> str: if color == "default": return DEFAULT_FG if is_foreground else DEFAULT_BG - # Already a hex color + # Already a hex color with # if color.startswith("#"): return color + # Hex color without # prefix (pyte's 256-color/truecolor format) + # Check if it looks like a hex color (6 hex digits) + if len(color) == 6 and all(c in "0123456789abcdefABCDEF" for c in color): + return f"#{color}" + # Named color lookup (case-insensitive) lower = color.lower() if lower in ANSI_COLORS: diff --git a/tests/test_svg_exporter.py b/tests/test_svg_exporter.py index 101c623..8430740 100644 --- a/tests/test_svg_exporter.py +++ b/tests/test_svg_exporter.py @@ -31,6 +31,13 @@ class TestColorToHex: assert _color_to_hex("#123456") == "#123456" assert _color_to_hex("#AABBCC") == "#AABBCC" + def test_hex_color_without_hash(self) -> None: + """Hex colors without # prefix (pyte's 256-color/truecolor) get # added.""" + assert _color_to_hex("ff0000") == "#ff0000" + assert _color_to_hex("123456") == "#123456" + assert _color_to_hex("AABBCC") == "#AABBCC" + assert _color_to_hex("ff8700") == "#ff8700" # Common 256-color orange + def test_named_colors(self) -> None: """Named ANSI colors map correctly.""" assert _color_to_hex("red") == ANSI_COLORS["red"]