From 6beb885d41c7deddfeffd68df9dfa7d6ed0b6596 Mon Sep 17 00:00:00 2001 From: David Mazary Date: Sun, 18 Mar 2012 18:54:19 -0300 Subject: [PATCH] Reduce the operations in hex_repl for a small performance boost. --- xonstat/util.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/xonstat/util.py b/xonstat/util.py index de5153a..aaa52d5 100755 --- a/xonstat/util.py +++ b/xonstat/util.py @@ -83,16 +83,23 @@ def strip_colors(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(0) * 2, 16) / 255.0 + g = int(match.group(1) * 2, 16) / 255.0 + b = int(match.group(2) * 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 ''.format(r, g, b) + 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=''): -- 2.39.2