Files
LihonT/scripts/make_widget_preview.py
cheb0chik 1d5a9a1fad LihonT 2 Luna
Ghost mode for stories and chats, several photos in one comment, .ltp plugins (rights, defender, rollback, downloads
only with consent), custom chat bubbles with a text colour of your choice, the lihont-ltp SDK and the plugin docs,
streak flames as widgets with a keeper, avatar shapes, Material 3 surfaces.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-01 18:19:27 +03:00

186 lines
7.8 KiB
Python

#!/usr/bin/env python3
"""Draws the picture the home-screen widget list shows for the LihonT streak widget.
The launcher shows a preview of every widget before it is added. The four streak widgets used to show TikTok's
own previews (a camera, a search bar), because they ride in TikTok's widget slots. This draws a flower with the
flame and a sample number in the colours of the LihonT logo, once, into src/brand/widget_preview.png; the build
(lihont/widget_patch.py) puts that picture in the place of TikTok's previews.
It mirrors the drawing in src/lihont/two/widget/FlowerArt.java (eight petals, the flame, the number), at the
size of the 360-unit grid that class draws on.
"""
from __future__ import annotations
import sys
from pathlib import Path
from PIL import Image, ImageDraw, ImageFont
ROOT = Path(__file__).resolve().parent.parent
OUT = ROOT / "src" / "brand" / "widget_preview.png"
SIDE = 360.0 # FlowerArt.SIDE
PETALS = 8
FLOWER = (0xE2, 0x6D, 0x6D, 255) # the coral of the logo
INK = (0x4E, 0x13, 0x20, 255) # its dark maroon
FLAME = (0xFF, 0x7A, 0x00, 255) # a lit flame, as FlowerArt.FLAME
FLAME_HEART = (0xFF, 0xC5, 0x3D, 255)
COLD = (0x8E, 0x93, 0x99, 255) # FlowerArt.COLD: the flame that is not lit
COLD_HEART = (0xC7, 0xCB, 0xD0, 255)
SAMPLE = "7"
# FlowerArt.SWAY and FlowerArt.HEART: how far the flame stretches and leans, and how its heart pulses, per frame
SWAY = [(1.00, 0.0), (1.06, 0.07), (0.97, 0.0), (1.04, -0.07)]
HEART = [1.00, 1.10, 0.92, 1.06]
SCALE = 4 # supersampling
PIXELS = 512
OUTER = [
("M", (12, 1.5)),
("C", (13.2, 6), (19.5, 9), (19.5, 15.2)),
("C", (19.5, 19.6), (16.2, 22.5), (12, 22.5)),
("C", (7.8, 22.5), (4.5, 19.6), (4.5, 15.4)),
("C", (4.5, 12.6), (6, 10.6), (7.8, 8.8)),
("C", (8, 11), (8.9, 12.2), (10.2, 12.8)),
("C", (9.5, 8.6), (10.4, 4.8), (12, 1.5)),
]
INNER = [
("M", (12, 20.3)),
("C", (9.9, 20.3), (8.6, 18.9), (8.6, 17.2)),
("C", (8.6, 15.5), (10.2, 14.2), (12, 12.2)),
("C", (13.8, 14.2), (15.4, 15.5), (15.4, 17.2)),
("C", (15.4, 18.9), (14.1, 20.3), (12, 20.3)),
]
def flatten(commands, place):
"""The outline of a path of moves and cubic curves as a list of points, placed by `place`."""
points = []
current = None
for command in commands:
if command[0] == "M":
current = command[1]
points.append(place(current))
else:
_, c1, c2, end = command
for step in range(1, 25):
t = step / 24.0
u = 1 - t
x = u ** 3 * current[0] + 3 * u * u * t * c1[0] + 3 * u * t * t * c2[0] + t ** 3 * end[0]
y = u ** 3 * current[1] + 3 * u * u * t * c1[1] + 3 * u * t * t * c2[1] + t ** 3 * end[1]
points.append(place((x, y)))
current = end
return points
def render(lit: bool = True, phase: int = 0) -> Image.Image:
flame_color, heart_color = (FLAME, FLAME_HEART) if lit else (COLD, COLD_HEART)
size = PIXELS * SCALE
k = size / SIDE
image = Image.new("RGBA", (size, size), (0, 0, 0, 0))
draw = ImageDraw.Draw(image)
def circle(cx, cy, r):
draw.ellipse([(cx - r) * k, (cy - r) * k, (cx + r) * k, (cy + r) * k], fill=FLOWER)
middle = SIDE / 2
reach = SIDE * 0.30
petal = SIDE * 0.20
import math
circle(middle, middle, reach)
for i in range(PETALS):
angle = i * 2 * math.pi / PETALS - math.pi / 2
circle(middle + math.cos(angle) * reach, middle + math.sin(angle) * reach, petal)
flame = SIDE * 0.30
left = middle - flame / 2
top = SIDE * 0.10
scale = flame / 24.0
stretch, lean = SWAY[phase % 4]
pulse = HEART[phase % 4] if lit else 1.0
def place(point, around=None):
x, y = point
if around is not None: # the heart pulses around its own base
x, y = around[0] + (x - around[0]) * pulse, around[1] + (y - around[1]) * pulse
dx, dy = x - 12, (y - 22.5) * stretch # the flame sways around its base (12, 22.5)
x, y = 12 + dx + lean * dy, 22.5 + dy
return ((left + x * scale) * k, (top + y * scale) * k)
outline = flatten(OUTER, place)
if max(abs(a - b) for a, b in zip(FLOWER[:3], flame_color[:3])) < 110: # FlowerArt.MIN_CONTRAST
draw.line(outline + [outline[0]], fill=INK, width=int(1.5 * scale * k * 2), joint="curve")
draw.polygon(outline, fill=flame_color)
draw.polygon(flatten(INNER, lambda pt: place(pt, (12, 20.3))), fill=heart_color)
font = ImageFont.truetype(r"C:\Windows\Fonts\arialbd.ttf", int(SIDE * 0.32 * k))
draw.text((middle * k, SIDE * 0.735 * k), SAMPLE, font=font, fill=INK, anchor="ms")
return image.resize((PIXELS, PIXELS), Image.LANCZOS)
def _hex(color) -> str:
return "#%02X%02X%02X" % color[:3]
def _path_data(commands) -> str:
"""An SVG path (absolute move and curve commands) of one of the flame outlines."""
parts = []
for command in commands:
if command[0] == "M":
parts.append("M%g %g" % command[1])
else:
parts.append("C%g %g %g %g %g %g" % (command[1] + command[2] + command[3]))
return " ".join(parts) + "Z"
def svg(lit: bool = True, sample: str = SAMPLE) -> str:
"""The widget as a vector picture on the 360-unit grid of FlowerArt: crisp at any size, one file to edit.
The number is text in a bold sans font; a viewer without Arial or Roboto draws its own bold sans.
"""
import math
flame_color, heart_color = (FLAME, FLAME_HEART) if lit else (COLD, COLD_HEART)
middle = SIDE / 2
reach = SIDE * 0.30
petal = SIDE * 0.20
flame = SIDE * 0.30
scale = flame / 24.0
close_colors = max(abs(a - b) for a, b in zip(FLOWER[:3], flame_color[:3])) < 110 # FlowerArt.MIN_CONTRAST
lines = ['<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 360 360" width="360" height="360">',
' <title>LihonT streak widget</title>',
' <g fill="%s">' % _hex(FLOWER),
' <circle cx="%g" cy="%g" r="%g"/>' % (middle, middle, reach)]
for i in range(PETALS):
angle = i * 2 * math.pi / PETALS - math.pi / 2
lines.append(' <circle cx="%.3f" cy="%.3f" r="%g"/>' % (
middle + math.cos(angle) * reach, middle + math.sin(angle) * reach, petal))
lines.append(' </g>')
lines.append(' <g transform="translate(%g %g) scale(%g)">' % (middle - flame / 2, SIDE * 0.10, scale))
if close_colors:
lines.append(' <path d="%s" fill="none" stroke="%s" stroke-width="1.5" stroke-linejoin="round"/>' % (
_path_data(OUTER), _hex(INK)))
lines.append(' <path d="%s" fill="%s"/>' % (_path_data(OUTER), _hex(flame_color)))
lines.append(' <path d="%s" fill="%s"/>' % (_path_data(INNER), _hex(heart_color)))
lines.append(' </g>')
lines.append(' <text x="%g" y="%g" font-family="Roboto, Arial, Helvetica, sans-serif" font-weight="700" '
'font-size="%g" text-anchor="middle" fill="%s">%s</text>' % (
middle, SIDE * 0.735, SIDE * 0.32, _hex(INK), sample))
lines.append('</svg>')
return "\n".join(lines) + "\n"
def main() -> int:
OUT.parent.mkdir(parents=True, exist_ok=True)
render().save(OUT, optimize=True)
(OUT.parent / "widget_flame.svg").write_text(svg(True), encoding="utf-8")
(OUT.parent / "widget_flame_grey.svg").write_text(svg(False), encoding="utf-8")
if len(sys.argv) > 1: # a picture of the grey flame too, for the release cards
render(lit=False).save(sys.argv[1], optimize=True)
print("wrote", OUT, OUT.stat().st_size, "bytes, and widget_flame.svg, widget_flame_grey.svg")
return 0
if __name__ == "__main__":
sys.exit(main())