-
Notifications
You must be signed in to change notification settings - Fork 1
/
svgfns.py
31 lines (24 loc) · 939 Bytes
/
svgfns.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
import re
# ~---------------------------
def color_element(root, pattern, color, opacity):
print(f'Replacing with {color}')
print(f'Using opacity {opacity}')
try:
element = root.find(f'.//*[@id="{pattern}"]')
#print(element)
except:
print(f'!! FAILED coloring at element "{pattern}"')
try:
current_style = element.get('style')
#print(current_style)
new_style = re.sub('#[0-9A-Fa-f]{6}', color, current_style, 1)
except:
print(f'!! Failed regex substitution while coloring "{pattern}"')
try:
new_style = re.sub(r'fill-opacity:[0-1][.0-9]+', 'fill-opacity:'+str(opacity), new_style, 1)
except:
print(f'!! FAILED regex substitution while changing opacity on "{pattern}"')
element.set('style', new_style)
print(f'SUCCESS coloring {pattern}')
return root
# ~---------------------------