-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsublime_color_scheme_converter.py
More file actions
226 lines (198 loc) · 6.69 KB
/
Copy pathsublime_color_scheme_converter.py
File metadata and controls
226 lines (198 loc) · 6.69 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
SublimeColorSchemeConverter
Converts sublime-color-scheme json files into tmTheme plist files.
"""
import os
import re
import json
import plistlib
import colorsys
import traceback
import collections
import sublime
import sublime_plugin
from SublimeColorSchemeConverter.lib import plistlib
re_var = re.compile("var\([^\)]+\)")
re_alpha = re.compile("alpha\(((0\.)?[0-9]+)\)")
re_hex = re.compile("(#[0-9,a-z,A-Z]{6})([0-9,a-z,A-Z]{2})?")
re_rgb = re.compile("rgb\((\d+),\s?(\d+),\s?(\d+)(,\s?(\d+\.?\d*))?\)")
re_hsl = re.compile("hsl\((\d+),\s?(\d+)%,\s?(\d+)%(,\s?(\d+\.?\d*))?\)")
def alpha_to_hex(a):
return "{:02x}".format(int(255*float(a)))
def hexa_to_hex(hex, a):
return hex[:7] + alpha_to_hex(a)
def rgb_to_hex(r, g, b, a=None):
hexcode = "#{:02x}{:02x}{:02x}".format(r, g, b)
if a:
hexcode += alpha_to_hex(a)
return hexcode
def get_alpha_adjuster(string, default=None):
alpha = re_alpha.search(string)
if alpha:
return float(alpha.group(1))
else:
return default
def get_alpha_hex(hexcode):
if len(hexcode) < 8:
return None
else:
return int(hexcode[7:], 16)
def match_hex(string):
match = re_hex.search(string)
result = None
if match:
result = match.group(1)
alpha = get_alpha_adjuster(string)
if alpha:
result += alpha_to_hex(alpha)
elif match.group(2):
result += match.group(2)
return result
def match_rgb(string):
match = re_rgb.search(string)
result = None
if match:
r = int(match.group(1))
g = int(match.group(2))
b = int(match.group(3))
a = get_alpha_adjuster(string, match.group(5))
result = rgb_to_hex(r, g, b, a)
return result
def match_hsl(string):
match = re_hsl.search(string)
result = None
if match:
h = int(match.group(1))/360
s = int(match.group(2))/100
l = int(match.group(3))/100
a = get_alpha_adjuster(string, match.group(5))
r, g, b = [255*int(v) for v in colorsys.hls_to_rgb(h, l, s)]
result = rgb_to_hex(r, g, b, a)
return result
def try_match_color(string):
hexcode = match_hex(string)
if not hexcode:
hexcode = match_rgb(string)
if not hexcode:
hexcode = match_hsl(string)
if hexcode:
alpha = get_alpha_adjuster(string, get_alpha_hex(hexcode))
if alpha:
hexcode = hexa_to_hex(hexcode[:7], alpha)
else:
hexcode = string
return hexcode
class ConvertSublimeColorSchemeCommand(sublime_plugin.TextCommand):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.filename = None
self.theme = None
self.output = None
self.output_view = None
def convert_name(self, string):
return re.sub(
"_([a-z,A-Z,0-9])",
lambda match: match.group(1).upper(),
string
)
def parse_color(self, key, variables):
if key in list(variables):
return try_match_color(variables[key])
else:
var = re_var.search(key)
if var:
color = variables.get(var.group(), var.group())
return try_match_color(key.replace(var.group(), color))
else:
return try_match_color(key)
def parse_settings(self, settings, variables):
parsed = {}
for key in list(settings):
value = settings[key]
parsed[self.convert_name(key)] = self.parse_color(value, variables)
return parsed
def parse_scope(self, scope):
parsed = self.convert_name(scope)
parsed = re.sub("\s(-|\|)\s", ", ", parsed)
parsed = re.sub("\s*(\(|\))\s*", " ", parsed)
return parsed
def parse_rules(self, rules, variables):
parsed = []
for settings in rules:
rule = {}
name = settings.pop("name", None)
if name:
rule["name"] = name
scope = settings.pop("scope", None)
if scope:
rule["scope"] = self.parse_scope(scope)
rule["settings"] = self.parse_settings(settings, variables)
parsed.append(rule)
return parsed
def parse(self):
name = self.theme.get("name", None)
author = self.theme.get("author", None)
variables = self.theme.get("variables", None)
globals_ = self.theme["globals"]
rules = self.theme["rules"]
for key in list(variables):
variables["var({})".format(key)] = variables.pop(key)
settings = self.parse_settings(globals_, variables)
rules = self.parse_rules(rules, variables)
self.theme = {
"name": name,
"author": author,
"settings": [{"settings": settings}] + rules
}
def convert(self):
error = False
try:
dumps = plistlib.dumps(self.theme, sort_keys=False)
self.output = dumps.decode("UTF-8")
except Exception:
error = True
sublime.error_message("Could not convert Sublime Color Scheme")
print("SublimeColorSchemeConverter:")
print(traceback.format_exc())
return error
def read_source(self):
error = False
self.filename = self.view.file_name()
try:
if self.filename and os.path.exists(self.filename):
with open(self.filename) as sublime_color_scheme:
self.theme = json.load(sublime_color_scheme)
except Exception:
error = True
sublime.error_message("Could not read source")
print("SublimeColorSchemeConverter:")
print(traceback.format_exc())
return error
def write_buffer(self, edit):
error = False
output_name = os.path.splitext(os.path.basename(self.filename))[0] \
+ ".tmTheme"
try:
self.output_view = self.view.window().new_file()
self.output_view.set_encoding("UTF-8")
self.output_view.set_name(output_name)
self.output_view.replace(
edit,
sublime.Region(0, self.view.size()),
self.output
)
self.output = None
except Exception:
error = True
sublime.error_message("Could not write buffer")
print("SublimeColorSchemeConverter:")
print(traceback.format_exc())
return error
def run(self, edit):
if not self.read_source():
if not self.parse():
if not self.convert():
self.write_buffer(edit)
self.filename = None
self.theme = None
self.output = None