615 lines
28 KiB
Python
615 lines
28 KiB
Python
"""Renders the GPU | CPU dashboard for the Thermaltake 3.9" bar LCD (480x128).
|
|
|
|
Left half is the GPU; the right half is the same layout mirrored horizontally
|
|
for the CPU, with all gauges filling clockwise. The centre column shows the whole PC's power draw
|
|
against the power supply's rating, stacked as other (bottom), CPU, GPU (top).
|
|
The supplied red-panel background sits behind the procedural instruments.
|
|
Everything is rendered at SS x resolution and then downsampled,
|
|
which gives clean anti-aliasing on the small panel.
|
|
|
|
[GPU]---------------------[ TOTAL W ]---------------------[CPU]
|
|
temp gauge load gauge | tank | load gauge temp gauge
|
|
[ MHz ]-------[# GPU W] [# other W] [# CPU W]-------[ MHz ]
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import math
|
|
import os
|
|
import numpy as np
|
|
from PIL import Image, ImageChops, ImageDraw, ImageFilter, ImageFont
|
|
|
|
from kurgan.telemetry.readings import PowerReadings, Readings
|
|
from kurgan.theme import Theme
|
|
|
|
W, H = 480, 128
|
|
SS = 4 # supersampling factor
|
|
|
|
FONT_FILE = os.path.join(os.environ.get("WINDIR", r"C:\Windows"), "Fonts", "bahnschrift.ttf")
|
|
FALLBACK_FONT = os.path.join(os.environ.get("WINDIR", r"C:\Windows"), "Fonts", "segoeuib.ttf")
|
|
|
|
|
|
# ----------------------------------------------------------------------------- layout
|
|
# All coordinates are in panel pixels for the GPU (left) half. The CPU half is
|
|
# produced by mirroring x around the centre line.
|
|
|
|
R_GAUGE = 48.0
|
|
R_FACE = 41.0
|
|
ARC_R_OUT = 38.5
|
|
ARC_W = 6.0
|
|
ARC_START = 150.0 # degrees, 0 = 3 o'clock, clockwise
|
|
ARC_SWEEP = 240.0
|
|
G_TEMP = (52.0, 66.0)
|
|
G_LOAD = (152.0, 66.0)
|
|
DIGIT_CY = 62.0
|
|
UNIT_CY = 88.0
|
|
|
|
TITLE_BOX = (80.0, 2.0, 124.0, 16.0)
|
|
MHZ_BOX = (74.0, 112.0, 130.0, 126.0)
|
|
PART_BOX = (170.0, 112.0, 212.0, 126.0) # this half's watts (CPU or GPU)
|
|
|
|
# Centre column (drawn once, not mirrored)
|
|
TOTAL_BOX = (206.0, 2.0, 274.0, 16.0)
|
|
OTHER_BOX = (218.0, 112.0, 262.0, 126.0)
|
|
TANK_X0, TANK_X1 = 222.0, 258.0
|
|
CAP_TOP = (17.0, 24.0)
|
|
CAP_BOT = (97.0, 108.0)
|
|
GLASS_X0, GLASS_X1 = TANK_X0 + 3.0, TANK_X1 - 3.0
|
|
GLASS_Y0, GLASS_Y1 = CAP_TOP[1], CAP_BOT[0]
|
|
|
|
PIPE_T = 6.0
|
|
_GY = math.sqrt(R_GAUGE ** 2 - 24.0 ** 2) # where x = cx - 24 meets the bezel
|
|
PIPES = [
|
|
[(28.0, G_TEMP[1] + _GY), (28.0, 119.0), (MHZ_BOX[0], 119.0)], # temp gauge -> MHz box
|
|
[(MHZ_BOX[2], 119.0), (PART_BOX[0], 119.0)], # MHz box -> watt box
|
|
[(28.0, G_TEMP[1] - _GY), (28.0, 9.0), (TITLE_BOX[0], 9.0)], # temp gauge -> title
|
|
[(TITLE_BOX[2], 9.0), (TOTAL_BOX[0], 9.0)], # title -> total box
|
|
]
|
|
TANK_PIPE = [(W / 2, CAP_BOT[1]), (W / 2, OTHER_BOX[1])] # tank -> other box
|
|
JUNCTION_PIPE = [(G_TEMP[0] + R_GAUGE - 4, 66.0), (G_LOAD[0] - R_GAUGE + 4, 66.0)]
|
|
RIVETS = [(6.0, 6.0), (6.0, 122.0)]
|
|
|
|
COPPER = [
|
|
(1.00, (64, 30, 16)),
|
|
(0.84, (112, 56, 30)),
|
|
(0.66, (156, 82, 48)),
|
|
(0.46, (196, 116, 72)),
|
|
(0.26, (228, 156, 110)),
|
|
(0.09, (252, 212, 178)),
|
|
]
|
|
|
|
# 5x7 dot-matrix glyphs
|
|
GLYPHS = {
|
|
"0": [".###.", "#...#", "#...#", "#...#", "#...#", "#...#", ".###."],
|
|
"1": ["..#..", ".##..", "..#..", "..#..", "..#..", "..#..", ".###."],
|
|
"2": [".###.", "#...#", "....#", "...#.", "..#..", ".#...", "#####"],
|
|
"3": ["#####", "...#.", "..#..", "...#.", "....#", "#...#", ".###."],
|
|
"4": ["...#.", "..##.", ".#.#.", "#..#.", "#####", "...#.", "...#."],
|
|
"5": ["#####", "#....", "####.", "....#", "....#", "#...#", ".###."],
|
|
"6": ["..##.", ".#...", "#....", "####.", "#...#", "#...#", ".###."],
|
|
"7": ["#####", "....#", "...#.", "..#..", ".#...", ".#...", ".#..."],
|
|
"8": [".###.", "#...#", "#...#", ".###.", "#...#", "#...#", ".###."],
|
|
"9": [".###.", "#...#", "#...#", ".####", "....#", "...#.", ".##.."],
|
|
"-": [".....", ".....", ".....", ".###.", ".....", ".....", "....."],
|
|
}
|
|
|
|
|
|
# ----------------------------------------------------------------------------- helpers
|
|
|
|
def _s(v: float) -> float:
|
|
return v * SS
|
|
|
|
|
|
def _font(size_px: float, style: str = "Bold") -> ImageFont.FreeTypeFont:
|
|
try:
|
|
f = ImageFont.truetype(FONT_FILE, round(size_px * SS))
|
|
f.set_variation_by_name(style)
|
|
return f
|
|
except Exception:
|
|
return ImageFont.truetype(FALLBACK_FONT, round(size_px * SS))
|
|
|
|
|
|
def _grid(x0: float, y0: float, x1: float, y1: float):
|
|
ix0 = max(0, int(math.floor(x0 * SS)))
|
|
iy0 = max(0, int(math.floor(y0 * SS)))
|
|
ix1 = min(W * SS, int(math.ceil(x1 * SS)))
|
|
iy1 = min(H * SS, int(math.ceil(y1 * SS)))
|
|
xs = (np.arange(ix0, ix1) + 0.5) / SS
|
|
ys = (np.arange(iy0, iy1) + 0.5) / SS
|
|
X, Y = np.meshgrid(xs, ys)
|
|
return ix0, iy0, X, Y
|
|
|
|
|
|
def _paste(canvas: Image.Image, ix0: int, iy0: int, rgb: np.ndarray, alpha: np.ndarray) -> None:
|
|
arr = np.dstack([np.clip(rgb, 0, 255), np.clip(alpha, 0, 1) * 255]).astype(np.uint8)
|
|
canvas.alpha_composite(Image.fromarray(arr, "RGBA"), (ix0, iy0))
|
|
|
|
|
|
def _rrect_mask(X, Y, x0, y0, x1, y1, r):
|
|
cx = np.clip(X, x0 + r, x1 - r)
|
|
cy = np.clip(Y, y0 + r, y1 - r)
|
|
return ((X - cx) ** 2 + (Y - cy) ** 2 <= r * r) & (X >= x0) & (X <= x1) & (Y >= y0) & (Y <= y1)
|
|
|
|
|
|
def _mix(lo, hi, t):
|
|
lo = np.asarray(lo, dtype=float)
|
|
hi = np.asarray(hi, dtype=float)
|
|
return lo + (hi - lo) * np.asarray(t)[..., None]
|
|
|
|
|
|
def _cyl_shade(u, hl=0.33, width=0.2, ambient=0.18):
|
|
"""Cylinder lighting profile across a tube, u in 0..1."""
|
|
u = np.clip(u, 0, 1)
|
|
body = np.sin(np.pi * u) ** 0.6
|
|
spec = np.exp(-((u - hl) / width) ** 2)
|
|
return np.clip(ambient + 0.55 * body + 0.45 * spec, 0, 1)
|
|
|
|
|
|
def cyl_rect(canvas, x0, y0, x1, y1, across, lo, hi, radius=0.0, hl=0.33, width=0.2, ambient=0.18):
|
|
ix0, iy0, X, Y = _grid(x0, y0, x1, y1)
|
|
u = (X - x0) / (x1 - x0) if across == "x" else (Y - y0) / (y1 - y0)
|
|
rgb = _mix(lo, hi, _cyl_shade(u, hl, width, ambient))
|
|
mask = _rrect_mask(X, Y, x0, y0, x1, y1, radius) if radius else (X >= x0) & (X <= x1) & (Y >= y0) & (Y <= y1)
|
|
_paste(canvas, ix0, iy0, rgb, mask.astype(float))
|
|
|
|
|
|
def dome(canvas, cx, cy, rad, tint=(205, 205, 212)):
|
|
ix0, iy0, X, Y = _grid(cx - rad - 1, cy - rad - 1, cx + rad + 1, cy + rad + 1)
|
|
d = np.hypot(X - cx, Y - cy) / rad
|
|
hl = np.exp(-(((X - cx + rad * 0.35) ** 2 + (Y - cy + rad * 0.35) ** 2) / (rad * 0.45) ** 2))
|
|
v = 0.18 + 0.30 * np.sqrt(np.clip(1 - d ** 2, 0, 1)) + 0.65 * hl
|
|
rgb = _mix((0, 0, 0), tint, np.clip(v, 0, 1.2))
|
|
ring = np.clip((d - 0.82) / 0.18, 0, 1)
|
|
rgb = rgb * (1 - 0.55 * ring[..., None])
|
|
_paste(canvas, ix0, iy0, rgb, (d <= 1).astype(float))
|
|
|
|
|
|
def _fillet(points, radius=5.0, steps=10):
|
|
"""Round the corners of a polyline with quadratic Bezier curves."""
|
|
pts = [np.array(p, dtype=float) for p in points]
|
|
if len(pts) < 3:
|
|
return [tuple(p) for p in pts]
|
|
out = [tuple(pts[0])]
|
|
for i in range(1, len(pts) - 1):
|
|
p0, p1, p2 = pts[i - 1], pts[i], pts[i + 1]
|
|
d1, d2 = np.linalg.norm(p0 - p1), np.linalg.norm(p2 - p1)
|
|
r = min(radius, d1 / 2, d2 / 2)
|
|
a = p1 + (p0 - p1) / d1 * r
|
|
b = p1 + (p2 - p1) / d2 * r
|
|
for t in np.linspace(0, 1, steps):
|
|
q = (1 - t) ** 2 * a + 2 * (1 - t) * t * p1 + t ** 2 * b
|
|
out.append(tuple(q))
|
|
out.append(tuple(pts[-1]))
|
|
return out
|
|
|
|
|
|
def _extend(points, amount=2.0):
|
|
"""Push both ends of a polyline outward so pipes tuck under their fittings."""
|
|
pts = [np.array(p, dtype=float) for p in points]
|
|
for end, nxt in ((0, 1), (-1, -2)):
|
|
v = pts[end] - pts[nxt]
|
|
n = np.linalg.norm(v)
|
|
if n:
|
|
pts[end] = pts[end] + v / n * amount
|
|
return [tuple(p) for p in pts]
|
|
|
|
|
|
class _Half:
|
|
"""Coordinate mapping for one half; mirror=True maps the layout onto the right."""
|
|
|
|
def __init__(self, mirror: bool):
|
|
self.mirror = mirror
|
|
|
|
def x(self, x: float) -> float:
|
|
return W - x if self.mirror else x
|
|
|
|
def pt(self, p):
|
|
return (self.x(p[0]), p[1])
|
|
|
|
def box(self, b):
|
|
xa, xb = sorted((self.x(b[0]), self.x(b[2])))
|
|
return (xa, b[1], xb, b[3])
|
|
|
|
|
|
# ----------------------------------------------------------------------------- renderer
|
|
|
|
class DashboardRenderer:
|
|
def __init__(self, theme: Theme | None = None, rotate180: bool = False):
|
|
self.theme = theme or Theme()
|
|
self.rotate180 = rotate180
|
|
self.halves = (_Half(False), _Half(True))
|
|
self.f_title = _font(11, "Bold")
|
|
self.f_unit = _font(9, "SemiBold")
|
|
self.f_value = _font(10.5, "Bold SemiCondensed")
|
|
self.f_small = _font(7, "SemiBold SemiCondensed")
|
|
self._static = self._build_static()
|
|
self._glass = self._build_glass_overlay()
|
|
self._dome_light, self._dome_shade = self._build_dome_overlay()
|
|
|
|
# ------------------------------------------------------------------ static art
|
|
|
|
def _build_static(self) -> Image.Image:
|
|
canvas = self._plate()
|
|
self._shadows(canvas)
|
|
for half in self.halves:
|
|
for pipe in PIPES:
|
|
self._pipe(canvas, [half.pt(p) for p in pipe])
|
|
self._pipe(canvas, [half.pt(p) for p in JUNCTION_PIPE])
|
|
self._pipe(canvas, TANK_PIPE)
|
|
|
|
for half in self.halves:
|
|
for g in (G_TEMP, G_LOAD):
|
|
self._gauge(canvas, half.x(g[0]), g[1])
|
|
for b in (MHZ_BOX, PART_BOX, TITLE_BOX):
|
|
self._box(canvas, half.box(b))
|
|
self._tank(canvas)
|
|
self._box(canvas, TOTAL_BOX)
|
|
self._box(canvas, OTHER_BOX)
|
|
|
|
for half in self.halves:
|
|
for pipe in PIPES:
|
|
pts = [half.pt(p) for p in pipe]
|
|
self._coupler(canvas, pts[0], pts[1])
|
|
self._coupler(canvas, pts[-1], pts[-2])
|
|
self._nut(canvas, half.x((JUNCTION_PIPE[0][0] + JUNCTION_PIPE[1][0]) / 2), 66.0)
|
|
self._coupler(canvas, TANK_PIPE[0], TANK_PIPE[1])
|
|
self._coupler(canvas, TANK_PIPE[1], TANK_PIPE[0])
|
|
for x, y in RIVETS:
|
|
for half in self.halves:
|
|
dome(canvas, half.x(x), y, 2.3)
|
|
|
|
draw = ImageDraw.Draw(canvas)
|
|
glow = Image.new("RGB", canvas.size, (0, 0, 0))
|
|
gdraw = ImageDraw.Draw(glow)
|
|
for i, half in enumerate(self.halves):
|
|
b = half.box(TITLE_BOX)
|
|
c = (_s((b[0] + b[2]) / 2), _s((b[1] + b[3]) / 2 + 0.4))
|
|
gdraw.text(c, self.theme.titles[i], font=self.f_title, fill=self.theme.accent, anchor="mm")
|
|
for g, unit in ((G_TEMP, "\u00b0C"), (G_LOAD, "%")):
|
|
draw.text((_s(half.x(g[0])), _s(UNIT_CY)), unit, font=self.f_unit,
|
|
fill=(196, 196, 200), anchor="mm")
|
|
glow = glow.filter(ImageFilter.GaussianBlur(_s(2.2)))
|
|
rgb = ImageChops.screen(canvas.convert("RGB"), glow)
|
|
canvas = rgb.convert("RGBA")
|
|
draw = ImageDraw.Draw(canvas)
|
|
for i, half in enumerate(self.halves):
|
|
b = half.box(TITLE_BOX)
|
|
c = (_s((b[0] + b[2]) / 2), _s((b[1] + b[3]) / 2 + 0.4))
|
|
draw.text(c, self.theme.titles[i], font=self.f_title, fill=self.theme.accent, anchor="mm")
|
|
return canvas
|
|
|
|
def _plate(self) -> Image.Image:
|
|
path = os.path.join(os.path.dirname(__file__), "background.png")
|
|
with Image.open(path) as background:
|
|
return background.convert("RGBA").resize((W * SS, H * SS), Image.Resampling.LANCZOS)
|
|
|
|
def _shadows(self, canvas: Image.Image) -> None:
|
|
mask = Image.new("L", canvas.size, 0)
|
|
d = ImageDraw.Draw(mask)
|
|
for half in self.halves:
|
|
for g in (G_TEMP, G_LOAD):
|
|
cx, cy = half.x(g[0]), g[1]
|
|
d.ellipse([_s(cx - R_GAUGE), _s(cy - R_GAUGE), _s(cx + R_GAUGE), _s(cy + R_GAUGE)], fill=255)
|
|
for b in (MHZ_BOX, PART_BOX, TITLE_BOX):
|
|
d.rectangle([_s(v) for v in half.box(b)], fill=255)
|
|
for pipe in PIPES:
|
|
d.line([(_s(x), _s(y)) for x, y in _fillet([half.pt(p) for p in pipe])],
|
|
fill=255, width=round(_s(PIPE_T)), joint="curve")
|
|
for b in (TOTAL_BOX, OTHER_BOX, (TANK_X0, CAP_TOP[0], TANK_X1, CAP_BOT[1])):
|
|
d.rectangle([_s(v) for v in b], fill=255)
|
|
d.line([(_s(x), _s(y)) for x, y in TANK_PIPE], fill=255, width=round(_s(PIPE_T)))
|
|
mask = mask.filter(ImageFilter.GaussianBlur(_s(2.4)))
|
|
shifted = Image.new("L", canvas.size, 0)
|
|
shifted.paste(mask, (round(_s(1.4)), round(_s(2.0))))
|
|
shade = np.asarray(shifted, dtype=float)[..., None] / 255
|
|
arr = np.asarray(canvas, dtype=float).copy()
|
|
arr[..., :3] *= 1 - 0.75 * shade
|
|
canvas.paste(Image.fromarray(arr.astype(np.uint8), "RGBA"))
|
|
|
|
def _pipe(self, canvas, points) -> None:
|
|
pts = _fillet(_extend(points, 2.5))
|
|
d = ImageDraw.Draw(canvas)
|
|
for frac, col in COPPER:
|
|
off = -0.13 * PIPE_T * (1 - frac) if frac < 0.9 else 0.0
|
|
d.line([(_s(x + off), _s(y + off)) for x, y in pts], fill=col,
|
|
width=max(1, round(_s(PIPE_T * frac))), joint="curve")
|
|
|
|
def _coupler(self, canvas, end, toward) -> None:
|
|
horizontal = abs(end[0] - toward[0]) > abs(end[1] - toward[1])
|
|
x, y = end
|
|
half_len, half_w = 1.7, PIPE_T / 2 + 1.0
|
|
if horizontal:
|
|
cyl_rect(canvas, x - half_len, y - half_w, x + half_len, y + half_w, "y",
|
|
(10, 10, 12), (170, 170, 176), radius=0.6)
|
|
else:
|
|
cyl_rect(canvas, x - half_w, y - half_len, x + half_w, y + half_len, "x",
|
|
(10, 10, 12), (170, 170, 176), radius=0.6)
|
|
|
|
def _nut(self, canvas, x, y) -> None:
|
|
hw, hh = 3.4, PIPE_T / 2 + 2.2
|
|
cyl_rect(canvas, x - hw, y - hh, x + hw, y + hh, "y", (12, 12, 14), (190, 190, 196), radius=0.8)
|
|
d = ImageDraw.Draw(canvas)
|
|
for fx in (-1.15, 1.15):
|
|
d.line([(_s(x + fx), _s(y - hh + 0.4)), (_s(x + fx), _s(y + hh - 0.4))],
|
|
fill=(20, 20, 22), width=round(_s(0.45)))
|
|
|
|
def _gauge(self, canvas, cx, cy) -> None:
|
|
ix0, iy0, X, Y = _grid(cx - R_GAUGE - 1, cy - R_GAUGE - 1, cx + R_GAUGE + 1, cy + R_GAUGE + 1)
|
|
dx, dy = X - cx, Y - cy
|
|
r = np.hypot(dx, dy)
|
|
phi = np.arctan2(dy, dx)
|
|
light = np.cos(phi - math.radians(225))
|
|
t = np.clip((r - R_FACE) / (R_GAUGE - R_FACE), 0, 1)
|
|
prof = np.sin(t * np.pi)
|
|
v = 0.17 + 0.16 * prof + 0.20 * light * (0.35 + 0.65 * prof)
|
|
v += 0.30 * np.exp(-((R_GAUGE - 0.7 - r) / 0.45) ** 2) * (0.45 + 0.55 * light)
|
|
v -= 0.12 * np.exp(-((r - R_FACE - 0.5) / 0.6) ** 2)
|
|
v += 0.018 * np.sin(r * SS * 2.2)
|
|
face = 0.03 + 0.05 * (1 - r / R_FACE) ** 2
|
|
face = face * (0.45 + 0.55 * np.clip((R_FACE - r) / 3.5, 0, 1))
|
|
val = np.where(r < R_FACE, face, v)
|
|
rgb = _mix((0, 0, 0), (238, 240, 246), np.clip(val, 0, 1))
|
|
_paste(canvas, ix0, iy0, rgb, (r <= R_GAUGE).astype(float))
|
|
|
|
d = ImageDraw.Draw(canvas)
|
|
# arc groove
|
|
for rad, width, col in ((ARC_R_OUT + 0.8, ARC_W + 1.6, (54, 54, 58)), (ARC_R_OUT, ARC_W, (20, 20, 22))):
|
|
bb = [_s(cx - rad), _s(cy - rad), _s(cx + rad), _s(cy + rad)]
|
|
d.arc(bb, ARC_START, ARC_START + ARC_SWEEP, fill=col, width=round(_s(width)))
|
|
# bolts on the bezel
|
|
for a in (45, 135, 225, 315):
|
|
ar = math.radians(a)
|
|
dome(canvas, cx + 44.5 * math.cos(ar), cy + 44.5 * math.sin(ar), 1.7, tint=(170, 170, 178))
|
|
|
|
def _tank(self, canvas) -> None:
|
|
x0, x1 = TANK_X0, TANK_X1
|
|
chrome = ((22, 22, 25), (236, 236, 242))
|
|
# glass background
|
|
cyl_rect(canvas, GLASS_X0, GLASS_Y0 - 1, GLASS_X1, GLASS_Y1 + 1, "x", (6, 6, 8), (40, 40, 46),
|
|
hl=0.3, width=0.25)
|
|
# end caps with grooves
|
|
for y0, y1 in (CAP_TOP, CAP_BOT):
|
|
cyl_rect(canvas, x0, y0, x1, y1, "x", *chrome, radius=1.4)
|
|
d = ImageDraw.Draw(canvas)
|
|
for gy in (CAP_TOP[0] + 2.4, 100.5, 105.0):
|
|
d.line([(_s(x0 + 0.6), _s(gy)), (_s(x1 - 0.6), _s(gy))], fill=(30, 30, 34, 200), width=round(_s(0.6)))
|
|
for y0, y1 in ((CAP_TOP[1] - 2.2, CAP_TOP[1]), (CAP_BOT[0], CAP_BOT[0] + 2.2)):
|
|
cyl_rect(canvas, x0 + 1, y0, x1 - 1, y1, "x", (10, 10, 12), (110, 110, 116))
|
|
|
|
def _build_glass_overlay(self) -> Image.Image:
|
|
layer = Image.new("RGBA", (W * SS, H * SS), (0, 0, 0, 0))
|
|
gx0, gx1 = GLASS_X0, GLASS_X1
|
|
ix0, iy0, X, Y = _grid(gx0, GLASS_Y0, gx1, GLASS_Y1)
|
|
u = (X - gx0) / (gx1 - gx0)
|
|
a = 0.38 * np.exp(-((u - 0.24) / 0.04) ** 2) + 0.12 * np.exp(-((u - 0.80) / 0.04) ** 2)
|
|
edge = np.exp(-(u / 0.05) ** 2) + np.exp(-((1 - u) / 0.05) ** 2)
|
|
top_shadow = np.clip(1 - (Y - GLASS_Y0) / 3.0, 0, 1)
|
|
rgb = np.where((edge > 0.3)[..., None], 0.0, 255.0) * np.ones(3)
|
|
alpha = np.maximum(a, np.maximum(0.55 * edge, 0.65 * top_shadow))
|
|
rgb = np.where((top_shadow * 0.65 > a)[..., None], 0.0, rgb)
|
|
_paste(layer, ix0, iy0, rgb, alpha)
|
|
# scale: a tick every 10% of the power supply's rating, a longer one at 50%
|
|
d = ImageDraw.Draw(layer)
|
|
span = GLASS_Y1 - GLASS_Y0
|
|
for i in range(1, 10):
|
|
y = GLASS_Y1 - span * i / 10
|
|
length = 3.5 if i == 5 else 2.0
|
|
for xa, xb in ((gx0 + 1.5, gx0 + 1.5 + length), (gx1 - 1.5 - length, gx1 - 1.5)):
|
|
d.line([(_s(xa), _s(y)), (_s(xb), _s(y))], fill=(230, 230, 235, 130), width=round(_s(0.5)))
|
|
return layer
|
|
|
|
def _build_dome_overlay(self) -> tuple[np.ndarray, np.ndarray]:
|
|
"""Reflections of a glass dome over each gauge face, lit from the top left.
|
|
|
|
Returns panel-sized (H, W, 1) maps: `light` is screened over the finished
|
|
frame (so it also sits over the glowing arcs and digits) and `shade` multiplies it.
|
|
"""
|
|
light = np.zeros((H * SS, W * SS))
|
|
shade = np.ones((H * SS, W * SS))
|
|
rad = R_FACE - 0.2
|
|
for half in self.halves:
|
|
for g in (G_TEMP, G_LOAD):
|
|
cx, cy = half.x(g[0]), g[1]
|
|
ix0, iy0, X, Y = _grid(cx - rad - 1, cy - rad - 1, cx + rad + 1, cy + rad + 1)
|
|
u, v = (X - cx) / rad, (Y - cy) / rad
|
|
r = np.hypot(u, v)
|
|
inside = np.clip((1 - r) * rad * 1.5, 0, 1)
|
|
# broad crescent sheen: inside the dome but outside a circle offset to the lower right
|
|
inner = np.hypot(u - 0.10, v - 0.20) / 0.93
|
|
crescent = np.clip((inner - 1) * 9, 0, 1) * np.clip((0.98 - r) * 25, 0, 1)
|
|
towards = np.clip(0.55 - 0.75 * (u * 0.55 + v * 0.83), 0, 1)
|
|
sheen = 0.42 * crescent * towards
|
|
# window highlight up and to the left
|
|
a, b = (u + v + 0.95) / 1.414, (v - u + 0.18) / 1.414
|
|
spot = 0.75 * np.exp(-(a / 0.07) ** 2 - (b / 0.30) ** 2) + 0.12 * np.exp(-(a / 0.22) ** 2 - (b / 0.45) ** 2)
|
|
# faint bounce along the lower-right rim
|
|
ang = np.arctan2(v, u)
|
|
bounce = 0.13 * np.exp(-((r - 0.93) / 0.035) ** 2) * np.clip(np.cos(ang - math.radians(60)), 0, 1) ** 2
|
|
haze = 0.04 * (1 - r ** 2).clip(0, 1)
|
|
hi = (np.maximum(sheen, spot) + bounce + haze) * inside
|
|
dark = 1 - 0.30 * np.clip((r - 0.80) / 0.20, 0, 1) ** 2 * inside
|
|
sl = (slice(iy0, iy0 + hi.shape[0]), slice(ix0, ix0 + hi.shape[1]))
|
|
light[sl] = np.maximum(light[sl], hi)
|
|
shade[sl] = np.minimum(shade[sl], dark)
|
|
|
|
def small(m: np.ndarray) -> np.ndarray:
|
|
img = Image.fromarray(np.clip(m * 255, 0, 255).astype(np.uint8), "L")
|
|
return np.asarray(img.resize((W, H), Image.Resampling.BOX), dtype=float)[..., None] / 255
|
|
|
|
return small(light), small(shade)
|
|
|
|
def _box(self, canvas, b) -> None:
|
|
x0, y0, x1, y1 = b
|
|
ix0, iy0, X, Y = _grid(x0, y0, x1, y1)
|
|
t = (Y - y0) / (y1 - y0)
|
|
frame = _mix((40, 40, 44), (215, 215, 222), np.clip(1.05 - 1.1 * t, 0, 1))
|
|
_paste(canvas, ix0, iy0, frame, _rrect_mask(X, Y, x0, y0, x1, y1, 2.2).astype(float))
|
|
i = 1.4
|
|
inner = _mix((4, 4, 5), (20, 20, 23), t)
|
|
inner_shadow = np.clip((Y - (y0 + i)) / 2.0, 0, 1)
|
|
inner = inner * (0.4 + 0.6 * inner_shadow)[..., None]
|
|
_paste(canvas, ix0, iy0, inner, _rrect_mask(X, Y, x0 + i, y0 + i, x1 - i, y1 - i, 1.2).astype(float))
|
|
|
|
# ------------------------------------------------------------------ dynamic
|
|
|
|
def render(self, cpu: Readings, gpu: Readings, power: PowerReadings | None = None) -> Image.Image:
|
|
if power is None:
|
|
power = PowerReadings(cpu=cpu.power, gpu=gpu.power)
|
|
canvas = self._static.copy()
|
|
glow = Image.new("RGB", canvas.size, (0, 0, 0))
|
|
draw = ImageDraw.Draw(canvas)
|
|
gdraw = ImageDraw.Draw(glow)
|
|
th = self.theme
|
|
|
|
for half, rd, temp_range in ((self.halves[0], gpu, th.gpu_temp_range),
|
|
(self.halves[1], cpu, th.cpu_temp_range)):
|
|
reverse = half.mirror and th.mirror_gauge_direction
|
|
# gauges
|
|
gauges = (
|
|
(G_TEMP, rd.temp, *temp_range, th.temp_warn, th.temp_hot),
|
|
(G_LOAD, rd.load, 0.0, 100.0, th.load_warn, th.load_hot),
|
|
)
|
|
for g, value, lo, hi, warn, hot in gauges:
|
|
cx, cy = half.x(g[0]), g[1]
|
|
frac = None if value is None else min(1.0, max(0.0, (value - lo) / (hi - lo)))
|
|
if frac:
|
|
self._arc_fill(draw, gdraw, cx, cy, frac, reverse, th.level_color(value, warn, hot))
|
|
self._digits(draw, gdraw, cx, DIGIT_CY, value)
|
|
self._readout(draw, half.box(MHZ_BOX), rd.clock, "MHz")
|
|
|
|
# power column: stacked tank, its parts along the bottom, the sum on top
|
|
self._liquid(canvas, power)
|
|
draw = ImageDraw.Draw(canvas)
|
|
self._readout(draw, self.halves[0].box(PART_BOX), power.gpu, "W", swatch=th.gpu_color)
|
|
self._readout(draw, self.halves[1].box(PART_BOX), power.cpu, "W", swatch=th.cpu_color)
|
|
self._readout(draw, OTHER_BOX, power.other, "W", swatch=th.other_color)
|
|
self._readout(draw, TOTAL_BOX, power.total, "W", prefix="TOTAL")
|
|
|
|
small = canvas.convert("RGB").resize((W, H), Image.LANCZOS)
|
|
g_small = glow.resize((W, H), Image.BOX).filter(ImageFilter.GaussianBlur(2.0))
|
|
g_small = Image.eval(g_small, lambda v: min(255, int(v * 0.85)))
|
|
out = ImageChops.screen(small, g_small)
|
|
arr = np.asarray(out, dtype=float) * self._dome_shade
|
|
arr += (255 - arr) * self._dome_light
|
|
out = Image.fromarray(np.clip(arr, 0, 255).astype(np.uint8), "RGB")
|
|
if self.rotate180:
|
|
out = out.transpose(Image.ROTATE_180)
|
|
return out
|
|
|
|
def _arc_fill(self, draw, gdraw, cx, cy, frac, reverse, acc) -> None:
|
|
sweep = ARC_SWEEP * frac
|
|
if reverse:
|
|
end = ARC_START + ARC_SWEEP
|
|
start = end - sweep
|
|
else:
|
|
start, end = ARC_START, ARC_START + sweep
|
|
dark = tuple(int(c * 0.55) for c in acc)
|
|
light = tuple(min(255, int(c + (255 - c) * 0.45)) for c in acc)
|
|
layers = (
|
|
(ARC_R_OUT - 0.3, ARC_W - 0.6, dark),
|
|
(ARC_R_OUT - 0.9, ARC_W - 1.8, acc),
|
|
(ARC_R_OUT - 1.9, 1.1, light),
|
|
)
|
|
for rad, width, col in layers:
|
|
bb = [_s(cx - rad), _s(cy - rad), _s(cx + rad), _s(cy + rad)]
|
|
draw.arc(bb, start, end, fill=col, width=round(_s(width)))
|
|
bb = [_s(cx - ARC_R_OUT), _s(cy - ARC_R_OUT), _s(cx + ARC_R_OUT), _s(cy + ARC_R_OUT)]
|
|
gdraw.arc(bb, start, end, fill=acc, width=round(_s(ARC_W)))
|
|
|
|
def _digits(self, draw, gdraw, cx, cy, value) -> None:
|
|
text = "--" if value is None else str(int(round(value)))
|
|
pitch = 4.0 if len(text) <= 2 else 3.2 # 3 digits ("100") need a finer grid to fit
|
|
gap = pitch
|
|
total = len(text) * 5 * pitch + (len(text) - 1) * gap
|
|
left = cx - total / 2
|
|
top = cy - 7 * pitch / 2
|
|
rad = pitch * 0.40
|
|
for i, ch in enumerate(text):
|
|
glyph = GLYPHS.get(ch, GLYPHS["-"])
|
|
for row in range(7):
|
|
for col in range(5):
|
|
x = left + i * (5 * pitch + gap) + col * pitch + pitch / 2
|
|
y = top + row * pitch + pitch / 2
|
|
bb = [_s(x - rad), _s(y - rad), _s(x + rad), _s(y + rad)]
|
|
if glyph[row][col] == "#":
|
|
draw.ellipse(bb, fill=(248, 248, 250))
|
|
gdraw.ellipse(bb, fill=(150, 150, 155))
|
|
else:
|
|
draw.ellipse(bb, fill=(30, 30, 33))
|
|
|
|
def _liquid(self, canvas, power: PowerReadings) -> None:
|
|
"""Stack other / CPU / GPU from the bottom of the glass, scaled to the PSU rating."""
|
|
th = self.theme
|
|
gx0, gx1 = GLASS_X0, GLASS_X1
|
|
span = GLASS_Y1 - GLASS_Y0
|
|
layers = ((power.other, th.other_color), (power.cpu, th.cpu_color), (power.gpu, th.gpu_color))
|
|
base = GLASS_Y1
|
|
tops: list[tuple[float, tuple[int, int, int]]] = []
|
|
for watts, acc in layers:
|
|
if not watts or watts <= 0 or base <= GLASS_Y0:
|
|
continue
|
|
top = max(GLASS_Y0, base - span * watts / th.psu_watts)
|
|
lo = tuple(int(c * 0.22) for c in acc)
|
|
hi = tuple(min(255, int(c + (255 - c) * 0.12)) for c in acc)
|
|
cyl_rect(canvas, gx0, top, gx1, base, "x", lo, hi, hl=0.32, width=0.24, ambient=0.12)
|
|
tops.append((top, acc))
|
|
base = top
|
|
d = ImageDraw.Draw(canvas)
|
|
# a dark gap between layers, then a bright meniscus on the top one
|
|
for top, _ in tops[:-1]:
|
|
d.line([(_s(gx0), _s(top)), (_s(gx1), _s(top))], fill=(6, 6, 8), width=round(_s(0.5)))
|
|
if tops:
|
|
top, acc = tops[-1]
|
|
d.line([(_s(gx0 + 0.5), _s(top + 0.3)), (_s(gx1 - 0.5), _s(top + 0.3))],
|
|
fill=tuple(min(255, int(c + (255 - c) * 0.5)) for c in acc), width=round(_s(0.7)))
|
|
# glass reflections and scale on top of the liquid
|
|
ix0 = int(math.floor(gx0 * SS))
|
|
iy0 = int(math.floor(GLASS_Y0 * SS))
|
|
box = (ix0, iy0, int(math.ceil(gx1 * SS)), int(math.ceil(GLASS_Y1 * SS)))
|
|
canvas.alpha_composite(self._glass.crop(box), (ix0, iy0))
|
|
|
|
def _readout(self, draw, b, value, unit, swatch=None, prefix=None) -> None:
|
|
"""Value + unit centred in a box, optionally led by a colour swatch or a small label."""
|
|
x0, y0, x1, y1 = b
|
|
text = "--" if value is None else f"{value:.0f}"
|
|
gap = 1.2
|
|
wv = self.f_value.getlength(text) / SS
|
|
wu = self.f_small.getlength(unit) / SS
|
|
lead = 0.0
|
|
if swatch is not None:
|
|
lead = 4.0 + 2.6
|
|
elif prefix:
|
|
lead = self.f_small.getlength(prefix) / SS + 2.6
|
|
start = (x0 + x1) / 2 - (lead + wv + gap + wu) / 2
|
|
cy = (y0 + y1) / 2 + 0.3
|
|
if swatch is not None:
|
|
draw.rounded_rectangle([_s(start), _s(cy - 3.6), _s(start + 4.0), _s(cy + 3.4)],
|
|
radius=_s(1.0), fill=swatch)
|
|
elif prefix:
|
|
draw.text((_s(start), _s(cy + 1.0)), prefix, font=self.f_small, fill=(170, 170, 176), anchor="lm")
|
|
start += lead
|
|
draw.text((_s(start), _s(cy)), text, font=self.f_value, fill=(245, 245, 248), anchor="lm")
|
|
draw.text((_s(start + wv + gap), _s(cy + 1.0)), unit, font=self.f_small, fill=(170, 170, 176), anchor="lm")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
import time
|
|
|
|
ap = argparse.ArgumentParser(description="Render a preview of the dashboard to PNG")
|
|
ap.add_argument("out", nargs="?", default="preview.png")
|
|
ap.add_argument("--zoom", type=int, default=3)
|
|
args = ap.parse_args()
|
|
|
|
t0 = time.perf_counter()
|
|
r = DashboardRenderer()
|
|
t1 = time.perf_counter()
|
|
img = r.render(Readings(49, 11, 3114, 36.2), Readings(76, 88, 2700, 313),
|
|
PowerReadings(cpu=36.2, gpu=313, other=78))
|
|
t2 = time.perf_counter()
|
|
print(f"static build {1000 * (t1 - t0):.0f} ms, frame {1000 * (t2 - t1):.0f} ms")
|
|
img.save(args.out)
|
|
if args.zoom > 1:
|
|
root, ext = os.path.splitext(args.out)
|
|
img.resize((W * args.zoom, H * args.zoom), Image.NEAREST).save(f"{root}_x{args.zoom}{ext}")
|