-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_from_haldclut.py
114 lines (94 loc) · 3.02 KB
/
generate_from_haldclut.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import json
import os
import subprocess
from PIL import Image
CLASSIC_PNG = '/tmp/classic.png'
PROCESSED_PNG = '/tmp/processed.png'
CLUTS_DIR = './cluts'
SCHEMES_DIR = './styles/schemes'
CLUT_THEMES_JSON = './lib/clut-themes.json'
WHITELIST_TXT = './clut_whitelist.txt'
classic_monokai = {
'background': '#272821',
'neutral': '#6e7066',
'foreground': '#fdfff1',
'red': '#f82570',
'orange': '#fc961f',
'yellow': '#e4db73',
'green': '#a6e12d',
'cyan': '#66d9ee',
'purple': '#ae81ff',
}
def hex_to_rgb(hex_color):
return [
int(hex_color[1:3], 16),
int(hex_color[3:5], 16),
int(hex_color[5:7], 16)
]
def rgb_to_hex(rgb_color):
output = '#'
for component in rgb_color:
output += format(component, '02x')
return output
def scheme_to_pixels(scheme):
output = []
for name, hex_color in scheme.items():
output += hex_to_rgb(hex_color)
return bytes(output)
def decamelize_name(name):
output = ''
for idx, x in enumerate(name):
if idx > 0:
prev = name[idx - 1]
if (
prev.isalnum()
and (x.isupper() or x.isdigit())
and prev.islower()
):
output += f' {x}'
continue
output += x
return output
def generate_schemes():
with open(WHITELIST_TXT, 'r') as fp:
whitelist = fp.read().splitlines()
generated_names = []
for sub_directory in os.listdir(CLUTS_DIR):
for clut_filename in os.listdir(
os.path.join(CLUTS_DIR, sub_directory)
):
name = decamelize_name(clut_filename[:-4])
if sub_directory.startswith('CreativePack'):
name = f'CP {name}'
if name not in whitelist:
continue
classic_img = Image.frombytes(
'RGB',
(len(classic_monokai), 1),
scheme_to_pixels(classic_monokai)
)
classic_img.save(CLASSIC_PNG)
cmd = [
'gm', 'convert', CLASSIC_PNG,
'-hald-clut', os.path.join(
CLUTS_DIR, sub_directory, clut_filename
),
PROCESSED_PNG
]
subprocess.Popen(cmd).wait()
processed_image = Image.open(PROCESSED_PNG)
less_scheme = ''
for idx, color_name in enumerate(classic_monokai):
rgb_pixel = processed_image.getpixel((idx, 0))
hex_color = rgb_to_hex(rgb_pixel)
less_scheme += f'@{color_name}: {hex_color};\n'
name_normalized = name.replace(' ', '-').lower()
scheme_filename = f'{name_normalized}.less'
with open(os.path.join(SCHEMES_DIR, scheme_filename), 'w') as fp:
fp.write(less_scheme)
generated_names.append(name)
return generated_names
if __name__ == '__main__':
names = generate_schemes()
with open(CLUT_THEMES_JSON, 'w') as fp:
json.dump(names, fp)