]> de.git.xonotic.org Git - xonotic/xonstat.git/commitdiff
Reduce one operation in hex_repl (util.py) via dmazary.
authorAnt Zucaro <azucaro@gmail.com>
Mon, 19 Mar 2012 02:53:17 +0000 (22:53 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Mon, 19 Mar 2012 02:53:17 +0000 (22:53 -0400)
xonstat/util.py

index 1db7770565314029da3a2b8734a22d75513d6063..92ab448e1616f57a9a247fdb2cfb316564058c9a 100755 (executable)
@@ -80,19 +80,25 @@ def strip_colors(qstr=''):
     if qstr == None:
         qstr = ''
     return _all_colors.sub('', qstr)
-    
-    
+
+
 def hex_repl(match):
-    # Convert hex to 8 bits and to 0.0-1.0 scale
-    r = int(match.group(1) * 2, 16) / 255.
-    g = int(match.group(2) * 2, 16) / 255.
-    b = int(match.group(3) * 2, 16) / 255.
+    """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
-    # Get new rgb in 0-255 scale
-    r, g, b = tuple([int(round(255 * i)) for i in hls_to_rgb(hue, light, satur)])
-    return '<span style="color:rgb({0},{1},{2})">'.format(r, g, b)
+        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=''):