183 lines
6.2 KiB
Python
183 lines
6.2 KiB
Python
"""Минимальный набор линейных иконок для Xenith (24×24, штрих 1.5).
|
||
|
||
Рисуются прямо на tk.Canvas — без внешних ассетов и зависимостей.
|
||
Один набор, один штрих, один размер.
|
||
"""
|
||
|
||
import math
|
||
import os
|
||
import tkinter as tk
|
||
from functools import lru_cache
|
||
|
||
|
||
def draw(canvas: tk.Canvas, name: str, size: int = 18, color: str = "#ffffff",
|
||
stroke: float = 1.5, bg: str = None):
|
||
canvas.delete("all")
|
||
if bg:
|
||
try:
|
||
canvas.configure(bg=bg)
|
||
except Exception:
|
||
pass
|
||
|
||
s = size / 24.0
|
||
|
||
def line(pts, close=False, smooth=False):
|
||
xy = [v * s for v in pts]
|
||
if close:
|
||
canvas.create_polygon(*xy, outline=color, fill="", width=stroke,
|
||
smooth=smooth, joinstyle="round")
|
||
else:
|
||
canvas.create_line(*xy, fill=color, width=stroke, capstyle="round",
|
||
joinstyle="round", smooth=smooth)
|
||
|
||
def circle(cx, cy, r, filled=False):
|
||
canvas.create_oval((cx - r) * s, (cy - r) * s, (cx + r) * s, (cy + r) * s,
|
||
outline=color, fill=color if filled else "", width=stroke)
|
||
|
||
def arc(x1, y1, x2, y2, start, extent):
|
||
canvas.create_arc(x1 * s, y1 * s, x2 * s, y2 * s, start=start, extent=extent,
|
||
style="arc", outline=color, width=stroke)
|
||
|
||
if name == "chat":
|
||
line([5, 5, 19, 5, 20, 6, 20, 14, 19, 15, 10, 15, 7, 19, 7, 15, 5, 15, 4, 14, 4, 6],
|
||
close=True, smooth=True)
|
||
elif name == "layers":
|
||
line([12, 3, 20, 7, 12, 11, 4, 7], close=True)
|
||
line([4, 12, 12, 16, 20, 12])
|
||
line([4, 16, 12, 20, 20, 16])
|
||
elif name == "sliders":
|
||
line([4, 7, 20, 7])
|
||
line([4, 12, 20, 12])
|
||
line([4, 17, 20, 17])
|
||
circle(9, 7, 2)
|
||
circle(15, 12, 2)
|
||
circle(11, 17, 2)
|
||
elif name == "swap":
|
||
line([4, 9, 19, 9])
|
||
line([15, 5, 19, 9, 15, 13])
|
||
line([20, 15, 5, 15])
|
||
line([9, 11, 5, 15, 9, 19])
|
||
elif name == "play":
|
||
circle(12, 12, 8)
|
||
line([10, 8, 16, 12, 10, 16], close=True)
|
||
elif name == "server":
|
||
line([4, 4, 20, 4, 20, 10, 4, 10], close=True)
|
||
line([4, 13, 20, 13, 20, 19, 4, 19], close=True)
|
||
circle(8, 7, 1.2, True)
|
||
circle(8, 16, 1.2, True)
|
||
elif name == "chart":
|
||
line([4, 4, 4, 20, 20, 20])
|
||
line([6, 16, 10, 12, 13, 15, 19, 7])
|
||
elif name == "user":
|
||
circle(12, 8, 4)
|
||
arc(6, 12, 18, 23, 200, 140)
|
||
elif name == "code":
|
||
line([9, 8, 5, 12, 9, 16])
|
||
line([15, 8, 19, 12, 15, 16])
|
||
line([13.5, 6, 10.5, 18])
|
||
elif name == "plus":
|
||
line([12, 5, 12, 19])
|
||
line([5, 12, 19, 12])
|
||
elif name == "refresh":
|
||
arc(5, 5, 19, 19, 40, 280)
|
||
line([16.8, 4.2, 19.6, 7.6, 15.6, 8.4])
|
||
elif name == "gear":
|
||
pts = []
|
||
for i in range(8):
|
||
base = i * 45
|
||
for off, radius in ((-20, 6.4), (-10, 9.0), (10, 9.0), (20, 6.4)):
|
||
a = math.radians(base + off)
|
||
pts.extend([12 + radius * math.cos(a),
|
||
12 + radius * math.sin(a)])
|
||
line(pts, close=True)
|
||
circle(12, 12, 2.6)
|
||
elif name == "arrow_up":
|
||
line([12, 20, 12, 5])
|
||
line([6, 11, 12, 5, 18, 11])
|
||
elif name == "chevron_down":
|
||
line([8, 10, 12, 14, 16, 10])
|
||
elif name == "chevron_right":
|
||
line([10, 8, 14, 12, 10, 16])
|
||
elif name == "close":
|
||
line([6, 6, 18, 18])
|
||
line([18, 6, 6, 18])
|
||
elif name == "search":
|
||
circle(11, 11, 6)
|
||
line([15.5, 15.5, 20, 20])
|
||
elif name == "folder":
|
||
line([3, 7, 3, 19, 21, 19, 21, 9, 12, 9, 10, 7, 3, 7], close=True)
|
||
elif name == "download":
|
||
line([12, 4, 12, 15])
|
||
line([7, 11, 12, 16, 17, 11])
|
||
line([5, 20, 19, 20])
|
||
elif name == "trash":
|
||
line([4, 7, 20, 7])
|
||
line([9, 7, 9, 4, 15, 4, 15, 7])
|
||
line([6, 7, 7, 20, 17, 20, 18, 7])
|
||
elif name == "info":
|
||
circle(12, 12, 9)
|
||
line([12, 11, 12, 17])
|
||
circle(12, 7.5, 1, True)
|
||
elif name == "dot":
|
||
circle(12, 12, 5, True)
|
||
elif name == "more":
|
||
for cx in (6, 12, 18):
|
||
circle(cx, 12, 1.3, True)
|
||
elif name == "trace":
|
||
line([3, 19, 8, 12, 12, 15, 16, 8, 21, 10])
|
||
else:
|
||
circle(12, 12, 6)
|
||
|
||
|
||
# ---------------------------------------------------------------------------
|
||
# Растровый рендер для чётких иконок
|
||
# ---------------------------------------------------------------------------
|
||
try: # Pillow есть в собранном приложении; в dev-окружении допустим fallback.
|
||
from PIL import Image, ImageColor, ImageTk
|
||
_PIL_AVAILABLE = True
|
||
except Exception: # pragma: no cover - зависит от окружения
|
||
_PIL_AVAILABLE = False
|
||
|
||
|
||
def _pil_color(color: str):
|
||
try:
|
||
r, g, b = ImageColor.getrgb(str(color))[:3]
|
||
return (int(r), int(g), int(b), 255)
|
||
except Exception:
|
||
return (255, 255, 255, 255)
|
||
|
||
|
||
_ICON_DIR = os.path.join(
|
||
os.path.dirname(os.path.abspath(__file__)), "..", "assets", "icons"
|
||
)
|
||
|
||
|
||
@lru_cache(maxsize=64)
|
||
def _load_mask(name: str):
|
||
"""Белая PNG-маска, отрисованная из SVG через rsvg-convert."""
|
||
path = os.path.normpath(os.path.join(_ICON_DIR, str(name) + ".png"))
|
||
try:
|
||
if not os.path.isfile(path):
|
||
return None
|
||
return Image.open(path).convert("RGBA")
|
||
except Exception:
|
||
return None
|
||
|
||
|
||
def render_icon(name: str, size: int, color: str):
|
||
"""Возвращает сглаженную иконку нужного цвета, либо None без Pillow."""
|
||
if not _PIL_AVAILABLE or size <= 0:
|
||
return None
|
||
mask = _load_mask(name)
|
||
if mask is None:
|
||
return None
|
||
try:
|
||
tinted = Image.new("RGBA", mask.size, _pil_color(color))
|
||
tinted.putalpha(mask.getchannel("A"))
|
||
size = int(size)
|
||
if tinted.size != (size, size):
|
||
tinted = tinted.resize((size, size), Image.Resampling.LANCZOS)
|
||
return ImageTk.PhotoImage(tinted)
|
||
except Exception:
|
||
return None
|