-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·95 lines (83 loc) · 3.29 KB
/
generate.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3
import json
import numpy as np
from argparse import ArgumentParser
from terminaltables import SingleTable
from colormath.color_objects import sRGBColor, LabColor, HSVColor, HSLColor, XYZColor
from colormath.color_conversions import convert_color
from colormath.color_diff import delta_e_cie2000
XTERM = []
with open('xterm.json') as file:
schemeRaw = json.load(file)
for entry in schemeRaw:
XTERM.append({
'id':
entry['colorId'],
'color':
convert_color(sRGBColor.new_from_rgb_hex(entry['hexString']),
LabColor),
'name':
entry['name']
})
def fmtHex(str):
black = convert_color(sRGBColor(0, 0, 0), LabColor)
white = convert_color(sRGBColor(1, 1, 1), LabColor)
color = sRGBColor.new_from_rgb_hex(str)
lcolor = convert_color(color, LabColor)
if delta_e_cie2000(lcolor, white) > delta_e_cie2000(lcolor, black):
return "\033[48;2;{};{};{};38;2;255;255;255m{}\033[0m".format(
int(255 * color.rgb_r), int(255 * color.rgb_g),
int(255 * color.rgb_b), color.get_rgb_hex())
else:
return "\033[48;2;{};{};{};38;2;0;0;0m{}\033[0m".format(
int(255 * color.rgb_r), int(255 * color.rgb_g),
int(255 * color.rgb_b), color.get_rgb_hex())
def closestXTerm(color):
color = convert_color(color, LabColor)
des = [delta_e_cie2000(color, x['color']) for x in XTERM]
return XTERM[np.argmin(des)]
def colorCodes(color):
rgb = convert_color(color, sRGBColor)
hsv = convert_color(color, HSVColor)
lab = convert_color(color, LabColor)
xcolor = closestXTerm(color)
return {
'hex': rgb.get_rgb_hex(),
'lab': lab,
'rgb': rgb,
'hsv': hsv,
'xhex': convert_color(xcolor['color'], sRGBColor).get_rgb_hex(),
'xid': xcolor['id'],
'xname': xcolor['name']
}
def detailTable(colors):
print("\033[1m{:7} {:20} {:3} {:7} {:22} {:20} {:21}\033[0m".format(
"HEX", "NAME", "ID", "XHEX", "L*A*B", "RGB", "HSV"))
for color in colors:
codes = colorCodes(color)
print(
"{:7} {:20} {:3} {:7} {: 6.3f} {: 7.3f} {: 7.3f} {:6.3f} {:6.3f} {:6.3f} {:7.3f} {:6.3f} {:6.3f}"
.format(fmtHex(codes['hex']), codes['xname'], codes['xid'],
fmtHex(codes['xhex']), codes['lab'].lab_l,
codes['lab'].lab_a, codes['lab'].lab_b, codes['rgb'].rgb_r,
codes['rgb'].rgb_g, codes['rgb'].rgb_b, codes['hsv'].hsv_h,
codes['hsv'].hsv_s, codes['hsv'].hsv_v))
BASEA = convert_color(sRGBColor.new_from_rgb_hex("#263238"), LabColor)
BASEB = convert_color(sRGBColor.new_from_rgb_hex("#eceff1"), LabColor)
L_STEPS = 8
monochrome = [
LabColor(np.interp(x, [0, 1], [BASEA.lab_l, BASEB.lab_l]),
np.interp(x, [0, 1], [BASEA.lab_a, BASEB.lab_a]),
np.interp(x, [0, 1], [BASEA.lab_b, BASEB.lab_b]))
for x in np.linspace(0, 1, L_STEPS)
]
detailTable(monochrome)
avgMonL = np.mean([x.lab_l for x in monochrome])
colors = []
HUE_BASE = convert_color(sRGBColor.new_from_rgb_hex("#ff0000"), HSVColor)
HUE_STEPS = [19, 28, 14, 76, 37, 61, 35]
h = 0
for step in HUE_STEPS:
colors.append(HSVColor(HUE_BASE.hsv_h + h, HUE_BASE.hsv_s, HUE_BASE.hsv_v))
h += step
detailTable(colors)