forked from xthezealot/white-theme-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·179 lines (143 loc) · 5.49 KB
/
build.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
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
#!/usr/bin/env python3
"""Generate Day 'n' Nite Light and Day 'n' Nite Dark themes."""
from __future__ import annotations
from copy import deepcopy
from typing import Any, Dict, Union, Type
import definitions
import json
class Color(object):
"""Color defines a color.
Args:
hex6 (str): The 6 digits hexadecimal representation.
alpha (float, optional): The alpha value between 0 and 1. Defaults to 1.
Raises:
ValueError: The representation is not a 6 digits hexadecimal number.
"""
def __init__(self, hex6: str, alpha: float = 1):
if hex6.startswith("#"):
hex6 = hex6[1:]
if len(hex6) != 6:
raise ValueError("Hexadecimal value must have 6 chars")
hex6 = hex6.lower()
self._rgb = int(hex6, 16)
self.alpha = alpha
@property
def alpha(self) -> float:
"""Get the alpha of the color, always between 0 and 1.
Returns:
float: The alpha value.
"""
return self._alpha
@alpha.setter
def alpha(self, v: float):
"""Get the alpha of the color.
Args:
v (float): The alpha value.
Raises:
ValueError: The value is not between 0 and 1.
"""
if not 0 <= v <= 1:
raise ValueError("Alpha must be between 0 and 1")
self._alpha = v
def __str__(self) -> str:
"""Get the shortest hexadecimal color value.
Returns:
str: The hexadecimal representation.
"""
s = self.to_hex()
if self.alpha < 1:
s += "{:02x}".format(int(self.alpha * 255))
if s[0] == s[1] and s[2] == s[3] and s[4] == s[5]:
if len(s) == 6:
s = s[0] + s[2] + s[4]
elif s[6] == s[7]:
s = s[0] + s[2] + s[4] + s[6]
return "#" + s
def to_hex(self) -> str:
return "{:06x}".format(self._rgb)
class ColorSplit(object):
"""Contains colors for Light and Dark Day 'n' Nite themes.
Args:
white (Union[str, ColorSplit]): The White theme color from an hexadecimal
representation or another color split.
white_night (Union[str, ColorSplit], optional): The White Night theme color from
an hexadecimal representation or another color split. If None, the White
version is used. Defaults to None.
"""
def __init__(
self, white: Union[str, ColorSplit], white_night: Union[str, ColorSplit] = None
):
if isinstance(white, ColorSplit):
self.white: Color = deepcopy(white.white)
else:
self.white = Color(white)
if white_night:
if isinstance(white_night, ColorSplit):
self.white_night: Color = deepcopy(white_night.white_night)
else:
self.white_night = Color(white_night)
else:
self.white_night = deepcopy(self.white)
def set_alpha(self, white: float, white_night: float = None) -> ColorSplit:
"""Set alpha values of colors.
Args:
white (float): The alpha value for the White theme.
white_night (float, optional): The alpha value for the White Night theme. If
None, the White version is used. Defaults to None.
Returns:
ColorSplit: The same color split.
"""
self.white.alpha = white
self.white_night.alpha = white_night if white_night else white
return self
def with_alpha(self, white: float, white_night: float = None) -> ColorSplit:
"""Copy colors split and set alpha values of colors.
Args:
white (float): The alpha value for the White theme.
white_night (float, optional): The alpha value for the White Night theme. If
None, the White version is used. Defaults to None.
Returns:
ColorSplit: A color split copy.
"""
color_split = deepcopy(self)
color_split.set_alpha(white, white_night)
return color_split
def to_reversed(self) -> ColorSplit:
"""Copy colors split and set values of colors in reversed order."""
color_split_reversed = ColorSplit(
self.white_night.to_hex(), self.white.to_hex()
)
color_split = deepcopy(color_split_reversed)
return color_split
def forge(defs: Dict[str, Any], night: bool = False):
"""Replace color splits with plain strings.
Args:
defs (Dict[str, Any]): The definitions needed to be forged.
night (bool, optional): Wether forge for the normal theme or its night version.
Defaults to False.
"""
for d in defs:
if isinstance(defs[d], dict):
forge(defs[d], night)
elif isinstance(defs[d], list):
for i, v in enumerate(defs[d]):
if isinstance(v, dict):
forge(defs[d][i], night)
elif isinstance(defs[d], definitions.ColorSplit):
defs[d] = str(defs[d].white) if not night else str(defs[d].white_night)
def dump(night: bool = False):
"""Write definitions to JSON files.
Args:
night (bool, optional): Wether dump for the normal theme or its night version.
Defaults to False.
"""
filepath = "themes/Day-n-nite-{}-color-theme.json".format(
"Dark" if night else "Light",
)
with open(filepath, "w") as f:
defs = deepcopy(definitions.definitions)
forge(defs, night)
json.dump(defs, f, indent=4)
if __name__ == "__main__":
dump()
dump(True)