From 82ef398df91182a3f1bcc446092300b65ab83556 Mon Sep 17 00:00:00 2001 From: David Mazary Date: Sun, 18 Mar 2012 16:07:20 -0300 Subject: [PATCH] adjust color scales between css and Python's colorsys --- xonstat/util.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/xonstat/util.py b/xonstat/util.py index 57fd54f..e88c6b3 100755 --- a/xonstat/util.py +++ b/xonstat/util.py @@ -59,8 +59,8 @@ _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 255 (white) -_contrast_threshold = 128 +# On a light scale of 0 (black) to 1.0 (white) +_contrast_threshold = 0.5 def qfont_decode(qstr=''): @@ -83,13 +83,15 @@ def strip_colors(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)) + # Convert hex to 8 bits and to 0.0-1.0 scale + r = 255. / int(match.group(1) * 2, 16) + g = 255. / int(match.group(2) * 2, 16) + b = 255. / int(match.group(3) * 2, 16) + 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) + # Get new rgb in 0-255 scale + r, g, b = map((255).__mul__, hls_to_rgb(hue, light, satur)) return ''.format(r, g, b) -- 2.39.2