-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoefilter.py
297 lines (260 loc) · 8.32 KB
/
poefilter.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
from colorsys import rgb_to_hsv, hsv_to_rgb
from contextlib import contextmanager
from inspect import currentframe
global in_block
in_block = 0
global always_continue
always_continue = False
global add_continue
add_continue = False
global condition_stack
condition_stack = []
global block_style
block_style = {}
# Private utility functions
# =========================
def _hex(hex_string):
"""
If the string starts with #, convert it from hex to a PoE color string.
Otherwise, just let the color string pass through.
"""
if hex_string is None:
return None
if hex_string.startswith("#"):
# Taken from https://stackoverflow.com/a/29643643
h = hex_string.lstrip("#")
if len(h) == 6:
t = tuple(int(h[i:i+2], 16) for i in (0, 2, 4))
elif len(h) == 8:
t = tuple(int(h[i:i+2], 16) for i in (0, 2, 4, 6))
else:
raise Exception("Invalid hex format")
return " ".join(str(x) for x in t)
return hex_string
def _rgb_float_to_dec(rgb):
r_in, g_in, b_in = rgb
r_out = int(r_in * 255)
g_out = int(g_in * 255)
b_out = int(b_in * 255)
return "%d %d %d" % (r_out, g_out, b_out)
def _esc(text):
t = str(text)
if not " " in t:
return t
return '"' + t + '"'
def _indent():
global in_block
if in_block:
print(" ", end='')
def _cond(*args):
assert len(args) > 0
result = str(args[0])
if len(args) > 1:
for a in args[1:]:
result += " " + _esc(a)
return result
def _enter_block():
global in_block
global condition_stack
global block_style
global add_continue
assert not in_block
in_block = True
for cond in condition_stack:
_indent()
print(cond)
block_style = {}
add_continue = False
def _exit_block():
global always_continue
global block_style
global add_continue
global in_block
assert in_block
for k, v in block_style.items():
_indent()
print(k + " " + v)
if add_continue or always_continue:
_indent()
print("Continue")
in_block = False
def _apply(vals, text):
global block_style
if vals is None:
return False
key = text.split()[0]
if isinstance(vals, list):
for v in vals:
if v is None:
return False
block_style[key] = " ".join(str(x) for x in vals)
else:
block_style[key] = str(vals)
return True
# Public interface
# ================
class Style:
def __init__(self,
border=None,
text=None,
background=None,
size=None,
alert_sound_id=None,
alert_sound_volume=None,
alert_sound_positional=None,
disable_drop_sound=None,
custom_alert_sound=None,
minimap_icon_size=None,
minimap_icon_color=None,
minimap_icon_shape=None,
play_effect_color=None,
play_effect_temp=None,
warn_if_unused=True):
self.border = _hex(border)
self.text = _hex(text)
self.background = _hex(background)
self.size = size
self.alert_sound_id = alert_sound_id
self.alert_sound_volume = alert_sound_volume
self.alert_sound_positional = alert_sound_positional
self.disable_drop_sound = disable_drop_sound
self.custom_alert_sound = custom_alert_sound
self.minimap_icon_size = minimap_icon_size
self.minimap_icon_color = minimap_icon_color
self.minimap_icon_shape = minimap_icon_shape
self.play_effect_color = play_effect_color
self.play_effect_temp = play_effect_temp
# Print a warning if the style is never used
if warn_if_unused:
self.used = False
self.lineno = currentframe().f_back.f_lineno
else:
self.used = True
def apply(self):
self.used = True
_apply(self.border, "SetBorderColor")
_apply(self.text, "SetTextColor")
_apply(self.background, "SetBackgroundColor")
_apply(self.size, "SetFontSize")
if self.alert_sound_positional is not None and self.alert_sound_positional:
if not _apply([self.alert_sound_id, self.alert_sound_volume], "PlayAlertSoundPositional"):
_apply(self.alert_sound_id, "PlayAlertSoundPositional")
else:
if not _apply([self.alert_sound_id, self.alert_sound_volume], "PlayAlertSound"):
_apply(self.alert_sound_id, "PlayAlertSound")
if self.disable_drop_sound is not None and self.disable_drop_sound:
_indent()
print("DisableDropSound")
_apply(self.custom_alert_sound, "CustomAlertSound")
if not _apply([self.minimap_icon_size, self.minimap_icon_color, self.minimap_icon_shape], "MinimapIcon"):
if self.minimap_icon_size == "-1":
_apply("-1", "MinimapIcon")
if self.play_effect_temp is not None and self.play_effect_temp:
_apply([self.play_effect_color, "Temp"], "PlayEffect")
else:
_apply(self.play_effect_color, "PlayEffect")
def __del__(self):
if not self.used:
print("# unused style defined on line %d" % (self.lineno))
class ColorSeries:
def __init__(self, fg_color, bg_color, tiers, min_sat=0.01, min_val=0.1):
fg_col_str = _hex(fg_color)
bg_col_str = _hex(bg_color)
self.fg_color = [(int(x) / 255.0) for x in fg_col_str.split()]
self.bg_color = [(int(x) / 255.0) for x in bg_col_str.split()]
self.tiers = tiers
self.min_sat = min_sat
self.min_val = min_val
def fg(self, tier):
assert tier >= 0 and tier < self.tiers
h, s, v = rgb_to_hsv(*self.fg_color)
# Desaturate lower tiers
delta = tier / self.tiers
s = self.min_sat * (1.0 - delta) + s * delta
# Lighten higher tiers
missing_val = 1.0 - v
v += delta * missing_val
return _rgb_float_to_dec(hsv_to_rgb(h, s, v))
def bg(self, tier):
assert tier >= 0 and tier < self.tiers
h, s, v = rgb_to_hsv(*self.bg_color)
# Darken lower tiers
delta = tier / self.tiers
v = self.min_val * (1.0 - delta) + v * delta
# Saturate lower tiers
missing_sat = 1.0 - s
s += (1.0 - delta) * missing_sat
# Hue transitions to complementary color for higher tiers
delta *= delta # Transition is nonlinear
opposite = h + 0.5
if opposite >= 1.0:
opposite -= 1.0
h = (1.0 - delta) * h + opposite * delta
return _rgb_float_to_dec(hsv_to_rgb(h, s, v))
def bg_complementary(self, tier):
assert tier >= 0 and tier < self.tiers
h, s, v = rgb_to_hsv(*self.bg_color)
# Darken lower tiers
delta = tier / self.tiers
v = self.min_val * (1.0 - delta) + v * delta
# Saturate lower tiers
missing_sat = 1.0 - s
s += (1.0 - delta) * missing_sat
# Hue transitions to complementary color for higher tiers
delta *= delta # Transition is nonlinear
opposite = h + 0.5
if opposite >= 1.0:
opposite -= 1.0
h = (1.0 - delta) * h + opposite * delta
return _rgb_float_to_dec(hsv_to_rgb(h, s, v))
def set_always_continue(val):
global always_continue
always_continue = val
def begin_show():
print("Show")
_enter_block()
def begin_hide():
print("Hide")
_enter_block()
def end():
_exit_block()
print("")
def cont():
global add_continue
add_continue = True
def condition(*args):
"""Add a condition to the active block"""
global in_block
assert in_block
_indent()
print(_cond(*args))
@contextmanager
def show():
begin_show()
try:
yield None
finally:
end()
@contextmanager
def hide():
begin_hide()
try:
yield None
finally:
end()
@contextmanager
def conditions(*args):
"""Expects each argument to be a tuple defining one condition"""
global condition_stack
global in_block
assert not in_block
num_to_pop = len(args)
for a in args:
condition_stack.append(_cond(*a))
try:
yield None
finally:
assert not in_block
while num_to_pop:
condition_stack.pop()
num_to_pop -= 1