palettes.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. """
  2. xlsx skill — Palette System (Style-First Theme Engine)
  3. =======================================================
  4. 12 visual styles × scene-based fallback. No domain-color binding.
  5. Themes (12):
  6. professional, warm, elegant, creative,
  7. muji, aesop, kinfolk, celine, bottega, chanel, bloomberg, original_blue
  8. Matching priority:
  9. 1. Explicit style keywords in prompt → direct match
  10. 2. Scene/content keywords → infer style
  11. 3. No match → professional (safe default)
  12. Usage:
  13. from templates.palettes import resolve_palette, get_palette
  14. # Auto-detect from user prompt
  15. palette = resolve_palette("帮我做一个温暖的销售月报") # Chinese prompt example
  16. # → warm palette
  17. # Manual selection
  18. palette = get_palette("bottega")
  19. """
  20. from __future__ import annotations
  21. from typing import Dict, Optional, Tuple
  22. # ============================================================
  23. # §1 Palette Data Structure
  24. # ============================================================
  25. _Palette = Dict[str, str | list]
  26. def _make_palette(
  27. *,
  28. primary: str,
  29. primary_light: str,
  30. accent_positive: str = "1B7D46",
  31. accent_negative: str = "C0392B",
  32. accent_warning: str = "D4820A",
  33. neutral_900: str = "37352F",
  34. neutral_600: str = "8C8A84",
  35. neutral_200: str = "E9E9E8",
  36. neutral_100: str = "F7F7F5",
  37. neutral_50: str = "FAFAF9",
  38. neutral_0: str = "FFFFFF",
  39. header_text: str = "FFFFFF",
  40. cf_positive_bg: str = "E8F5E9",
  41. cf_negative_bg: str = "FDEDEC",
  42. cf_warning_bg: str = "FEF9E7",
  43. ) -> _Palette:
  44. return {
  45. "PRIMARY": primary,
  46. "PRIMARY_LIGHT": primary_light,
  47. "SECONDARY": primary_light,
  48. "ACCENT_POSITIVE": accent_positive,
  49. "ACCENT_NEGATIVE": accent_negative,
  50. "ACCENT_WARNING": accent_warning,
  51. "NEUTRAL_900": neutral_900,
  52. "NEUTRAL_600": neutral_600,
  53. "NEUTRAL_200": neutral_200,
  54. "NEUTRAL_100": neutral_100,
  55. "NEUTRAL_50": neutral_50,
  56. "NEUTRAL_0": neutral_0,
  57. "HEADER_TEXT": header_text,
  58. "CF_POSITIVE_BG": cf_positive_bg,
  59. "CF_NEGATIVE_BG": cf_negative_bg,
  60. "CF_WARNING_BG": cf_warning_bg,
  61. "CHART_COLORS": [primary, accent_positive, accent_warning, accent_negative, neutral_600],
  62. }
  63. # ============================================================
  64. # §2 Legacy Palettes (6 original styles)
  65. # ============================================================
  66. # -- Professional: formal business, universal default --
  67. PROFESSIONAL = _make_palette(
  68. primary="1B2A4A",
  69. primary_light="D6E4F0",
  70. )
  71. # -- Warm: warm and vibrant, high impact --
  72. WARM = _make_palette(
  73. primary="B85C1E",
  74. primary_light="F5E6D5",
  75. accent_positive="2E7D32",
  76. accent_negative="C62828",
  77. accent_warning="E65100",
  78. neutral_900="3E2F1F",
  79. neutral_600="9C8B78",
  80. neutral_200="EAE0D5",
  81. neutral_100="F7F2EC",
  82. neutral_50="FBF8F5",
  83. )
  84. # -- Fresh: natural freshness, friendly and light --
  85. FRESH = _make_palette(
  86. primary="0E7C6B",
  87. primary_light="D4F0EB",
  88. accent_positive="2E9E5A",
  89. accent_negative="D94F4F",
  90. accent_warning="E6A023",
  91. neutral_900="2F3735",
  92. neutral_600="7A8C87",
  93. neutral_200="DEE9E6",
  94. neutral_100="F2F8F6",
  95. neutral_50="F8FBFA",
  96. )
  97. # -- Elegant: premium restraint, minimalist black-white --
  98. ELEGANT = _make_palette(
  99. primary="2C2C2C",
  100. primary_light="E5E5E5",
  101. accent_positive="4A4A4A",
  102. accent_negative="8B0000",
  103. accent_warning="6B6B6B",
  104. neutral_900="1A1A1A",
  105. neutral_600="808080",
  106. neutral_200="D4D4D4",
  107. neutral_100="F0F0F0",
  108. neutral_50="F8F8F8",
  109. )
  110. # -- Creative: artistic personality, distinctive --
  111. CREATIVE = _make_palette(
  112. primary="6C5B7B",
  113. primary_light="E4DDE8",
  114. accent_positive="6B9E78",
  115. accent_negative="C06C7E",
  116. accent_warning="C4A46A",
  117. neutral_900="3E3A42",
  118. neutral_600="9590A0",
  119. neutral_200="E0DCE4",
  120. neutral_100="F3F1F5",
  121. neutral_50="F9F8FA",
  122. )
  123. # -- Vibrant: high-saturation multi-color, data display --
  124. VIBRANT = _make_palette(
  125. primary="2563EB",
  126. primary_light="DBEAFE",
  127. accent_positive="16A34A",
  128. accent_negative="DC2626",
  129. accent_warning="EA580C",
  130. neutral_900="1E293B",
  131. neutral_600="64748B",
  132. neutral_200="E2E8F0",
  133. neutral_100="F1F5F9",
  134. neutral_50="F8FAFC",
  135. )
  136. # ============================================================
  137. # §3 Premium Palettes (8 curated themes, "high-end feel" series)
  138. # ============================================================
  139. # -- A · MUJI breathing feel: restrained minimalism, pencil on paper --
  140. MUJI = _make_palette(
  141. primary="2C2C2C",
  142. primary_light="F2F1EE",
  143. accent_positive="5B8C5A",
  144. accent_negative="C25450",
  145. accent_warning="C9A84C",
  146. neutral_900="2C2C2C",
  147. neutral_600="999999",
  148. neutral_200="E8E6E1",
  149. neutral_100="F9F9F7",
  150. neutral_50="FCFCFB",
  151. header_text="FFFFFF",
  152. )
  153. # -- B · Aesop sandstone: earth tones, premium skincare packaging --
  154. AESOP = _make_palette(
  155. primary="3D3229",
  156. primary_light="EDE8E0",
  157. accent_positive="6B8F71",
  158. accent_negative="B85C4A",
  159. accent_warning="C4975A",
  160. neutral_900="4A4038",
  161. neutral_600="8C7B6B",
  162. neutral_200="DDD5C9",
  163. neutral_100="FAF8F5",
  164. neutral_50="FDFCFA",
  165. header_text="FFFFFF",
  166. )
  167. # -- C · Dieter Rams Industrial: Less but better --
  168. DIETER_RAMS = _make_palette(
  169. primary="1A1A1A",
  170. primary_light="F7F7F7",
  171. accent_positive="2D8C6F",
  172. accent_negative="D44D3C",
  173. accent_warning="D4920A",
  174. neutral_900="1A1A1A",
  175. neutral_600="787878",
  176. neutral_200="E5E5E5",
  177. neutral_100="F7F7F7",
  178. neutral_50="FAFAFA",
  179. header_text="FFFFFF",
  180. )
  181. # -- D · Kinfolk cream publication: independent magazine typography, slow-life aesthetic --
  182. KINFOLK = _make_palette(
  183. primary="5C524C",
  184. primary_light="F0ECE7",
  185. accent_positive="8DAA7F",
  186. accent_negative="C9776A",
  187. accent_warning="C9A96A",
  188. neutral_900="5C524C",
  189. neutral_600="BEB5AD",
  190. neutral_200="EAE5DF",
  191. neutral_100="FDFCFA",
  192. neutral_50="FEFDFB",
  193. header_text="FFFFFF",
  194. )
  195. # -- E · Céline pure black-white: monochrome, fashion house coldness --
  196. CELINE = _make_palette(
  197. primary="000000",
  198. primary_light="FAFAFA",
  199. accent_positive="4A7C59",
  200. accent_negative="A63D2F",
  201. accent_warning="8C7A3C",
  202. neutral_900="000000",
  203. neutral_600="ADADAD",
  204. neutral_200="E0E0E0",
  205. neutral_100="FAFAFA",
  206. neutral_50="FDFDFD",
  207. header_text="FFFFFF",
  208. )
  209. # -- F · Bottega dark green: Italian luxury, deep forest green --
  210. BOTTEGA = _make_palette(
  211. primary="2D4A3E",
  212. primary_light="E8F0EB",
  213. accent_positive="5FA67A",
  214. accent_negative="C2694B",
  215. accent_warning="B89B4A",
  216. neutral_900="3B5249",
  217. neutral_600="7A9B8C",
  218. neutral_200="D4E3DB",
  219. neutral_100="F6FAF8",
  220. neutral_50="F9FCFA",
  221. header_text="FFFFFF",
  222. )
  223. # -- G · Chanel champagne gold: Chanel elegance, beige + golden brown --
  224. CHANEL = _make_palette(
  225. primary="1C1917",
  226. primary_light="E7DFD4",
  227. accent_positive="A3845B",
  228. accent_negative="B0413E",
  229. accent_warning="C4975A",
  230. neutral_900="1C1917",
  231. neutral_600="A39888",
  232. neutral_200="E7E0D5",
  233. neutral_100="FDFBF7",
  234. neutral_50="FEFDFB",
  235. header_text="FFFFFF",
  236. )
  237. # -- H · Bloomberg deep blue: financial terminal, high-density data aesthetic --
  238. BLOOMBERG = _make_palette(
  239. primary="0D1B2A",
  240. primary_light="D6E0EB",
  241. accent_positive="10B981",
  242. accent_negative="EF4444",
  243. accent_warning="F59E0B",
  244. neutral_900="0D1B2A",
  245. neutral_600="708DA8",
  246. neutral_200="D6E0EB",
  247. neutral_100="F4F7FA",
  248. neutral_50="F8FAFB",
  249. header_text="FFFFFF",
  250. )
  251. # -- Original Blue/Black: original blue-black color scheme (Round 1 #1/#6 style) --
  252. ORIGINAL_BLUE = _make_palette(
  253. primary="1B2A4A",
  254. primary_light="D6E4F0",
  255. accent_positive="2E8B57",
  256. accent_negative="EB5757",
  257. accent_warning="F2994A",
  258. neutral_900="333333",
  259. neutral_600="666666",
  260. neutral_200="E0E0E0",
  261. neutral_100="F5F5F5",
  262. neutral_50="FAFAFA",
  263. )
  264. # ============================================================
  265. # §4 Registry
  266. # ============================================================
  267. PALETTE_REGISTRY: Dict[str, _Palette] = {
  268. # Legacy (removed: fresh, vibrant)
  269. "professional": PROFESSIONAL,
  270. "warm": WARM,
  271. "elegant": ELEGANT,
  272. "creative": CREATIVE,
  273. # Premium (high-end feel)
  274. "muji": MUJI,
  275. "aesop": AESOP,
  276. # dieter_rams removed — header too dark, poor readability
  277. "kinfolk": KINFOLK,
  278. "celine": CELINE,
  279. "bottega": BOTTEGA,
  280. "chanel": CHANEL,
  281. "bloomberg": BLOOMBERG,
  282. "original_blue": ORIGINAL_BLUE,
  283. }
  284. # Aliases for convenience
  285. PALETTE_REGISTRY["muji_breathing"] = MUJI
  286. PALETTE_REGISTRY["sandstone"] = AESOP
  287. PALETTE_REGISTRY["industrial"] = BLOOMBERG # was dieter_rams, redirected
  288. PALETTE_REGISTRY["cream"] = KINFOLK
  289. PALETTE_REGISTRY["monochrome"] = CELINE
  290. PALETTE_REGISTRY["forest_green"] = BOTTEGA
  291. PALETTE_REGISTRY["champagne"] = CHANEL
  292. PALETTE_REGISTRY["terminal"] = BLOOMBERG
  293. PALETTE_REGISTRY["classic_blue"] = ORIGINAL_BLUE
  294. # ============================================================
  295. # §5 Keyword Matching (three-step)
  296. # ============================================================
  297. # Step 1: Explicit style keywords (highest priority)
  298. _STYLE_KEYWORDS: Dict[str, list[str]] = {
  299. "professional": [
  300. "正式", "商务", "专业", "沉稳", "稳重", "professional", "formal",
  301. "corporate", "business",
  302. ],
  303. "warm": [
  304. "温暖", "活力", "热情", "热烈", "暖色", "温馨", "warm", "energetic",
  305. "活跃", "热力",
  306. ],
  307. "elegant": [
  308. "极简", "简约", "elegant", "minimal",
  309. "清新", "自然", "清爽", "淡雅", "浅色", "明亮", "fresh",
  310. "natural", "clean", "light", "素雅",
  311. "多彩", "丰富", "鲜艳", "vivid", "colorful", "明快",
  312. "高饱和", "鲜明", "亮色",
  313. ],
  314. "creative": [
  315. "文艺", "个性", "紫色", "莫兰迪", "creative", "artistic",
  316. "柔和", "雅致",
  317. ],
  318. # Premium themes
  319. "muji": [
  320. "muji", "无印", "呼吸感", "白纸", "铅笔", "素净", "无印良品",
  321. ],
  322. "aesop": [
  323. "aesop", "沙岩", "大地色", "护肤", "泥土", "陶", "terracotta",
  324. ],
  325. "bloomberg": [
  326. "bloomberg", "终端", "深蓝", "terminal", "金融终端", "数据终端",
  327. "rams", "dieter", "工业", "德系", "包豪斯", "bauhaus", "less but better",
  328. "工业风",
  329. ],
  330. "kinfolk": [
  331. "kinfolk", "奶油", "刊物", "杂志", "慢生活", "latte", "拿铁",
  332. ],
  333. "celine": [
  334. "celine", "黑白", "时装", "冷冽", "mono", "纯黑", "monochrome",
  335. ],
  336. "bottega": [
  337. "bottega", "墨绿", "深绿", "森林", "橄榄", "绿色", "forest",
  338. "贵气", "奢牌",
  339. ],
  340. "chanel": [
  341. "chanel", "米金", "金棕", "香奈儿", "champagne", "米色", "奶茶",
  342. ],
  343. "original_blue": [
  344. "原始", "经典蓝", "classic blue", "original", "传统蓝",
  345. ],
  346. }
  347. # Step 2: Scene keywords → infer style (lower priority)
  348. _SCENE_TO_STYLE: Dict[str, str] = {
  349. # Sales / Marketing / Ops → warm
  350. "销售": "warm", "营销": "warm", "运营": "warm", "客户": "warm",
  351. "业绩": "warm", "KPI": "warm", "GMV": "warm", "转化": "warm",
  352. "漏斗": "warm", "签约": "warm", "提成": "warm", "电商": "warm",
  353. "sales": "warm", "marketing": "warm", "campaign": "warm",
  354. # Education / Medical → muji (was fresh, now removed)
  355. "成绩": "muji", "考试": "muji", "学生": "muji", "课程": "muji",
  356. "教育": "muji", "GPA": "muji", "学校": "muji", "班级": "muji",
  357. "医疗": "muji", "健康": "muji", "患者": "muji", "体检": "muji",
  358. "医院": "muji", "科室": "muji", "护理": "muji",
  359. "环保": "muji",
  360. "education": "muji", "medical": "muji", "health": "muji",
  361. # Design / Brand → creative
  362. "设计": "creative", "创意": "creative", "品牌": "creative",
  363. "UI": "creative", "UX": "creative", "作品": "creative",
  364. "视觉": "creative", "素材": "creative",
  365. "design": "creative", "brand": "creative", "portfolio": "creative",
  366. # Formal / Reporting → professional
  367. "汇报": "professional", "提案": "professional", "会议": "professional",
  368. "述职": "professional", "总结": "professional", "报告": "professional",
  369. "年报": "professional", "季报": "professional", "月报": "professional",
  370. "财务": "professional", "财报": "professional", "预算": "professional",
  371. "审计": "professional", "咨询": "professional", "战略": "professional",
  372. "finance": "professional", "budget": "professional", "report": "professional",
  373. # Minimal / Premium → elegant
  374. "premium": "elegant", "luxury": "elegant",
  375. # Finance data → bloomberg
  376. "股票": "bloomberg", "基金": "bloomberg", "投资": "bloomberg",
  377. "交易": "bloomberg", "行情": "bloomberg", "K线": "bloomberg",
  378. "stock": "bloomberg", "trading": "bloomberg", "portfolio_fin": "bloomberg",
  379. # High-end / Luxury brand → chanel
  380. "奢侈": "chanel", "高端": "chanel", "高级": "chanel",
  381. }
  382. def _match_style_keywords(text: str) -> Optional[str]:
  383. """Step 1: Match explicit style keywords. Returns style name or None."""
  384. text_lower = text.lower()
  385. best_match = None
  386. best_score = 0
  387. for style, keywords in _STYLE_KEYWORDS.items():
  388. score = sum(1 for kw in keywords if kw.lower() in text_lower)
  389. if score > best_score:
  390. best_score = score
  391. best_match = style
  392. return best_match if best_score > 0 else None
  393. def _infer_from_scene(text: str) -> Optional[str]:
  394. """Step 2: Infer style from scene/content keywords. Returns style name or None."""
  395. text_lower = text.lower()
  396. votes: Dict[str, int] = {}
  397. for keyword, style in _SCENE_TO_STYLE.items():
  398. if keyword.lower() in text_lower:
  399. votes[style] = votes.get(style, 0) + 1
  400. if not votes:
  401. return None
  402. return max(votes, key=votes.get)
  403. # ============================================================
  404. # §6 Public API
  405. # ============================================================
  406. def get_palette(style: str = "professional") -> _Palette:
  407. """Get a palette by style name. Falls back to professional."""
  408. return PALETTE_REGISTRY.get(style, PROFESSIONAL)
  409. def resolve_palette(prompt: str) -> _Palette:
  410. """
  411. Auto-detect style from user prompt (three-step):
  412. 1. Explicit style keywords → direct match
  413. 2. Scene/content keywords → infer style
  414. 3. No match → professional (safe default)
  415. """
  416. style = detect_style(prompt)
  417. return get_palette(style)
  418. def resolve_palette_with_info(prompt: str) -> Tuple[_Palette, str]:
  419. """Same as resolve_palette but also returns the detected style name."""
  420. style = detect_style(prompt)
  421. return get_palette(style), style
  422. def detect_style(prompt: str) -> str:
  423. """
  424. Detect style from prompt. Three-step priority:
  425. 1. Explicit style keywords
  426. 2. Scene keywords → infer style
  427. 3. Default: professional
  428. """
  429. style = _match_style_keywords(prompt)
  430. if style:
  431. return style
  432. style = _infer_from_scene(prompt)
  433. if style:
  434. return style
  435. return "professional"
  436. def list_available() -> list[str]:
  437. """Return list of available style names (no aliases)."""
  438. # Return only canonical names, not aliases
  439. canonical = [
  440. "professional", "warm", "elegant", "creative",
  441. "muji", "aesop", "kinfolk", "celine", "bottega",
  442. "chanel", "bloomberg", "original_blue",
  443. ]
  444. return canonical
  445. def apply_palette(palette: _Palette, module_globals: dict):
  446. """
  447. Inject palette tokens into a module's global namespace.
  448. Designed to be called from base.py to override its color constants.
  449. """
  450. key_map = {
  451. "PRIMARY": "PRIMARY",
  452. "PRIMARY_LIGHT": "PRIMARY_LIGHT",
  453. "SECONDARY": "SECONDARY",
  454. "ACCENT_POSITIVE": "ACCENT_POSITIVE",
  455. "ACCENT_NEGATIVE": "ACCENT_NEGATIVE",
  456. "ACCENT_WARNING": "ACCENT_WARNING",
  457. "NEUTRAL_900": "NEUTRAL_900",
  458. "NEUTRAL_600": "NEUTRAL_600",
  459. "NEUTRAL_200": "NEUTRAL_200",
  460. "NEUTRAL_100": "NEUTRAL_100",
  461. "NEUTRAL_50": "NEUTRAL_50",
  462. "NEUTRAL_0": "NEUTRAL_0",
  463. "CHART_COLORS": "CHART_COLORS",
  464. }
  465. for palette_key, global_key in key_map.items():
  466. if palette_key in palette:
  467. module_globals[global_key] = palette[palette_key]