diff --git a/humanfriendly/terminal/html.py b/humanfriendly/terminal/html.py
index 4214e09..34e8131 100644
--- a/humanfriendly/terminal/html.py
+++ b/humanfriendly/terminal/html.py
@@ -339,9 +339,9 @@ def parse_color(self, value):
elif length == 3:
# Three hex digits (shorthand).
return (
- int(value[0], 16),
- int(value[1], 16),
- int(value[2], 16),
+ int(value[0] * 2, 16),
+ int(value[1] * 2, 16),
+ int(value[2] * 2, 16),
)
# Try to recognize a named color.
value = value.lower()
diff --git a/humanfriendly/tests.py b/humanfriendly/tests.py
index 72dad99..ce090dc 100644
--- a/humanfriendly/tests.py
+++ b/humanfriendly/tests.py
@@ -1078,6 +1078,18 @@ def callback(text):
title="VPS hosting" rel="external">True
''')
+ def test_html_shorthand_colors(self):
+ """CSS short hex colors expand each digit before ANSI rendering."""
+ from humanfriendly.terminal.html import HTMLConverter
+ converter = HTMLConverter()
+ for short, full, rgb in [('#fff', '#ffffff', (255, 255, 255)),
+ ('#aBc', '#aaBBcc', (170, 187, 204)),
+ ('#123', '#112233', (17, 34, 51))]:
+ self.assertEqual(converter.parse_color(short), rgb)
+ for prop in ('color', 'background-color'):
+ template = 'text' % prop
+ self.assertEqual(html_to_ansi(template % short), html_to_ansi(template % full))
+
def test_generate_output(self):
"""Test the :func:`humanfriendly.terminal.output()` function."""
text = "Standard output generated by output()"