From 63abedde065d1849bb500c6c7aa40fd68f171377 Mon Sep 17 00:00:00 2001 From: David Mazary Date: Fri, 16 Mar 2012 12:14:36 -0300 Subject: [PATCH] Idea about classifying text as light or dark so css styling could increase contrast for dark text. This looks interesting: http://css-tricks.com/adding-stroke-to-web-text/ Maybe using that trick with an electric-blue text outline for dark text. --- xonstat/util.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/xonstat/util.py b/xonstat/util.py index 23d9e28..75db20f 100755 --- a/xonstat/util.py +++ b/xonstat/util.py @@ -1,5 +1,6 @@ import re -from cgi import escape +from colorsys import rgb_to_hls +from cgi import escape as html_escape from datetime import datetime # Map of special chars to ascii from Darkplace's console.c. @@ -76,15 +77,21 @@ def strip_colors(qstr=''): if qstr == None: qstr = '' return _all_colors.sub('', qstr) + + +def hex_repl(match): + r = match.group(1) * 2 + g = match.group(2) * 2 + b = match.group(3) * 2 + hue, light, satur = rgb_to_hls(int(r, 16), int(g, 16), int(b, 16)) + klass = 'text_dark' if light < 0x80 else 'text_light' + return ''.format(r, g, b, klass) def html_colors(qstr=''): - qstr = escape(qfont_decode(qstr)) - def dec_repl(match): - return _dec_spans[int(match.group(1))] - qstr = qstr.replace('^^', '^') - html = _dec_colors.sub(dec_repl, qstr) - html = _hex_colors.sub(r"", html) + qstr = html_escape(qfont_decode(qstr).replace('^^', '^')) + html = _dec_colors.sub(lambda match: _dec_spans[int(match.group(1))], qstr) + html = _hex_colors.sub(hex_repl, html) return html + "" * len(_all_colors.findall(qstr)) -- 2.39.2