Files
LihonT/scripts/make_sticker_icons.py
cheb0chik 38e7b2551e LihonT 2 Serena alpha 2
Plugin icons for the feed buttons (right ui.icons: like, comment, collect, share), preview cards that a theme author
attaches and the list of looks shows before a theme is picked, the sample plugins lihont.icons-sticker, lihont.theme-win95 and
lihont.theme-sunset with their pictures, docs, SDK, skill and tests for all of it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-02 13:08:56 +03:00

98 lines
3.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Draws the four sticker-style icons of the sample plugin lihont.icons-sticker (the buttons at the right of the feed).
usage: py -X utf8 -B scripts/make_sticker_icons.py
Writes plugins/lihont.icons-sticker/assets/{heart,bubble,star,plane}.png (96x96, a thick dark outline and a hard shadow) and
copies them to sdk/lihont-ltp/examples/files/ for the example 09_icons.py.
"""
import math
import shutil
from pathlib import Path
from PIL import Image, ImageDraw
ROOT = Path(__file__).resolve().parent.parent
OUT = ROOT / "plugins" / "lihont.icons-sticker" / "assets"
EXAMPLES = ROOT / "sdk" / "lihont-ltp" / "examples" / "files"
SIZE = 96
SCALE = 6
INK = (34, 28, 46, 255)
def canvas():
s = SIZE * SCALE
return Image.new("RGBA", (s, s), (0, 0, 0, 0)), s
def finish(img):
return img.resize((SIZE, SIZE), Image.Resampling.LANCZOS)
def sticker(draw_shape, fill):
"""Shadow first (shifted down and right), then the outlined shape on top."""
img, s = canvas()
d = ImageDraw.Draw(img)
off = s * 0.045
draw_shape(d, s, off, INK, None, 0)
draw_shape(d, s, 0, fill, INK, int(s * 0.04))
return finish(img)
def heart(d, s, off, fill, outline, width):
def bez(p0, p1, p2, p3, n=30):
return [((1 - t) ** 3 * p0[0] + 3 * (1 - t) ** 2 * t * p1[0] + 3 * (1 - t) * t * t * p2[0] + t ** 3 * p3[0],
(1 - t) ** 3 * p0[1] + 3 * (1 - t) ** 2 * t * p1[1] + 3 * (1 - t) * t * t * p2[1] + t ** 3 * p3[1])
for t in [i / n for i in range(n + 1)]]
q = lambda x, y: (s * x * 0.86 + s * 0.07 + off, s * y * 0.86 + s * 0.07 + off) # noqa: E731
pts = (bez(q(0.5, 0.94), q(0.10, 0.62), q(0.02, 0.40), q(0.10, 0.24)) + bez(q(0.10, 0.24), q(0.18, 0.08), q(0.40, 0.06), q(0.5, 0.27))
+ bez(q(0.5, 0.27), q(0.60, 0.06), q(0.82, 0.08), q(0.90, 0.24)) + bez(q(0.90, 0.24), q(0.98, 0.40), q(0.90, 0.62), q(0.5, 0.94)))
d.polygon(pts, fill=fill)
if outline:
d.line(pts + [pts[0]], fill=outline, width=width, joint="curve")
def bubble(d, s, off, fill, outline, width):
box = [s * 0.08 + off, s * 0.12 + off, s * 0.92 + off, s * 0.70 + off]
tail = [(s * 0.28 + off, s * 0.66 + off), (s * 0.22 + off, s * 0.92 + off), (s * 0.52 + off, s * 0.68 + off)]
d.rounded_rectangle(box, radius=s * 0.2, fill=fill, outline=outline, width=width)
d.polygon(tail, fill=fill)
if outline:
d.line([tail[0], tail[1], tail[2]], fill=outline, width=width, joint="curve")
d.line([(s * 0.30 + off, s * 0.665 + off), (s * 0.51 + off, s * 0.675 + off)], fill=fill, width=width + 2)
def star(d, s, off, fill, outline, width):
pts = []
for i in range(10):
r = s * (0.46 if i % 2 == 0 else 0.2)
a = -math.pi / 2 + i * math.pi / 5
pts.append((s / 2 + off + math.cos(a) * r, s * 0.54 + off + math.sin(a) * r))
d.polygon(pts, fill=fill)
if outline:
d.line(pts + [pts[0]], fill=outline, width=width, joint="curve")
def plane(d, s, off, fill, outline, width):
pts = [(s * 0.08 + off, s * 0.46 + off), (s * 0.92 + off, s * 0.12 + off), (s * 0.62 + off, s * 0.88 + off),
(s * 0.48 + off, s * 0.58 + off)]
d.polygon(pts, fill=fill)
if outline:
d.line(pts + [pts[0]], fill=outline, width=width, joint="curve")
d.line([pts[3], pts[1]], fill=outline, width=width)
def main():
OUT.mkdir(parents=True, exist_ok=True)
EXAMPLES.mkdir(parents=True, exist_ok=True)
for name, shape, color in (("heart", heart, (255, 107, 107, 255)), ("bubble", bubble, (90, 180, 255, 255)),
("star", star, (255, 205, 60, 255)), ("plane", plane, (94, 214, 170, 255))):
path = OUT / (name + ".png")
sticker(shape, color).save(path, optimize=True)
shutil.copy2(path, EXAMPLES / (name + ".png"))
print(path.name, path.stat().st_size, "bytes")
if __name__ == "__main__":
main()