File: colors_find.py

package info (click to toggle)
powerline 2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,828 kB
  • sloc: python: 22,536; sh: 1,789; ansic: 131; makefile: 74; csh: 51
file content (63 lines) | stat: -rwxr-xr-x 1,671 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, 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_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_lab(*line.split('\t')) for line in f]


ulab = get_lab(None, sys.argv[1])[1]


def find_color(urgb, colors):
	cur_distance = 3 * (255 ** 2 + 1)
	cur_color = None
	for color, clab in colors:
		dist = delta_e_cie2000(ulab, clab)
		if dist < cur_distance:
			cur_distance = dist
			cur_color = (color, clab)
	return cur_color


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:]

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')