Also use CIE2000 in colors_find
This commit is contained in:
parent
e74ca29330
commit
8b084bf60a
|
@ -1,39 +1,58 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
from __future__ import division, print_function
|
||||||
import sys
|
import sys
|
||||||
import os
|
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):
|
def get_lab(name, rgb):
|
||||||
return name, (int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:6], 16))
|
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:
|
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]
|
ulab = get_lab(None, sys.argv[1])[1]
|
||||||
|
|
||||||
|
|
||||||
def col_distance(rgb1, rgb2):
|
|
||||||
return sum(((rgb1[i] - rgb2[i]) ** 2 for i in range(3)))
|
|
||||||
|
|
||||||
|
|
||||||
def find_color(urgb, colors):
|
def find_color(urgb, colors):
|
||||||
cur_distance = 3 * (255 ** 2 + 1)
|
cur_distance = 3 * (255 ** 2 + 1)
|
||||||
cur_color = None
|
cur_color = None
|
||||||
for color, crgb in colors:
|
for color, clab in colors:
|
||||||
dist = col_distance(urgb, crgb)
|
dist = delta_e_cie2000(ulab, clab)
|
||||||
if dist < cur_distance:
|
if dist < cur_distance:
|
||||||
cur_distance = dist
|
cur_distance = dist
|
||||||
cur_color = (color, crgb)
|
cur_color = (color, clab)
|
||||||
return cur_color
|
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'
|
def lab_to_csi(lab):
|
||||||
col_2 = ';2;' + ';'.join((str(i) for i in cur_color[1])) + 'm'
|
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_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')
|
sys.stdout.write('\033[48' + col_2 + '\033[38' + col_1 + 'abc\033[0m <-- bg:crgb, fg:urgb\n')
|
||||||
|
|
Loading…
Reference in New Issue