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
|
#!/usr/bin/env python3
# Author: cirakg & claude ai
# Summary: Draw the launcher icon at every size the target watches need, plus the jungle that maps devices to sizes.
"""Draw the app's launcher icon: a white padlock with three "code" dots on a
teal disc, on a transparent background.
The icon is drawn at 8x the target size and scaled down, which gives smooth
edges at the small sizes watches use (38 to 70 px). Every PNG carries the
project's Author and a Description in PNG text chunks.
Two modes:
make_icon.py --size 60 --out FILE.png
One icon of the given size.
make_icon.py --all --devices DIR --manifest manifest.xml
One icon per launcher-icon size used by the products in manifest.xml
(read from each device's compiler.json), written to
resources-icons/<W>x<H>/, plus icons.jungle, which points each product
at its folder. The Monkey C compiler warns when it has to scale an
icon, and warnings fail the build, so every device needs an exact size.
"""
from __future__ import annotations
import argparse
import json
import xml.etree.ElementTree as ET
from collections import defaultdict
from pathlib import Path
from PIL import Image, ImageDraw
from PIL.PngImagePlugin import PngInfo
AUTHOR = "cirakg & claude ai"
DESCRIPTION = "OTP Wallet launcher icon: a padlock with a one-time code, on a teal disc."
TEAL = (14, 124, 134, 255)
WHITE = (255, 255, 255, 255)
SUPERSAMPLE = 8
IQ_NS = "{http://www.garmin.com/xml/connectiq}"
def draw_icon(width: int, height: int | None = None) -> Image.Image:
"""Return the icon as an RGBA image; non-square sizes get a centred square icon."""
height = width if height is None else height
side = min(width, height)
s = side * SUPERSAMPLE
img = Image.new("RGBA", (s, s), (0, 0, 0, 0))
d = ImageDraw.Draw(img)
margin = s * 0.02
d.ellipse((margin, margin, s - margin, s - margin), fill=TEAL)
# Lock body: a rounded rectangle in the lower middle.
body_w, body_h = s * 0.50, s * 0.36
body_x0, body_y0 = (s - body_w) / 2, s * 0.44
d.rounded_rectangle((body_x0, body_y0, body_x0 + body_w, body_y0 + body_h), radius=s * 0.06, fill=WHITE)
# Shackle: the top half of an ellipse plus two short legs into the body.
stroke = int(s * 0.07)
sx0, sx1, sy0, sy1 = s * 0.34, s * 0.66, s * 0.20, s * 0.56
d.arc((sx0, sy0, sx1, sy1), start=180, end=360, fill=WHITE, width=stroke)
leg_top, leg_bottom = (sy0 + sy1) / 2, body_y0 + s * 0.02
d.rectangle((sx0, leg_top, sx0 + stroke, leg_bottom), fill=WHITE)
d.rectangle((sx1 - stroke, leg_top, sx1, leg_bottom), fill=WHITE)
# Three dots on the body stand for a one-time code.
radius = s * 0.035
center_y = body_y0 + body_h * 0.5
for i in range(3):
center_x = s * (0.38 + 0.12 * i)
d.ellipse((center_x - radius, center_y - radius, center_x + radius, center_y + radius), fill=TEAL)
icon = img.resize((side, side), Image.Resampling.LANCZOS)
if (width, height) == (side, side):
return icon
canvas = Image.new("RGBA", (width, height), (0, 0, 0, 0))
canvas.paste(icon, ((width - side) // 2, (height - side) // 2))
return canvas
def save_png(img: Image.Image, path: Path) -> None:
"""Save with the project's Author and Description text chunks."""
info = PngInfo()
info.add_text("Author", AUTHOR)
info.add_text("Description", DESCRIPTION)
path.parent.mkdir(parents=True, exist_ok=True)
img.save(path, format="PNG", optimize=True, pnginfo=info)
def manifest_products(manifest: Path) -> list[str]:
"""Product IDs listed in manifest.xml."""
root = ET.parse(manifest).getroot()
return [p.attrib["id"] for p in root.iter(f"{IQ_NS}product")]
def icon_sizes(devices: Path, products: list[str]) -> dict[tuple[int, int], list[str]]:
"""Group products by the launcher-icon size in their compiler.json."""
groups: dict[tuple[int, int], list[str]] = defaultdict(list)
for product in products:
compiler = json.loads((devices / product / "compiler.json").read_text(encoding="utf-8"))
icon = compiler["launcherIcon"]
groups[(int(icon["width"]), int(icon["height"]))].append(product)
return dict(sorted(groups.items()))
DRAWABLES_XML = """\
<!--
Author: cirakg & claude ai
Summary: Launcher icon at {w}x{h} px for the devices listed in icons.jungle (generated by tools/make_icon.py).
-->
<drawables>
<bitmap id="LauncherIcon" filename="launcher_icon.png" />
</drawables>
"""
JUNGLE_HEADER = """\
# Author: cirakg & claude ai
# Summary: Per-device launcher-icon resources; generated by tools/make_icon.py --all (do not edit).
#
# Each product gets the icon folder matching the launcher-icon size in its
# device definition, so the compiler never has to scale (and warn).
"""
def write_all(devices: Path, manifest: Path, out_dir: Path, jungle: Path) -> None:
groups = icon_sizes(devices, manifest_products(manifest))
lines = [JUNGLE_HEADER]
for (w, h), products in groups.items():
folder = out_dir / f"{w}x{h}"
save_png(draw_icon(w, h), folder / "launcher_icon.png")
(folder / "drawables.xml").write_text(DRAWABLES_XML.format(w=w, h=h), encoding="utf-8")
lines += [f"{p}.resourcePath = $({p}.resourcePath);{folder.as_posix()}" for p in sorted(products)]
print(f"{w}x{h}: {len(products)} devices")
jungle.write_text("\n".join(lines) + "\n", encoding="utf-8")
print(f"wrote {jungle} and {len(groups)} icon folders under {out_dir}")
def main() -> None:
parser = argparse.ArgumentParser(description="Draw the OTP Wallet launcher icon(s).")
parser.add_argument("--size", type=int, default=60, help="single icon: width and height in pixels")
parser.add_argument("--out", type=Path, help="single icon: PNG file to write")
parser.add_argument("--all", action="store_true", help="every size used by the manifest's products")
parser.add_argument("--devices", type=Path, help="device definitions (~/.Garmin/ConnectIQ/Devices)")
parser.add_argument("--manifest", type=Path, default=Path("manifest.xml"))
parser.add_argument("--out-dir", type=Path, default=Path("resources-icons"))
parser.add_argument("--jungle", type=Path, default=Path("icons.jungle"))
args = parser.parse_args()
if args.all:
if args.devices is None:
parser.error("--all needs --devices")
write_all(args.devices, args.manifest, args.out_dir, args.jungle)
return
if args.out is None:
parser.error("give --out (single icon) or --all")
if not 16 <= args.size <= 256:
parser.error("--size must be between 16 and 256")
save_png(draw_icon(args.size), args.out)
print(f"wrote {args.out} ({args.size}x{args.size})")
if __name__ == "__main__":
main()
|