forked from sublhighlight/sublhighlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsublcolorscheme.py
More file actions
176 lines (164 loc) · 5.31 KB
/
sublcolorscheme.py
File metadata and controls
176 lines (164 loc) · 5.31 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
import os
import re
import yaml
import tinycss2
import tinycss2.color3
from colorsys import hls_to_rgb, rgb_to_hls
from scsast import parserulescope
file_ext = "sublime-color-scheme"
color_scheme_dir_path = os.path.join(
os.path.dirname(__file__) or ".",
"color-scheme"
)
all_color_schemes_basenames = list(
filter(
lambda x:x.endswith(file_ext),
os.listdir(color_scheme_dir_path)
)
)
all_color_schemes_names = list(
map(
lambda x: os.path.splitext(x)[0],
all_color_schemes_basenames
)
)
all_color_schemes_paths = list(
map(
lambda x: os.path.abspath(
os.path.join(
color_scheme_dir_path,
x
)
),
all_color_schemes_basenames
)
)
# yaml load error workaround, see: https://github.com/yaml/pyyaml/issues/89
def construct_value(load, node):
if not isinstance(node, yaml.ScalarNode):
raise yaml.constructor.ConstructorError(
"while constructing a value",
node.start_mark,
"expected a scalar, but found %s" % node.id, node.start_mark
)
yield str(node.value)
yaml.SafeLoader.add_constructor(u'tag:yaml.org,2002:value', construct_value)
# end yaml load error workaround
def loadcolorscheme(path):
with open(path, "rb") as f:
content = f.read()
# yes, json can be parsed as yaml with support for trailing commas!
# unfortunately yaml will not take // as a line comment...
try:
return yaml.load(content, Loader=yaml.SafeLoader)
except yaml.parser.ParserError:
re_dbl_qt_str = re.compile(br"\"[^\"]*\"")
cont_strmap = {b"${%d}" % i:v for i,v in enumerate(list(set(re_dbl_qt_str.findall(content))))}
for k, v in cont_strmap.items():
content = content.replace(v, k)
content = content.replace(b"//", b"#")
for k, v in cont_strmap.items():
content = content.replace(k, v)
return yaml.load(content, Loader=yaml.SafeLoader)
def parsecolorscheme(scheme):
glob = scheme["globals"]
_var = scheme["variables"]
rules = scheme["rules"]
try:
for name in _var:
_var[name] = evalexpr(_var, _var[name])
for key in glob:
glob[key] = evalexpr(_var, glob[key])
for rule in rules:
rule["scope"] = parserulescope(rule["scope"])
if "foreground" in rule:
rule["foreground"] = evalexpr(_var, rule["foreground"])
if "background" in rule:
rule["background"] = evalexpr(_var, rule["background"])
except:
print(f"parsecolorscheme: {scheme['name']}")
raise
return scheme
def evalexpr(_var, expr):
def evalfunc(_var, compo):
args = list(filter(lambda x:x.type != "whitespace", compo.arguments))
if not args:
raise ValueError(f"expecting args in function: {compo.name}")
if compo.name == "var":
return _var[args[0].value]
elif compo.name == "color":
if args[0].type == "function":
color = evalfunc(_var, args[0])
else:
color = tinycss2.color3.parse_color(args[0].value)
if color is None:
raise ValueError(f"invalid color spec in function: {compo.name}")
for i in range(1, len(args)):
subcompo = args[i]
if subcompo.type == "function":
subargs = list(filter(lambda x:x.type != "whitespace", subcompo.arguments))
if not subargs:
raise ValueError(f"expecting args in function: {subcompo.name}")
if subcompo.name in ("alpha", "a"):
color = tinycss2.color3.RGBA(
color.red,
color.green,
color.blue,
float(subargs[0].value)
)
elif subcompo.name in ("saturation", "s"):
_hls = rgb_to_hls(color.red, color.green, color.blue)
_rgb = hls_to_rgb(_hls[0], _hls[1], float(subargs[0].value))
color = tinycss2.color3.RGBA(*_rgb, color.alpha)
elif subcompo.name in ("lightness", "l"):
_hls = rgb_to_hls(color.red, color.green, color.blue)
_rgb = hls_to_rgb(_hls[0], float(subargs[0].value), _hls[2])
color = tinycss2.color3.RGBA(*_rgb, color.alpha)
elif subcompo.name == "blend":
blendcolor = tinycss2.color3.parse_color(subargs[0].value)
if blendcolor is None:
raise ValueError(f"invalid color spec in function: {subcompo.name}")
t = subargs[1].value/100.0
invt = 1.0 - t
color = tinycss2.color3.RGBA(
color.red * invt + blendcolor.red * t,
color.green * invt + blendcolor.green * t,
color.blue * invt + blendcolor.blue * t,
color.alpha
)
elif subcompo.name == "blenda":
blendcolor = tinycss2.color3.parse_color(subargs[0].value)
if blendcolor is None:
raise ValueError(f"invalid color spec in function: {subcompo.name}")
t = subargs[1].value/100.0
invt = 1.0 - t
color = tinycss2.color3.RGBA(
color.red * invt + blendcolor.red * t,
color.green * invt + blendcolor.green * t,
color.blue * invt + blendcolor.blue * t,
color.alpha * invt + blendcolor.alpha * t
)
elif subcompo.name == "min-contrast":
# ignored for now, sorry...
pass
else:
raise ValueError(f"unknown function: {subcompo.name}")
return color
else:
raise ValueError(f"unknown function: {compo.name}")
def _doeval(expr):
try:
color = tinycss2.color3.parse_color(expr)
if color is not None:
return color
components = tinycss2.parse_component_value_list(expr)
for compo in components:
if compo.type == "function":
return evalfunc(_var, compo)
return expr
except:
print(f"in expr: {expr}")
raise
if isinstance(expr, list):
return list(map(_doeval, expr))
return _doeval(expr)