100 lines
3.6 KiB
Python
100 lines
3.6 KiB
Python
"""Landscape clock face: analog dial on the left, digital time + date on the right.
|
|
|
|
Pure drawing code (Pillow only) so it can be previewed on any machine without the
|
|
screen: `render_frame(now, fonts)` returns the whole 480x320 image. The container
|
|
sends `render_dial` every second and `render_info` once a minute.
|
|
"""
|
|
import math
|
|
from datetime import datetime
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
W, H = 480, 320
|
|
DIAL_W = 300 # dial region is (0, 0, DIAL_W, H); info panel is the rest
|
|
INFO_W = W - DIAL_W
|
|
|
|
BG = (9, 11, 16)
|
|
FACE = (17, 21, 30)
|
|
RING = (44, 52, 70)
|
|
TICK = (110, 120, 140)
|
|
TICK_HOUR = (232, 236, 245)
|
|
HAND = (240, 244, 250)
|
|
ACCENT = (255, 112, 67)
|
|
TEXT = (245, 247, 252)
|
|
TEXT_DIM = (140, 148, 165)
|
|
|
|
DAYS = ["Pondělí", "Úterý", "Středa", "Čtvrtek", "Pátek", "Sobota", "Neděle"]
|
|
MONTHS = ["ledna", "února", "března", "dubna", "května", "června",
|
|
"července", "srpna", "září", "října", "listopadu", "prosince"]
|
|
|
|
SS = 4 # supersampling factor for the dial (smooth edges on a 320x480 panel)
|
|
|
|
|
|
def load_fonts(lib: str) -> dict:
|
|
root = f"{lib}/res/fonts"
|
|
return {
|
|
"time": ImageFont.truetype(f"{root}/roboto/Roboto-Light.ttf", 56),
|
|
"weekday": ImageFont.truetype(f"{root}/roboto/Roboto-Medium.ttf", 24),
|
|
"date": ImageFont.truetype(f"{root}/roboto/Roboto-Regular.ttf", 28),
|
|
"year": ImageFont.truetype(f"{root}/roboto/Roboto-Light.ttf", 22),
|
|
}
|
|
|
|
|
|
def _polar(cx, cy, r, deg):
|
|
a = math.radians(deg)
|
|
return cx + r * math.sin(a), cy - r * math.cos(a)
|
|
|
|
|
|
def _hand(d, cx, cy, deg, length, tail, width, color):
|
|
x1, y1 = _polar(cx, cy, length, deg)
|
|
x0, y0 = _polar(cx, cy, -tail, deg)
|
|
d.line([(x0, y0), (x1, y1)], fill=color, width=width)
|
|
r = width / 2
|
|
for x, y in ((x0, y0), (x1, y1)): # round the ends
|
|
d.ellipse([x - r, y - r, x + r, y + r], fill=color)
|
|
|
|
|
|
def render_dial(now: datetime) -> Image.Image:
|
|
img = Image.new("RGB", (DIAL_W * SS, H * SS), BG)
|
|
d = ImageDraw.Draw(img)
|
|
cx, cy, R = 150 * SS, 160 * SS, 138 * SS
|
|
|
|
d.ellipse([cx - R, cy - R, cx + R, cy + R], fill=FACE, outline=RING, width=3 * SS)
|
|
|
|
for i in range(60):
|
|
hour = i % 5 == 0
|
|
r_out = R - 9 * SS
|
|
r_in = R - (26 if hour else 17) * SS
|
|
d.line([_polar(cx, cy, r_in, i * 6), _polar(cx, cy, r_out, i * 6)],
|
|
fill=TICK_HOUR if hour else TICK, width=(4 if hour else 2) * SS)
|
|
|
|
h, m, s = now.hour % 12, now.minute, now.second
|
|
_hand(d, cx, cy, (h + m / 60) * 30, 72 * SS, 0, 7 * SS, HAND)
|
|
_hand(d, cx, cy, (m + s / 60) * 6, 108 * SS, 0, 5 * SS, HAND)
|
|
_hand(d, cx, cy, s * 6, 118 * SS, 28 * SS, 2 * SS, ACCENT)
|
|
|
|
for r, color in ((9 * SS, ACCENT), (4 * SS, FACE)): # centre cap
|
|
d.ellipse([cx - r, cy - r, cx + r, cy + r], fill=color)
|
|
|
|
return img.resize((DIAL_W, H), Image.LANCZOS)
|
|
|
|
|
|
def render_info(now: datetime, fonts: dict) -> Image.Image:
|
|
img = Image.new("RGB", (INFO_W, H), BG)
|
|
d = ImageDraw.Draw(img)
|
|
mid = INFO_W // 2 + 2
|
|
|
|
d.text((mid, 112), now.strftime("%H:%M"), font=fonts["time"], fill=TEXT, anchor="mm")
|
|
d.line([(mid - 28, 160), (mid + 28, 160)], fill=ACCENT, width=3)
|
|
d.text((mid, 192), DAYS[now.weekday()].upper(), font=fonts["weekday"], fill=ACCENT, anchor="mm")
|
|
d.text((mid, 230), f"{now.day}. {MONTHS[now.month - 1]}", font=fonts["date"], fill=TEXT, anchor="mm")
|
|
d.text((mid, 262), str(now.year), font=fonts["year"], fill=TEXT_DIM, anchor="mm")
|
|
return img
|
|
|
|
|
|
def render_frame(now: datetime, fonts: dict) -> Image.Image:
|
|
frame = Image.new("RGB", (W, H), BG)
|
|
frame.paste(render_dial(now), (0, 0))
|
|
frame.paste(render_info(now, fonts), (DIAL_W, 0))
|
|
return frame
|