X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;f=xonstat%2Futil.py;h=1bea47c94d8100e3047464dc6601657d010d9de3;hb=8c670addca3e7870b12252cfed7b546c43ff4560;hp=23d9e28e0170ba6d9a93d6e61dd33fd9e0f3aaf1;hpb=f348d11d41223893ecff4e6746bb0cce5aeab8de;p=xonotic%2Fxonstat.git diff --git a/xonstat/util.py b/xonstat/util.py old mode 100755 new mode 100644 index 23d9e28..1bea47c --- a/xonstat/util.py +++ b/xonstat/util.py @@ -1,5 +1,6 @@ import re -from cgi import escape +from colorsys import rgb_to_hls, hls_to_rgb +from cgi import escape as html_escape from datetime import datetime # Map of special chars to ascii from Darkplace's console.c. @@ -41,16 +42,16 @@ _qfont_table = [ # Hex-colored spans for decimal color codes ^0 - ^9 _dec_spans = [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" ] # Color code patterns @@ -58,6 +59,9 @@ _all_colors = re.compile(r'\^(\d|x[\dA-Fa-f]{3})') _dec_colors = re.compile(r'\^(\d)') _hex_colors = re.compile(r'\^x([\dA-Fa-f])([\dA-Fa-f])([\dA-Fa-f])') +# On a light scale of 0 (black) to 1.0 (white) +_contrast_threshold = 0.5 + def qfont_decode(qstr=''): """ Convert the qfont characters in a string to ascii. @@ -78,13 +82,29 @@ def strip_colors(qstr=''): return _all_colors.sub('', qstr) +def hex_repl(match): + """Convert Darkplaces hex color codes to CSS rgb. + Brighten colors with HSL light value less than 50%""" + + # Extend hex char to 8 bits and to 0.0-1.0 scale + r = int(match.group(1) * 2, 16) / 255.0 + g = int(match.group(2) * 2, 16) / 255.0 + b = int(match.group(3) * 2, 16) / 255.0 + + # Check if color is too dark + hue, light, satur = rgb_to_hls(r, g, b) + if light < _contrast_threshold: + light = _contrast_threshold + r, g, b = hls_to_rgb(hue, light, satur) + + # Convert back to 0-255 scale for css + return '' % (255 * r, 255 * g, 255 * b) + + 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)) @@ -98,7 +118,7 @@ def pretty_date(time=False): pretty string like 'an hour ago', 'Yesterday', '3 months ago', 'just now', etc """ - now = datetime.now() + now = datetime.utcnow() if type(time) is int: diff = now - datetime.fromtimestamp(time) elif isinstance(time,datetime):