-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
130 lines (100 loc) · 5.2 KB
/
convert.py
File metadata and controls
130 lines (100 loc) · 5.2 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import math
import re
import sys
class ColourFormat:
def __init__(self, pattern, parse, stringify):
self.pattern = pattern
self.parse = parse
self.stringify = stringify
class Colour:
formats = {
"#hex": ColourFormat(
r'#[0-9a-f]{6}',
lambda text: tuple(map(lambda value_text: int(value_text, base=16), (text[i:i + 2]
for i in range(1, 7, 2)))),
lambda rgb: ("#" + "{:02X}" * 3).format(*rgb)
),
"hex_with_quotes": ColourFormat(
r'"[0-9a-f]{6}"',
lambda text: tuple(map(lambda value_text: int(value_text, base=16), (text[i:i + 2]
for i in range(1, 7, 2)))),
lambda rgb: ("\"" + "{:02X}" * 3 + "\"").format(*rgb)
),
"rgba_decimal": ColourFormat(
' '.join([r'\d(?:\.\d+)?'] * 3),
lambda text: tuple(map(lambda value: float(value) * 255, text.split(' ')[0:3])),
lambda rgb: ' '.join(["{:g}"] * 3).format(*(value / 255 for value in rgb))
),
}
def __init__(self, colour_text, colour_format):
self.original_text = colour_text
self.rgb = self.formats.get(colour_format, self.formats["#hex"]).parse(colour_text)
def __eq__(self, other):
return all(value == another_value
for (value, another_value) in zip(self.rgb, other.rgb))
def __repr__(self):
return "Colour(original_text={}, rgb={})".format(self.original_text, self.rgb)
def __sub__(self, other):
return ColourDistance(self, other)
def __format__(self, colour_format):
return self.formats[colour_format].stringify(self.rgb)
class ColourDistance:
types = {
"euclidean": lambda differences: math.sqrt(sum(tuple(math.pow(difference, 2) for difference in differences))),
"manhattan": lambda differences: sum(tuple(abs(difference) for difference in differences)),
"uniform": lambda differences: max(tuple(abs(difference) for difference in differences)),
}
def __init__(self, colour, other_colour):
self.rgb_differences = tuple((value - other_value)
for (value, other_value) in zip(colour.rgb, other_colour.rgb))
def __eq__(self, other):
return all(value == other_value
for (value, other_value) in zip(self.rgb_differences, other.rgb_differences))
def __getitem__(self, distance_type):
return self.types.get(distance_type, self.types["euclidean"])(self.rgb_differences)
def find_nearest_colour(colours_to_find, origin_colour, distance_type):
return min(colours_to_find, key=lambda colour_to_find: (origin_colour - colour_to_find)[distance_type])
def substitute(theme, theme_colour_to_palette_colour):
return re.compile('|'.join(theme_colour_to_palette_colour.keys())).sub(
lambda matched_colour: theme_colour_to_palette_colour[matched_colour.group()], theme)
def read_theme_colours(theme, theme_colour_format):
colour_pattern = Colour.formats.get(theme_colour_format, Colour.formats["#hex"]).pattern
return set(re.compile(colour_pattern, re.IGNORECASE).findall(theme))
def read_palette_colours(palette):
return palette.splitlines()
def convert(theme="", theme_colour_format="", palette="", distance_type=""):
if palette:
palette_colours = list(map(lambda palette_colour_text: Colour(palette_colour_text, "#hex"),
read_palette_colours(palette)))
theme_colours = list(map(lambda theme_colour_text: Colour(theme_colour_text, theme_colour_format),
read_theme_colours(theme, theme_colour_format)))
theme_colour_to_palette_colour = {
theme_colour.original_text:
format(find_nearest_colour(palette_colours, theme_colour, distance_type), theme_colour_format)
for theme_colour in theme_colours
}
return substitute(theme, theme_colour_to_palette_colour)
return theme
def get_colour_format(file_name_extension):
file_name_extension_to_colour_format = {
"xccolortheme": "rgba_decimal",
"icls": "hex_with_quotes"
}
return file_name_extension_to_colour_format.get(file_name_extension, "#hex")
def main():
palette_file_name = sys.argv[1]
theme_file_name = sys.argv[2]
for distance_type in ColourDistance.types.keys():
theme_file_name_parts = theme_file_name.split('.')
theme_file_name_extension = theme_file_name_parts[-1]
converted_theme_file_name = '.'.join(theme_file_name_parts[0:-1] + [distance_type, theme_file_name_extension])
with open(palette_file_name, 'r') as palette_file, \
open(theme_file_name, 'r') as theme_file, \
open(converted_theme_file_name, 'w') as converted_theme_file:
converted_theme_file.write(
convert(theme=theme_file.read(),
theme_colour_format=get_colour_format(theme_file_name_extension),
palette=palette_file.read(),
distance_type=distance_type))
if __name__ == '__main__':
main()