QQgit/scripts/audit_colors.py

68 行
2.2 KiB
Python
原始文件 Blame 文件历史

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""配色审计:确认主题文件里不再残留绿色相,并统计变量覆盖情况。
用法:python3 scripts/audit_colors.py
"""
import colorsys
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
CSS_DIR = ROOT / "custom" / "public" / "assets" / "css"
HEX_RE = re.compile(r"#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b")
GREEN_RANGE = (60, 170) # 色相区间:绿 → 黄绿
FILES = [
"theme-qq-auto.css",
"theme-qq-light.css",
"theme-qq-dark.css",
"qq-ui.css",
"qq-pages.css",
"qq-fx.css",
]
def hue_of(h):
h = h.lstrip("#")
if len(h) == 3:
h = "".join(c * 2 for c in h)
r, g, b = (int(h[i:i + 2], 16) / 255 for i in (0, 2, 4))
hh, ll, ss = colorsys.rgb_to_hls(r, g, b)
return hh * 360, ss, ll
def main():
bad_total = 0
for name in FILES:
path = CSS_DIR / name
if not path.exists():
print(f"· {name:22s} 跳过(不存在)")
continue
text = path.read_text(encoding="utf-8")
offenders = []
for m in HEX_RE.finditer(text):
# 跳过内联 SVG 蒙版与注释中的颜色
line_start = text.rfind("\n", 0, m.start()) + 1
line = text[line_start:text.find("\n", m.start())]
if "url(" in line or line.lstrip().startswith("*"):
continue
hue, sat, light = hue_of(m.group(1))
if GREEN_RANGE[0] <= hue <= GREEN_RANGE[1] and sat > 0.12 and 0.03 < light < 0.97:
offenders.append((m.group(0), round(hue), round(sat, 2)))
bad_total += len(offenders)
vars_n = len(re.findall(r"^\s*--[a-z0-9-]+:", text, re.M))
status = "OK" if not offenders else f"{len(offenders)} 处残留绿色"
print(f"· {name:22s} 变量 {vars_n:>4d} {status}")
for off in offenders[:8]:
print(f" {off[0]} hue={off[1]}° sat={off[2]}")
print()
print("审计结果:", "全部通过,无绿色残留" if bad_total == 0 else f"仍有 {bad_total} 处需处理")
return 0 if bad_total == 0 else 1
if __name__ == "__main__":
sys.exit(main())