From 8b084bf60afd20d804bd5184b1773aa6e9b2c187 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 6 Jul 2014 11:55:54 +0400 Subject: [PATCH] Also use CIE2000 in colors_find --- tools/colors_find.py | 49 ++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/tools/colors_find.py b/tools/colors_find.py index 6ba1489d..cf1ba1c5 100755 --- a/tools/colors_find.py +++ b/tools/colors_find.py @@ -1,39 +1,58 @@ #!/usr/bin/env python +from __future__ import division, print_function import sys import os +from colormath.color_objects import sRGBColor, LabColor +from colormath.color_conversions import convert_color +from colormath.color_diff import delta_e_cie2000 -def get_color(name, rgb): - return name, (int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:6], 16)) +def get_lab(name, rgb): + rgb = sRGBColor(int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:6], 16), + is_upscaled=True) + lab = convert_color(rgb, LabColor) + return name, lab with open(os.path.join(os.path.dirname(__file__), 'colors.map'), 'r') as f: - colors = [get_color(*line.split('\t')) for line in f] + colors = [get_lab(*line.split('\t')) for line in f] -urgb = get_color(None, sys.argv[1])[1] - - -def col_distance(rgb1, rgb2): - return sum(((rgb1[i] - rgb2[i]) ** 2 for i in range(3))) +ulab = get_lab(None, sys.argv[1])[1] def find_color(urgb, colors): cur_distance = 3 * (255 ** 2 + 1) cur_color = None - for color, crgb in colors: - dist = col_distance(urgb, crgb) + for color, clab in colors: + dist = delta_e_cie2000(ulab, clab) if dist < cur_distance: cur_distance = dist - cur_color = (color, crgb) + cur_color = (color, clab) return cur_color -cur_color = find_color(urgb, colors) +cur_color = find_color(ulab, colors) -print urgb, ':', cur_color -col_1 = ';2;' + ';'.join((str(i) for i in urgb)) + 'm' -col_2 = ';2;' + ';'.join((str(i) for i in cur_color[1])) + 'm' +def lab_to_csi(lab): + rgb = convert_color(lab, sRGBColor) + colstr = ';2;' + ';'.join((str(i) for i in get_upscaled_values(rgb))) + return colstr + 'm' + + +def get_upscaled_values(rgb): + return [min(max(0, i), 255) for i in rgb.get_upscaled_value_tuple()] + + +def get_rgb(lab): + rgb = convert_color(lab, sRGBColor) + rgb = sRGBColor(*get_upscaled_values(rgb), is_upscaled=True) + return rgb.get_rgb_hex()[1:] + +print(get_rgb(ulab), ':', cur_color[0], ':', get_rgb(cur_color[1])) + +col_1 = lab_to_csi(ulab) +col_2 = lab_to_csi(cur_color[1]) sys.stdout.write('\033[48' + col_1 + '\033[38' + col_2 + 'abc\033[0m <-- bg:urgb, fg:crgb\n') sys.stdout.write('\033[48' + col_2 + '\033[38' + col_1 + 'abc\033[0m <-- bg:crgb, fg:urgb\n')