Files
Bela/tools/make_icon.py
T
TMCandClaude Opus 5.5 90acbcb4e6 Bela v0: local game against bots, counting training, settings with self-update
- engine: Croatian Bela rules (deal, trump call, declarations, play rules, scoring),
  heuristic bot, counting drill; unit tests and a 1000-game simulation
- app: Jetpack Compose table with partner and two bot opponents, Hungarian deck,
  card-counting training, settings with in-app update and a QR code for sharing
- tools: card import/render/upscale scripts and the launcher icon generator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:40:57 +01:00

92 lines
4.4 KiB
Python

"""
Turn icon.png (square artwork, full bleed) into the Android launcher icon and the in-app logo.
python tools/make_icon.py
Android adaptive icons are 108 dp layers that the launcher masks to a circle, squircle or rounded square,
showing only about the middle 72 dp. Placed full size, the artwork would lose the "B" and the card corners.
So the artwork is scaled to fit the mask, and the space around it is filled with a blurred, enlarged copy of
plain felt in the artwork's edge colour, blended in with soft edges so no square seam shows.
Writes:
app/src/main/res/mipmap-<density>/ic_launcher_foreground.webp (adaptive icon, all densities)
app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml (adaptive icon definition)
app/src/main/res/values/ic_launcher_background.xml (colour behind the layer)
app/src/main/res/drawable-nodpi/logo.webp (the artwork for the home screen)
"""
from pathlib import Path
from PIL import Image, ImageFilter
ROOT = Path(__file__).resolve().parent.parent
SOURCE = ROOT / "icon.png"
RES = ROOT / "app" / "src" / "main" / "res"
DENSITIES = {"mdpi": 1.0, "hdpi": 1.5, "xhdpi": 2.0, "xxhdpi": 3.0, "xxxhdpi": 4.0}
LAYER_DP = 108
ART_DP = 68 # artwork size inside the 108 dp layer; its content then stays inside a 72 dp circle
FEATHER = 0.03 # fraction of the artwork, at its edges, faded into the surround
LOGO_PX = 900
def edge_colour(art: Image.Image) -> tuple:
"""Average colour of the artwork's outer border: the felt the surround has to continue."""
w, h = art.size
band = max(2, w // 60)
strips = [art.crop((0, 0, w, band)), art.crop((0, h - band, w, h)), art.crop((0, 0, band, h)), art.crop((w - band, 0, w, h))]
means = [strip.resize((1, 1), Image.BOX).getpixel((0, 0)) for strip in strips]
return tuple(sum(m[i] for m in means) // len(means) for i in range(3))
def adaptive_layer(art: Image.Image, size: int) -> Image.Image:
# Surround: plain felt in the artwork's edge colour, a little darker towards the corners.
edge = edge_colour(art)
dark = tuple(int(c * 0.6) for c in edge)
gradient = Image.radial_gradient("L").resize((size, size), Image.BICUBIC) # black centre -> white rim
surround = Image.composite(Image.new("RGB", (size, size), dark), Image.new("RGB", (size, size), edge), gradient)
art_px = round(size * ART_DP / LAYER_DP)
sharp = art.resize((art_px, art_px), Image.LANCZOS)
# Alpha: opaque in the middle, fading out over the outer FEATHER of each side so no square edge shows.
fade = max(1, round(art_px * FEATHER))
mask = Image.new("L", (art_px, art_px), 0)
mask.paste(Image.new("L", (art_px - 2 * fade, art_px - 2 * fade), 255), (fade, fade))
mask = mask.filter(ImageFilter.GaussianBlur(fade / 2))
offset = (size - art_px) // 2
surround.paste(sharp, (offset, offset), mask)
return surround
def main() -> None:
art = Image.open(SOURCE).convert("RGB")
for name, scale in DENSITIES.items():
size = round(LAYER_DP * scale)
folder = RES / f"mipmap-{name}"
folder.mkdir(parents=True, exist_ok=True)
adaptive_layer(art, size).save(folder / "ic_launcher_foreground.webp", "WEBP", quality=95, method=6)
print(f"mipmap-{name}/ic_launcher_foreground.webp {size}px")
(RES / "mipmap-anydpi-v26").mkdir(parents=True, exist_ok=True)
(RES / "mipmap-anydpi-v26" / "ic_launcher.xml").write_text(
'<?xml version="1.0" encoding="utf-8"?>\n'
"<!-- Generated by tools/make_icon.py from icon.png. -->\n"
'<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">\n'
' <background android:drawable="@color/ic_launcher_background" />\n'
' <foreground android:drawable="@mipmap/ic_launcher_foreground" />\n'
"</adaptive-icon>\n",
encoding="utf-8",
)
edge = art.getpixel((0, 0)) # the dark felt at the artwork's edge
(RES / "values" / "ic_launcher_background.xml").write_text(
'<?xml version="1.0" encoding="utf-8"?>\n<resources>\n'
f' <color name="ic_launcher_background">#{edge[0]:02X}{edge[1]:02X}{edge[2]:02X}</color>\n'
"</resources>\n",
encoding="utf-8",
)
art.resize((LOGO_PX, LOGO_PX), Image.LANCZOS).save(RES / "drawable-nodpi" / "logo.webp", "WEBP", quality=92, method=6)
print("drawable-nodpi/logo.webp", LOGO_PX, "px")
if __name__ == "__main__":
main()