am-freescan/scripts/adapt_theme_styles.py
lixiaolong 64db825a9f feat: 完善设备工作流集成、组合执行与主题适配
集成旋转底座、机械臂示教与坐标系,完善扫描参数、电量和框架点复用。增加组合执行、步骤进度与日志,优化首屏和流程画布性能,修复表头主题适配,并保留验证脚本和回归测试。
2026-11-02 17:38:34 +08:00

90 lines
6.3 KiB
Python

"""Audit/patch authored UI styles to semantic tokens, without touching data or rendering palettes.
Default is a dry run. --patch emits a reviewable apply_patch payload; it never writes source files.
"""
import argparse, collections, colorsys, difflib, json, re, sys
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
SOURCE=ROOT/'am-web/src'
EXCLUDE=('components/DiyEditor/components/mobile/','components/Tinyflow/ui/','components/bpmnProcessDesigner/',
'views/mp/components/wx-msg/','styles/','/assets/styles/variables','/LayoutRadioPicker.vue',
'views/Login/','components/Verifition/')
PRIMARY={'#409eff','#2f6bff','#1677ff','#1890ff','#165dff','#1664ff','#2563eb','#2f80ed','#4b8cf5','#3978c5','#2f6fca','#5b8ff9'}
TEXT_PRIMARY={'#1d2b4a','#1f2937','#111827','#253450','#303133','#333','#333333','#2c3e50','#1a202c'}
TEXT_REGULAR={'#31415f','#344054','#475467','#475569','#526071','#5b6780','#606266','#4b5563','#374151'}
SUCCESS={'#67c23a','#16a34a','#168447','#1e7d49','#239565','#52c41a','#389e0d','#38a169','#0f8a73'}
DANGER={'#f56c6c','#ef4444','#f5222d','#b42318','#d45252','#ff4d4f','#f44336','#c33b32','#d93025'}
WARNING={'#e6a23c','#b7791f','#faad14','#e6a700','#d48806','#ffb300'}
HEX=re.compile(r'#[0-9a-fA-F]{3,6}\b')
DECL=re.compile(r'(?<![-\w])(?P<prop>background(?:-color)?|color|border(?:-(?:top|right|bottom|left))?(?:-color)?|outline(?:-color)?|fill|stroke)\s*:\s*(?P<value>[^;{}\n]+)')
def rgb(value):
v=value[1:];v=''.join(c*2 for c in v) if len(v)==3 else v
return tuple(int(v[i:i+2],16)/255 for i in (0,2,4))
def token(value,prop):
v=value.lower();r,g,b=rgb(v);h,l,s=colorsys.rgb_to_hls(r,g,b)
border=prop.startswith(('border','outline'));bg=prop.startswith('background')
if v in PRIMARY:return 'var(--el-color-primary)' if bg or border or prop in ('fill','stroke') else 'var(--app-primary-text)'
if v in SUCCESS:return f'var(--el-color-success)' if bg or border else 'var(--app-success-text)'
if v in DANGER:return f'var(--el-color-danger)' if bg or border else 'var(--app-danger-text)'
if v in WARNING:return f'var(--el-color-warning)' if bg or border else 'var(--app-warning-text)'
if border and (s<.45 or l>.8):return 'var(--el-border-color-lighter)' if l>.92 else 'var(--el-border-color)'
if bg:
if min(r,g,b)>.99:return 'var(--el-bg-color)'
if l>.9:
if s>.35:
family='danger' if h<.03 or h>.96 else 'warning' if h<.17 else 'success' if h<.48 else 'primary' if h<.76 else 'info'
return f'var(--el-color-{family}-light-9)'
return 'var(--el-fill-color-light)'
if l>.7 and s<.25:return 'var(--el-fill-color)'
return value
if v in ('#fff','#ffffff'):return 'var(--app-text-on-accent)'
if v in TEXT_PRIMARY:return 'var(--el-text-color-primary)'
if v in TEXT_REGULAR:return 'var(--el-text-color-regular)'
if s<.35 or (s<.55 and .28<l<.7):
return 'var(--el-text-color-primary)' if l<.25 else 'var(--el-text-color-regular)' if l<.43 else 'var(--el-text-color-secondary)' if l<.75 else 'var(--el-text-color-placeholder)'
return value
def declarations(css):
def convert(m):
prop,value=m['prop'],m['value']
if any(x in value for x in ('url(', 'lighten(', 'darken(', 'rgba(#','rgb(#')):return m[0]
value=HEX.sub(lambda c:token(c[0],prop) if len(c[0]) in (4,7) else c[0],value)
# White translucent toolbars must not remain white over a dark surface.
if prop.startswith(('background','border')):
value=re.sub(r'rgba\(\s*255\s*,\s*255\s*,\s*255\s*,\s*(0?\.[5-9]\d*)\s*\)', 'var(--el-border-color-light)' if prop.startswith('border') else 'var(--app-glass-bg)',value)
if prop=='color':value=value.replace('var(--el-color-primary)','var(--app-primary-text)')
value=re.sub(r'rgba\(\s*(?:47\s*,\s*(?:107|128)\s*,\s*(?:255|237)|64\s*,\s*158\s*,\s*255)\s*,\s*(0?\.\d+)\s*\)',lambda c:f'color-mix(in srgb, var(--el-color-primary) {float(c[1])*100:g}%, transparent)',value)
return m[0][:m.start('value')-m.start()]+value
return DECL.sub(convert,css)
def transform(source,suffix):
if suffix=='.vue':
source=re.sub(r'(<style\b[^>]*>)([\s\S]*?)(</style>)',lambda m:m[1]+declarations(m[2])+m[3],source)
# Only literal style attributes are changed; dynamic JS objects and data stay intact.
source=re.sub(r'(?<![:\w-])style="([^"<>]*)"',lambda m:'style="'+declarations(m[1])+'"',source)
# Replace authored utility classes rather than override every white element globally.
parts=re.split(r'(<script\b[\s\S]*?</script>|<style\b[\s\S]*?</style>)',source)
for old,new in {'bg-white':'bg-[var(--el-bg-color)]','bg-[#fff]':'bg-[var(--el-bg-color)]','bg-[#ffffff]':'bg-[var(--el-bg-color)]',
'bg-gray-50':'bg-[var(--el-fill-color-light)]','bg-gray-100':'bg-[var(--el-fill-color-light)]',
'text-gray-900':'text-[var(--el-text-color-primary)]','text-gray-800':'text-[var(--el-text-color-primary)]',
'text-gray-700':'text-[var(--el-text-color-regular)]','text-gray-600':'text-[var(--el-text-color-regular)]',
'text-gray-500':'text-[var(--el-text-color-secondary)]','text-gray-400':'text-[var(--el-text-color-placeholder)]',
'border-gray-200':'border-[var(--el-border-color)]','border-gray-300':'border-[var(--el-border-color)]'}.items():
for i in range(0,len(parts),2):parts[i]=re.sub(r'(?<![\w/-])'+re.escape(old)+r'(?![\w/-])',lambda m:new,parts[i])
return ''.join(parts)
return declarations(source)
def main():
p=argparse.ArgumentParser(description=__doc__);p.add_argument('--patch',action='store_true');p.add_argument('--scope',default='');a=p.parse_args();changes={};counts=collections.Counter()
for path in sorted(SOURCE.rglob('*')):
rel=path.relative_to(SOURCE).as_posix()
if path.suffix not in ('.vue','.scss','.css') or any(x in rel for x in EXCLUDE) or (a.scope and not rel.startswith(a.scope)):continue
try:old=path.read_text(encoding='utf-8')
except (PermissionError,UnicodeError):continue
new=transform(old,path.suffix)
if new!=old:changes[path]=(old,new);counts[rel.split('/')[0]]+=1
if not a.patch:print(json.dumps({'files':len(changes),'areas':counts,'paths':[str(p.relative_to(ROOT)) for p in changes]},ensure_ascii=False));return
print('*** Begin Patch')
for path,(old,new) in changes.items():
print('*** Update File: '+path.as_posix())
for line in list(difflib.unified_diff(old.splitlines(),new.splitlines(),n=3))[2:]:print('@@' if line.startswith('@@') else line)
print('*** End Patch')
if __name__=='__main__':main()