Rajdhani font (OFL), chamfered/glitch drawing kit, Docker list every 5 s and process ranking over 3 s windows so the 1 s refresh stays cheap and calm. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
109 lines
4.0 KiB
Python
109 lines
4.0 KiB
Python
"""Cyberpunk-2077-style drawing kit: black scanlined background, yellow/cyan/red, chamfered shapes, glitch text."""
|
|
from functools import lru_cache
|
|
from pathlib import Path
|
|
from typing import Sequence
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
W, H = 480, 320
|
|
BG = (9, 9, 14)
|
|
SCAN = (13, 13, 20)
|
|
YELLOW = (252, 238, 10)
|
|
CYAN = (0, 240, 255)
|
|
RED = (255, 38, 74)
|
|
MAGENTA = (255, 0, 160)
|
|
DIM = (38, 38, 52)
|
|
TEXT = (226, 226, 232)
|
|
GREY = (120, 120, 140)
|
|
INK = (10, 10, 10)
|
|
|
|
FONT_DIR = Path(__file__).parent / "assets" / "fonts"
|
|
HOT_FROM = 80 # gauges turn red at this percentage
|
|
|
|
|
|
@lru_cache(maxsize=None)
|
|
def font(size: int, bold: bool = True) -> ImageFont.FreeTypeFont:
|
|
return ImageFont.truetype(str(FONT_DIR / ("Rajdhani-Bold.ttf" if bold else "Rajdhani-SemiBold.ttf")), size)
|
|
|
|
|
|
@lru_cache(maxsize=1)
|
|
def _background() -> Image.Image:
|
|
img = Image.new("RGB", (W, H), BG)
|
|
d = ImageDraw.Draw(img)
|
|
for y in range(0, H, 3):
|
|
d.line((0, y, W, y), fill=SCAN)
|
|
return img
|
|
|
|
|
|
def background() -> Image.Image:
|
|
"""A fresh copy of the scanlined black background."""
|
|
return _background().copy()
|
|
|
|
|
|
def chamfer(d: ImageDraw.ImageDraw, box: tuple[int, int, int, int], cut: int, outline=None, fill=None,
|
|
width: int = 1, cuts: str = "tl br") -> None:
|
|
"""A rectangle with the named corners (tl tr br bl) cut off at 45 degrees."""
|
|
x0, y0, x1, y1 = box
|
|
c = cut
|
|
pts: list[tuple[int, int]] = []
|
|
pts.append((x0 + c, y0) if "tl" in cuts else (x0, y0))
|
|
pts.append((x1 - c, y0) if "tr" in cuts else (x1, y0))
|
|
if "tr" in cuts:
|
|
pts.append((x1, y0 + c))
|
|
pts.append((x1, y1 - c) if "br" in cuts else (x1, y1))
|
|
if "br" in cuts:
|
|
pts.append((x1 - c, y1))
|
|
pts.append((x0 + c, y1) if "bl" in cuts else (x0, y1))
|
|
if "bl" in cuts:
|
|
pts.append((x0, y1 - c))
|
|
if "tl" in cuts:
|
|
pts.append((x0, y0 + c))
|
|
if fill is not None:
|
|
d.polygon(pts, fill=fill)
|
|
if outline is not None:
|
|
d.line(pts + [pts[0]], fill=outline, width=width)
|
|
|
|
|
|
def glitch_text(d: ImageDraw.ImageDraw, xy: tuple[float, float], text: str, f, main=YELLOW) -> None:
|
|
"""Text with a cyan and a red copy offset to either side (chromatic aberration)."""
|
|
x, y = xy
|
|
d.text((x - 1.5, y), text, font=f, fill=CYAN)
|
|
d.text((x + 1.5, y), text, font=f, fill=RED)
|
|
d.text((x, y), text, font=f, fill=main)
|
|
|
|
|
|
def slanted_bar(d: ImageDraw.ImageDraw, x: float, y: float, pct: float | None, h: float, n: int,
|
|
seg_w: float, gap: float, slant: float = 5, hot: float = HOT_FROM) -> None:
|
|
"""Segmented bar of slanted parallelograms: yellow, red from `hot` percent, unlit segments dark."""
|
|
lit = 0 if pct is None else max(0, min(n, round(pct / 100 * n)))
|
|
colour = RED if pct is not None and pct >= hot else YELLOW
|
|
for i in range(n):
|
|
x0 = x + i * (seg_w + gap)
|
|
d.polygon([(x0 + slant, y), (x0 + seg_w + slant, y), (x0 + seg_w, y + h), (x0, y + h)],
|
|
fill=colour if i < lit else DIM)
|
|
|
|
|
|
def tag(d: ImageDraw.ImageDraw, xy: tuple[int, int], text: str, f, colour=YELLOW, ink=INK) -> tuple[int, int, int, int]:
|
|
"""A chamfered label sized from its text; returns its box."""
|
|
left, top, right, bottom = d.textbbox((0, 0), text, font=f)
|
|
x, y = xy
|
|
box = (x, y, x + (right - left) + 20, y + (bottom - top) + 8)
|
|
chamfer(d, box, 7, fill=colour, cuts="tl br")
|
|
d.text((x + 10 - left, y + 4 - top), text, font=f, fill=ink)
|
|
return box
|
|
|
|
|
|
def fit(d: ImageDraw.ImageDraw, text: str, f, max_w: float) -> str:
|
|
"""`text`, shortened with an ellipsis until it is at most max_w pixels wide."""
|
|
if d.textlength(text, font=f) <= max_w:
|
|
return text
|
|
while text and d.textlength(text + "…", font=f) > max_w:
|
|
text = text[:-1]
|
|
return text + "…"
|
|
|
|
|
|
def hazard_stripe(d: ImageDraw.ImageDraw, x: int, y: int, length: int, h: int = 6, step: int = 12) -> None:
|
|
"""Yellow diagonal hazard stripes."""
|
|
for i in range(0, length, step * 2):
|
|
d.polygon([(x + i, y), (x + i + step, y), (x + i + step - h, y + h), (x + i - h, y + h)], fill=YELLOW)
|