QQgit/scripts/audit_colors.py

68 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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())