]> de.git.xonotic.org Git - xonotic/xonstat.git/blobdiff - xonstat/util.py
More work on classic and minimal themes; now both include a fancy Xonotic logo!
[xonotic/xonstat.git] / xonstat / util.py
old mode 100755 (executable)
new mode 100644 (file)
index 75db20f..1bea47c
@@ -1,5 +1,5 @@
 import re
-from colorsys import rgb_to_hls
+from colorsys import rgb_to_hls, hls_to_rgb
 from cgi import escape as html_escape
 from datetime import datetime
 
@@ -42,16 +42,16 @@ _qfont_table = [
 
 # Hex-colored spans for decimal color codes ^0 - ^9
 _dec_spans = [
- "<span style='color:#333333'>",
- "<span style='color:#FF0000'>",
- "<span style='color:#33FF00'>",
- "<span style='color:#FFFF00'>",
- "<span style='color:#3366FF'>",
- "<span style='color:#33FFFF'>",
- "<span style='color:#FF3366'>",
- "<span style='color:#FFFFFF'>",
- "<span style='color:#999999'>",
- "<span style='color:#666666'>"
+ "<span style='color:rgb(128,128,128)'>",
+ "<span style='color:rgb(255,0,0)'>",
+ "<span style='color:rgb(51,255,0)'>",
+ "<span style='color:rgb(255,255,0)'>",
+ "<span style='color:rgb(51,102,255)'>",
+ "<span style='color:rgb(51,255,255)'>",
+ "<span style='color:rgb(255,51,102)'>",
+ "<span style='color:rgb(255,255,255)'>",
+ "<span style='color:rgb(153,153,153)'>",
+ "<span style='color:rgb(128,128,128)'>"
 ]
 
 # Color code patterns
@@ -59,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.
@@ -77,15 +80,25 @@ 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 '<span style="color:#{0}{1}{2}" class="{3}">'.format(r, g, b, klass)
+    """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 '<span style="color:rgb(%d,%d,%d)">' % (255 * r, 255 * g, 255 * b)
 
 
 def html_colors(qstr=''):
@@ -105,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):