From 7e576941ca5382ca4b5737fc0b45d33ddf9fe620 Mon Sep 17 00:00:00 2001 From: Jonas Wielicki Date: Thu, 9 Nov 2017 09:05:09 +0100 Subject: Add support for XEP-0392 (Consistent Color Generation) --- poezio/xhtml.py | 26 +++++--------------------- 1 file changed, 5 insertions(+), 21 deletions(-) (limited to 'poezio/xhtml.py') diff --git a/poezio/xhtml.py b/poezio/xhtml.py index ee98f23d..4f469bd7 100644 --- a/poezio/xhtml.py +++ b/poezio/xhtml.py @@ -24,6 +24,7 @@ from xml.sax import saxutils from slixmpp.xmlstream import ET from poezio.config import config +from poezio.colors import ncurses_color_to_rgb digits = '0123456789' # never trust the modules @@ -212,6 +213,9 @@ def get_body_from_message_stanza(message, content = content if content else message['body'] return content or " " +def rgb_to_html(rgb): + r, g, b = rgb + return '#%02X%02X%02X' % (int(r*256), int(g*256), int(b*256)) def ncurses_color_to_html(color): """ @@ -219,27 +223,7 @@ def ncurses_color_to_html(color): a string of the form #XXXXXX representing an html color. """ - if color <= 15: - try: - (r, g, b) = curses.color_content(color) - except: # fallback in faulty terminals (e.g. xterm) - (r, g, b) = curses.color_content(color % 8) - r = r / 1000 * 6 - 0.01 - g = g / 1000 * 6 - 0.01 - b = b / 1000 * 6 - 0.01 - elif color <= 231: - color = color - 16 - r = color % 6 - color = color / 6 - g = color % 6 - color = color / 6 - b = color % 6 - else: - color -= 232 - r = g = b = color / 24 * 6 - return '#%02X%02X%02X' % (int(r * 256 / 6), int(g * 256 / 6), - int(b * 256 / 6)) - + return rgb_to_html(ncurses_color_to_rgb(color)) def _parse_css_color(name): if name[0] == '#': -- cgit v1.2.3