Files
dsh-go 40b8c0f982 从历史会话工作区同步到 Gitea
由 dsh-go 自动上传 · 2026/11/18 20:35:22
2026-11-18 20:35:23 +08:00

113 lines
3.6 KiB
Python

"""视觉主题:配色、字体、圆角/渐变绘制工具。"""
from __future__ import annotations
import tkinter as tk
from tkinter import font as tkfont
from typing import Dict, Optional, Tuple
from PIL import Image, ImageDraw, ImageFilter
C = {
"bg0": "#05080E",
"bg1": "#0A111C",
"card": "#0C1622",
"card2": "#0F1B2A",
"card3": "#132235",
"border": "#1B2B3F",
"border2": "#24384F",
"text": "#E8EFF8",
"dim": "#8394AB",
"faint": "#586880",
"accent": "#5EEAD4",
"accent_dim": "#2C6E68",
"violet": "#A78BFA",
"sky": "#5BB8F5",
"warn": "#FBBF24",
"danger": "#FB7185",
"ok": "#34D399",
"ink": "#04121A",
"key": "#14202F",
"key_edge": "#1E2E42",
}
FONT_CN = "Microsoft YaHei UI"
FONT_EN = "Segoe UI"
FONT_MONO = "Consolas"
def pick_font(root: tk.Misc) -> Tuple[str, str]:
fams = set(tkfont.families(root))
cn = FONT_CN if FONT_CN in fams else ("Microsoft YaHei" if "Microsoft YaHei" in fams else "Segoe UI")
mono = FONT_MONO if FONT_MONO in fams else "Courier New"
return cn, mono
def rr_points(x1: float, y1: float, x2: float, y2: float, r: float):
r = max(0.0, min(r, (x2 - x1) / 2, (y2 - y1) / 2))
return [
x1 + r, y1, x2 - r, y1, x2, y1, x2, y1 + r,
x2, y2 - r, x2, y2, x2 - r, y2, x1 + r, y2,
x1, y2, x1, y2 - r, x1, y1 + r, x1, y1,
]
def rr(canvas: tk.Canvas, x1, y1, x2, y2, r=12, **kw):
"""圆角矩形(平滑多边形近似)。"""
return canvas.create_polygon(rr_points(x1, y1, x2, y2, r), smooth=True, **kw)
def card(canvas: tk.Canvas, x1, y1, x2, y2, r=16, fill=None, outline=None, width=1, tags=None):
f = fill or C["card"]
o = outline or C["border"]
item = rr(canvas, x1, y1, x2, y2, r, fill=f, outline=o, width=width, tags=tags)
# 顶部一道极淡的高光,制造玻璃感
canvas.create_line(x1 + r, y1 + 1, x2 - r, y1 + 1, fill="#1A2C40", width=1, tags=tags)
return item
def make_background(w: int, h: int) -> Image.Image:
"""深空渐变 + 两团光晕 + 极淡网格,预渲染成一张位图。"""
img = Image.new("RGB", (w, h), C["bg0"])
d = ImageDraw.Draw(img)
for y in range(h):
t = y / max(1, h - 1)
r = int(0x05 + (0x0A - 0x05) * t)
g = int(0x08 + (0x11 - 0x08) * t)
b = int(0x0E + (0x1C - 0x0E) * t)
d.line([(0, y), (w, y)], fill=(r, g, b))
glow = Image.new("RGB", (w, h), (0, 0, 0))
gd = ImageDraw.Draw(glow)
gd.ellipse([w * 0.52, -h * 0.55, w * 1.15, h * 0.55], fill=(14, 60, 72))
gd.ellipse([-w * 0.2, h * 0.45, w * 0.55, h * 1.35], fill=(30, 22, 62))
glow = glow.filter(ImageFilter.GaussianBlur(110))
img = Image.blend(img, Image.blend(img, glow, 0.55), 0.85)
grid = Image.new("RGBA", (w, h), (0, 0, 0, 0))
gdr = ImageDraw.Draw(grid)
for x in range(0, w, 48):
gdr.line([(x, 0), (x, h)], fill=(90, 140, 170, 10))
for y in range(0, h, 48):
gdr.line([(0, y), (w, y)], fill=(90, 140, 170, 10))
img = Image.alpha_composite(img.convert("RGBA"), grid).convert("RGB")
return img
def ellipsize(text: str, max_chars: int) -> str:
if len(text) <= max_chars:
return text
keep = max(3, max_chars - 1)
return text[: keep] + "…"
def fmt_ms(ms: float) -> str:
s = max(0.0, ms) / 1000.0
return f"{int(s // 60):02d}:{s % 60:05.2f}"
def mix(c1: str, c2: str, t: float) -> str:
t = max(0.0, min(1.0, t))
a = tuple(int(c1[i:i + 2], 16) for i in (1, 3, 5))
b = tuple(int(c2[i:i + 2], 16) for i in (1, 3, 5))
return "#%02X%02X%02X" % tuple(int(a[i] + (b[i] - a[i]) * t) for i in range(3))