Skip to content

Commit b2e71b6

Browse files
committed
Use oxipng and svgo in generate-icons.py, convert it to Python 3 too
1 parent 5a8f971 commit b2e71b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+65
-2613
lines changed

generate-icons.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#!/usr/bin/env python3
22
# Used to generate some icons
3-
# Requires inkscape and imagemagick pacages
4-
5-
import os, subprocess, colorsys
3+
# Requires inkscape and imagemagick packages
4+
import subprocess
5+
import colorsys
6+
import oxipng
67
from xml.etree import ElementTree as ET
78

8-
ICODIR = "./images/" # Directory with icons
9-
CICONS = "./images/controller-icons/" # Directory controller-icons
10-
RECOLORS = { # Defines set of hue shifts for controller-icons
9+
ICODIR = "./images/" # Directory with icons
10+
CICONS = "./images/controller-icons/" # Directory controller-icons
11+
RECOLORS = {
12+
# Defines set of hue shifts for controller-icons
1113
# "0" : 0.0, # Green - original
1214
"1" : 0.3, # Blue
1315
"2" : 0.7, # Red
@@ -21,17 +23,17 @@
2123
# Generate svg state icons
2224
for size in (24, 256):
2325
for state in ('alive', 'dead', 'error', 'unknown'):
24-
print("scc-statusicon-%s.png" % (state,))
26+
print(f"scc-statusicon-{state}.png")
2527
subprocess.call([
2628
"inkscape",
27-
"%s/scc-statusicon-%s.svg" % (ICODIR, state),
29+
f"{ICODIR}scc-statusicon-{state}.svg",
2830
"--export-area-page",
29-
"--export-png=%s/%sx%s/status/scc-%s.png" % (ICODIR, size, size, state),
30-
"--export-width=%s" % (size,),
31-
"--export-height=%s" % (size,) ])
32-
31+
f"--export-filename={ICODIR}{size}x{size}/status/scc-{state}.png",
32+
f"--export-width={size}",
33+
f"--export-height={size}"])
34+
oxipng.optimize(f"{ICODIR}{size}x{size}/status/scc-{state}.png", level=6, deflate=oxipng.Deflaters.zopfli(100))
3335

34-
def html_to_rgb(html):
36+
def html_to_rgb(html: str) -> tuple[int,int,int,int]:
3537
""" Converts #rrggbbaa or #rrggbb to r, g, b,a in (0,1) ranges """
3638
html = html.strip("#")
3739
if len(html) == 6:
@@ -40,15 +42,15 @@ def html_to_rgb(html):
4042
return 0, 0, 0, 0
4143
elif len(html) != 8:
4244
raise ValueError("Needs RRGGBB(AA) format, got '%s'" % (html, ))
43-
return tuple(( float(int(html[i:i+2],16)) / 255.0 for i in xrange(0, len(html), 2) ))
45+
return tuple(( float(int(html[i:i+2],16)) / 255.0 for i in range(0, len(html), 2) ))
4446

4547

46-
def rgb_to_html(r,g,b):
48+
def rgb_to_html(r,g,b) -> str:
4749
""" Convets rgb back to html color code """
4850
return "#" + "".join(( "%02x" % int(x * 255) for x in (r,g,b) ))
4951

5052

51-
def recolor(tree, add):
53+
def recolor(tree, add) -> None:
5254
""" Recursive part of recolor_strokes and recolor_background """
5355
if 'id' in tree.attrib and "overlay" in tree.attrib['id']:
5456
return
@@ -68,7 +70,8 @@ def recolor(tree, add):
6870
h,s,v = colorsys.rgb_to_hsv(r,g,b)
6971
# Shift hue
7072
h += add
71-
while h > 1.0 : h -= 1.0
73+
while h > 1.0:
74+
h -= 1.0
7275
# Convert it back
7376
r,g,b = colorsys.hsv_to_rgb(h,s,v)
7477
# Store
@@ -80,13 +83,18 @@ def recolor(tree, add):
8083
ET.register_namespace("","http://www.w3.org/2000/svg")
8184
for tp in ("sc", "scbt", "fake", "ds4", "hid", "rpad"):
8285
# Read svg and parse it
83-
data = file("%s/%s-0.svg" % (CICONS, tp), "r").read()
86+
data = open(f"{CICONS}{tp}-0.svg", "r").read()
8487
# Create recolored images
8588
for key in RECOLORS:
8689
tree = ET.fromstring(data)
8790
# Walk recursively and recolor everything that has color
8891
recolor(tree, RECOLORS[key])
89-
90-
out = "%s/%s-%s.svg" % (CICONS, tp, key)
91-
file(out, "w").write(ET.tostring(tree))
92+
93+
out = f"{CICONS}{tp}-{key}.svg"
94+
with open(out, "w") as file:
95+
file.write(ET.tostring(tree).decode('utf-8'))
96+
subprocess.call([
97+
"svgo",
98+
"--multipass",
99+
f"--input={out}"])
92100
print(out)

images/24x24/status/scc-alive.png

-22 Bytes
Loading

images/24x24/status/scc-dead.png

-11 Bytes
Loading

images/24x24/status/scc-error.png

-20 Bytes
Loading

images/24x24/status/scc-unknown.png

-4 Bytes
Loading

images/256x256/status/scc-alive.png

418 Bytes
Loading

images/256x256/status/scc-dead.png

466 Bytes
Loading

images/256x256/status/scc-error.png

291 Bytes
Loading

images/256x256/status/scc-unknown.png

-104 Bytes
Loading

images/controller-icons/ds4-1.svg

Lines changed: 1 addition & 81 deletions
Loading

images/controller-icons/ds4-2.svg

Lines changed: 1 addition & 81 deletions
Loading

images/controller-icons/ds4-3.svg

Lines changed: 1 addition & 81 deletions
Loading

images/controller-icons/ds4-4.svg

Lines changed: 1 addition & 81 deletions
Loading

images/controller-icons/ds4-5.svg

Lines changed: 1 addition & 81 deletions
Loading

images/controller-icons/ds4-6.svg

Lines changed: 1 addition & 81 deletions
Loading

images/controller-icons/fake-1.svg

Lines changed: 1 addition & 41 deletions
Loading

images/controller-icons/fake-2.svg

Lines changed: 1 addition & 41 deletions
Loading

0 commit comments

Comments
 (0)