-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy paththeme_generator.py
143 lines (121 loc) · 4.21 KB
/
theme_generator.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
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
import os
import json
import pyte
from copy import deepcopy
from collections import OrderedDict
TEMPLATE = OrderedDict(
name="Terminus",
variables=OrderedDict(),
globals=OrderedDict()
)
def next_color(color_text):
"""
Given a color string "#xxxxxy", returns its next color "#xxxx{xy+1}".
"""
hex_value = int(color_text[5:], 16)
if hex_value == 255: # ff
return color_text[:5] + "fe"
else:
return color_text[:5] + "{:2x}".format(hex_value + 1).replace(" ", "0")
ANSI_COLORS = [
"black",
"red",
"green",
"brown",
"blue",
"magenta",
"cyan",
"white",
"light_black",
"light_red",
"light_green",
"light_brown",
"light_blue",
"light_magenta",
"light_cyan",
"light_white"
]
def generate_theme_file(
path, variables={}, globals={}, ansi_scopes=True, color256_scopes=False, pretty=True):
COLOR_SCHEME = deepcopy(TEMPLATE)
_colors16 = OrderedDict()
for i in range(16):
_colors16[ANSI_COLORS[i]] = "#{}".format(pyte.graphics.FG_BG_256[i])
if variables:
if "caret" not in variables and "foreground" in variables:
variables["caret"] = variables["foreground"]
# make sure the variables are in order
COLOR_SCHEME["variables"].update(variables)
COLOR_SCHEME["variables"].update(_colors16)
COLOR_SCHEME["variables"].update(variables)
if globals:
COLOR_SCHEME["globals"].update(globals)
# There is a bug/feature of add_regions
# if the background of a scope is exactly the same as the background of the theme.
# The foregound and background colors would be inverted. check
# https://github.com/SublimeTextIssues/Core/issues/817
if "background" in COLOR_SCHEME["variables"]:
background = COLOR_SCHEME["variables"]["background"]
COLOR_SCHEME["variables"]["background"] = next_color(background)
COLOR_SCHEME["globals"]["background"] = background
else:
background = None
for key, value in COLOR_SCHEME["variables"].items():
if key == "background":
continue
if value == background:
COLOR_SCHEME["variables"][key] = next_color(value)
colors = OrderedDict()
if ansi_scopes:
colors.update(_colors16)
colors["default"] = "#default"
colors["reverse_default"] = "#reverse_default"
if color256_scopes:
for i, rgb in enumerate(pyte.graphics.FG_BG_256):
colors[rgb] = "#{}".format(rgb)
if colors:
COLOR_SCHEME["rules"] = []
for u, ucolor in colors.items():
for v, vcolor in colors.items():
if u in ANSI_COLORS:
ucolor = "var({})".format(u)
elif ucolor == "#default":
ucolor = "var(foreground)"
elif ucolor == "#reverse_default":
ucolor = "var(background)"
if v in ANSI_COLORS:
vcolor = "var({})".format(v)
elif vcolor == "#default" or vcolor == background:
vcolor = "var(background)"
elif vcolor == "#reverse_default":
vcolor = "var(foreground)"
rule = {}
rule["scope"] = "terminus.{}.{}".format(u, v)
rule["foreground"] = ucolor
rule["background"] = vcolor
COLOR_SCHEME["rules"].append(rule)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
if pretty:
f.write(json.dumps(COLOR_SCHEME, indent=4))
else:
f.write(json.dumps(COLOR_SCHEME))
if __name__ == "__main__":
path = os.path.join(os.path.dirname(__file__), "..", "Terminus.hidden-color-scheme")
variables = {
"background": "#262626",
"foreground": "#ffffff",
"caret": "white",
"selection": "#444444",
"selection_foreground": "#ffffff"
}
globals = {
"background": "var(background)",
"foreground": "var(foreground)",
"caret": "var(caret)",
"selection": "var(selection)",
"selection_foreground": "var(selection_foreground)",
"selection_corner_style": "square",
"selection_border_width": "0"
}
generate_theme_file(path, variables=variables, globals=globals)