Skip to content

Commit 67cb29b

Browse files
Delgangvalkov
authored andcommitted
Fix wrong conversion of short hex color codes
1 parent ab1d1ed commit 67cb29b

File tree

2 files changed

+3
-2
lines changed

2 files changed

+3
-2
lines changed

ansimarkup/markup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ def sub_tag(self, match: Match, tag_list: List[str], res_list: List[str]) -> str
143143
elif re.match(r"#(?:[a-fA-F0-9]{3}){1,2}$", color):
144144
hex_color = color[1:]
145145
if len(hex_color) == 3:
146-
hex_color *= 2
146+
r, g, b = hex_color
147+
hex_color = r * 2 + g * 2 + b * 2
147148
res = "\033[%s;2;%s;%s;%sm" % ((code,) + hex_to_rgb(hex_color))
148149
elif color.count(",") == 2:
149150
colors = tuple(color.split(","))

tests/test_markup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_xterm_color():
6767
def test_xterm_hex():
6868
assert p("<fg #ff0000>1") == p("<fg #FF0000>1") == "\x1b[38;2;255;0;0m" "1"
6969
assert p("<bg #00A000><fg #ff0000>1") == "\x1b[48;2;0;160;0m" "\x1b[38;2;255;0;0m" "1"
70-
assert p("<fg #F12>1</fg #F12>") == p("<fg #F12F12>1</fg #F12F12>") == "\x1b[38;2;241;47;18m" + "1" + S.RESET_ALL
70+
assert p("<fg #F12>1</fg #F12>") == p("<fg #FF1122>1</fg #FF1122>") == "\x1b[38;2;255;17;34m" + "1" + S.RESET_ALL
7171

7272
assert p("<fg #>1</fg #>") == "<fg #>1</fg #>"
7373
assert p("<bg #12>1</bg #12>") == "<bg #12>1</bg #12>"

0 commit comments

Comments
 (0)