powerline/tools/colors_find.py

64 lines
1.6 KiB
Python
Raw Normal View History

2013-03-19 05:20:40 +01:00
#!/usr/bin/env python
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function)
import sys
import os
2014-07-06 09:55:54 +02:00
from colormath.color_objects import sRGBColor, LabColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
2014-07-06 09:55:54 +02:00
def get_lab(name, rgb):
rgb = sRGBColor(
int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:6], 16),
is_upscaled=True
)
2014-07-06 09:55:54 +02:00
lab = convert_color(rgb, LabColor)
return name, lab
with open(os.path.join(os.path.dirname(__file__), 'colors.map'), 'r') as f:
2014-07-06 09:55:54 +02:00
colors = [get_lab(*line.split('\t')) for line in f]
2014-07-06 09:55:54 +02:00
ulab = get_lab(None, sys.argv[1])[1]
def find_color(urgb, colors):
cur_distance = 3 * (255 ** 2 + 1)
cur_color = None
2014-07-06 09:55:54 +02:00
for color, clab in colors:
dist = delta_e_cie2000(ulab, clab)
if dist < cur_distance:
cur_distance = dist
2014-07-06 09:55:54 +02:00
cur_color = (color, clab)
return cur_color
2014-07-06 09:55:54 +02:00
cur_color = find_color(ulab, colors)
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:]
2014-07-06 09:55:54 +02:00
print(get_rgb(ulab), ':', cur_color[0], ':', get_rgb(cur_color[1]))
2014-07-06 09:55:54 +02:00
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')