resolve.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import type { ColorValue, DesktopTheme, HexColor, ResolvedTheme, ThemeVariant } from "./types"
  2. import { generateNeutralScale, generateScale, hexToOklch, oklchToHex, withAlpha } from "./color"
  3. export function resolveThemeVariant(variant: ThemeVariant, isDark: boolean): ResolvedTheme {
  4. const { seeds, overrides = {} } = variant
  5. const neutral = generateNeutralScale(seeds.neutral, isDark)
  6. const primary = generateScale(seeds.primary, isDark)
  7. const success = generateScale(seeds.success, isDark)
  8. const warning = generateScale(seeds.warning, isDark)
  9. const error = generateScale(seeds.error, isDark)
  10. const info = generateScale(seeds.info, isDark)
  11. const interactive = generateScale(seeds.interactive, isDark)
  12. const diffAdd = generateScale(seeds.diffAdd, isDark)
  13. const diffDelete = generateScale(seeds.diffDelete, isDark)
  14. const neutralAlpha = generateNeutralAlphaScale(neutral, isDark)
  15. const tokens: ResolvedTheme = {}
  16. tokens["background-base"] = neutral[0]
  17. tokens["background-weak"] = neutral[2]
  18. tokens["background-strong"] = neutral[0]
  19. tokens["background-stronger"] = isDark ? neutral[1] : "#fcfcfc"
  20. tokens["surface-base"] = neutralAlpha[1]
  21. tokens["base"] = neutralAlpha[1]
  22. tokens["surface-base-hover"] = neutralAlpha[2]
  23. tokens["surface-base-active"] = neutralAlpha[2]
  24. tokens["surface-base-interactive-active"] = withAlpha(interactive[2], 0.3) as ColorValue
  25. tokens["base2"] = neutralAlpha[1]
  26. tokens["base3"] = neutralAlpha[1]
  27. tokens["surface-inset-base"] = neutralAlpha[1]
  28. tokens["surface-inset-base-hover"] = neutralAlpha[2]
  29. tokens["surface-inset-strong"] = isDark
  30. ? (withAlpha(neutral[0], 0.5) as ColorValue)
  31. : (withAlpha(neutral[3], 0.09) as ColorValue)
  32. tokens["surface-inset-strong-hover"] = tokens["surface-inset-strong"]
  33. tokens["surface-raised-base"] = neutralAlpha[0]
  34. tokens["surface-float-base"] = isDark ? neutral[0] : neutral[11]
  35. tokens["surface-float-base-hover"] = isDark ? neutral[1] : neutral[10]
  36. tokens["surface-raised-base-hover"] = neutralAlpha[1]
  37. tokens["surface-raised-base-active"] = neutralAlpha[2]
  38. tokens["surface-raised-strong"] = isDark ? neutralAlpha[3] : neutral[0]
  39. tokens["surface-raised-strong-hover"] = isDark ? neutralAlpha[5] : "#ffffff"
  40. tokens["surface-raised-stronger"] = isDark ? neutralAlpha[5] : "#ffffff"
  41. tokens["surface-raised-stronger-hover"] = isDark ? neutralAlpha[6] : "#ffffff"
  42. tokens["surface-weak"] = neutralAlpha[2]
  43. tokens["surface-weaker"] = neutralAlpha[3]
  44. tokens["surface-strong"] = isDark ? neutralAlpha[6] : "#ffffff"
  45. tokens["surface-raised-stronger-non-alpha"] = isDark ? neutral[2] : "#ffffff"
  46. tokens["surface-brand-base"] = primary[8]
  47. tokens["surface-brand-hover"] = primary[9]
  48. tokens["surface-interactive-base"] = interactive[2]
  49. tokens["surface-interactive-hover"] = interactive[3]
  50. tokens["surface-interactive-weak"] = interactive[1]
  51. tokens["surface-interactive-weak-hover"] = interactive[2]
  52. tokens["surface-success-base"] = success[2]
  53. tokens["surface-success-weak"] = success[1]
  54. tokens["surface-success-strong"] = success[8]
  55. tokens["surface-warning-base"] = warning[2]
  56. tokens["surface-warning-weak"] = warning[1]
  57. tokens["surface-warning-strong"] = warning[8]
  58. tokens["surface-critical-base"] = error[2]
  59. tokens["surface-critical-weak"] = error[1]
  60. tokens["surface-critical-strong"] = error[8]
  61. tokens["surface-info-base"] = info[2]
  62. tokens["surface-info-weak"] = info[1]
  63. tokens["surface-info-strong"] = info[8]
  64. tokens["surface-diff-unchanged-base"] = isDark ? neutral[0] : "#ffffff00"
  65. tokens["surface-diff-skip-base"] = isDark ? neutralAlpha[0] : neutral[1]
  66. tokens["surface-diff-hidden-base"] = interactive[isDark ? 1 : 2]
  67. tokens["surface-diff-hidden-weak"] = interactive[isDark ? 0 : 1]
  68. tokens["surface-diff-hidden-weaker"] = interactive[isDark ? 2 : 0]
  69. tokens["surface-diff-hidden-strong"] = interactive[4]
  70. tokens["surface-diff-hidden-stronger"] = interactive[isDark ? 10 : 8]
  71. tokens["surface-diff-add-base"] = diffAdd[2]
  72. tokens["surface-diff-add-weak"] = diffAdd[isDark ? 3 : 1]
  73. tokens["surface-diff-add-weaker"] = diffAdd[isDark ? 2 : 0]
  74. tokens["surface-diff-add-strong"] = diffAdd[4]
  75. tokens["surface-diff-add-stronger"] = diffAdd[isDark ? 10 : 8]
  76. tokens["surface-diff-delete-base"] = diffDelete[2]
  77. tokens["surface-diff-delete-weak"] = diffDelete[isDark ? 3 : 1]
  78. tokens["surface-diff-delete-weaker"] = diffDelete[isDark ? 2 : 0]
  79. tokens["surface-diff-delete-strong"] = diffDelete[isDark ? 4 : 5]
  80. tokens["surface-diff-delete-stronger"] = diffDelete[isDark ? 10 : 8]
  81. tokens["input-base"] = isDark ? neutral[1] : neutral[0]
  82. tokens["input-hover"] = neutral[1]
  83. tokens["input-active"] = interactive[0]
  84. tokens["input-selected"] = interactive[3]
  85. tokens["input-focus"] = interactive[0]
  86. tokens["input-disabled"] = neutral[3]
  87. tokens["text-base"] = neutral[10]
  88. tokens["text-weak"] = neutral[8]
  89. tokens["text-weaker"] = neutral[7]
  90. tokens["text-strong"] = neutral[11]
  91. tokens["text-invert-base"] = isDark ? neutral[10] : neutralAlpha[10]
  92. tokens["text-invert-weak"] = isDark ? neutral[8] : neutralAlpha[8]
  93. tokens["text-invert-weaker"] = isDark ? neutral[7] : neutralAlpha[7]
  94. tokens["text-invert-strong"] = isDark ? neutral[11] : neutralAlpha[11]
  95. tokens["text-interactive-base"] = interactive[isDark ? 10 : 8]
  96. tokens["text-on-brand-base"] = neutralAlpha[10]
  97. tokens["text-on-interactive-base"] = isDark ? neutral[11] : neutral[0]
  98. tokens["text-on-interactive-weak"] = neutralAlpha[10]
  99. tokens["text-on-success-base"] = success[isDark ? 8 : 9]
  100. tokens["text-on-critical-base"] = error[isDark ? 8 : 9]
  101. tokens["text-on-critical-weak"] = error[7]
  102. tokens["text-on-critical-strong"] = error[11]
  103. tokens["text-on-warning-base"] = neutralAlpha[10]
  104. tokens["text-on-info-base"] = neutralAlpha[10]
  105. tokens["text-diff-add-base"] = diffAdd[10]
  106. tokens["text-diff-delete-base"] = diffDelete[isDark ? 8 : 9]
  107. tokens["text-diff-delete-strong"] = diffDelete[11]
  108. tokens["text-diff-add-strong"] = diffAdd[isDark ? 7 : 11]
  109. tokens["text-on-info-weak"] = neutralAlpha[8]
  110. tokens["text-on-info-strong"] = neutralAlpha[11]
  111. tokens["text-on-warning-weak"] = neutralAlpha[8]
  112. tokens["text-on-warning-strong"] = neutralAlpha[11]
  113. tokens["text-on-success-weak"] = success[isDark ? 7 : 5]
  114. tokens["text-on-success-strong"] = success[11]
  115. tokens["text-on-brand-weak"] = neutralAlpha[8]
  116. tokens["text-on-brand-weaker"] = neutralAlpha[7]
  117. tokens["text-on-brand-strong"] = neutralAlpha[11]
  118. tokens["button-secondary-base"] = isDark ? neutral[2] : neutral[0]
  119. tokens["button-secondary-hover"] = isDark ? neutral[3] : neutral[1]
  120. tokens["button-ghost-hover"] = neutralAlpha[1]
  121. tokens["button-ghost-hover2"] = neutralAlpha[2]
  122. tokens["border-base"] = neutralAlpha[6]
  123. tokens["border-hover"] = neutralAlpha[7]
  124. tokens["border-active"] = neutralAlpha[8]
  125. tokens["border-selected"] = withAlpha(interactive[8], isDark ? 0.9 : 0.99) as ColorValue
  126. tokens["border-disabled"] = neutralAlpha[7]
  127. tokens["border-focus"] = neutralAlpha[8]
  128. tokens["border-weak-base"] = neutralAlpha[isDark ? 5 : 4]
  129. tokens["border-strong-base"] = neutralAlpha[isDark ? 7 : 6]
  130. tokens["border-strong-hover"] = neutralAlpha[7]
  131. tokens["border-strong-active"] = neutralAlpha[isDark ? 7 : 6]
  132. tokens["border-strong-selected"] = withAlpha(interactive[5], 0.6) as ColorValue
  133. tokens["border-strong-disabled"] = neutralAlpha[5]
  134. tokens["border-strong-focus"] = neutralAlpha[isDark ? 7 : 6]
  135. tokens["border-weak-hover"] = neutralAlpha[isDark ? 6 : 5]
  136. tokens["border-weak-active"] = neutralAlpha[isDark ? 7 : 6]
  137. tokens["border-weak-selected"] = withAlpha(interactive[4], isDark ? 0.6 : 0.5) as ColorValue
  138. tokens["border-weak-disabled"] = neutralAlpha[5]
  139. tokens["border-weak-focus"] = neutralAlpha[isDark ? 7 : 6]
  140. tokens["border-weaker-base"] = neutralAlpha[2]
  141. tokens["border-weaker-hover"] = neutralAlpha[3]
  142. tokens["border-weaker-active"] = neutralAlpha[5]
  143. tokens["border-weaker-selected"] = withAlpha(interactive[3], isDark ? 0.3 : 0.4) as ColorValue
  144. tokens["border-weaker-disabled"] = neutralAlpha[1]
  145. tokens["border-weaker-focus"] = neutralAlpha[5]
  146. tokens["border-interactive-base"] = interactive[6]
  147. tokens["border-interactive-hover"] = interactive[7]
  148. tokens["border-interactive-active"] = interactive[8]
  149. tokens["border-interactive-selected"] = interactive[8]
  150. tokens["border-interactive-disabled"] = neutral[7]
  151. tokens["border-interactive-focus"] = interactive[8]
  152. tokens["border-success-base"] = success[5]
  153. tokens["border-success-hover"] = success[6]
  154. tokens["border-success-selected"] = success[8]
  155. tokens["border-warning-base"] = warning[5]
  156. tokens["border-warning-hover"] = warning[6]
  157. tokens["border-warning-selected"] = warning[8]
  158. tokens["border-critical-base"] = error[isDark ? 4 : 5]
  159. tokens["border-critical-hover"] = error[6]
  160. tokens["border-critical-selected"] = error[8]
  161. tokens["border-info-base"] = info[5]
  162. tokens["border-info-hover"] = info[6]
  163. tokens["border-info-selected"] = info[8]
  164. tokens["border-color"] = "#ffffff"
  165. tokens["icon-base"] = neutral[8]
  166. tokens["icon-hover"] = neutral[isDark ? 9 : 10]
  167. tokens["icon-active"] = neutral[isDark ? 10 : 11]
  168. tokens["icon-selected"] = neutral[11]
  169. tokens["icon-disabled"] = neutral[isDark ? 6 : 7]
  170. tokens["icon-focus"] = neutral[11]
  171. tokens["icon-invert-base"] = isDark ? neutral[0] : "#ffffff"
  172. tokens["icon-weak-base"] = neutral[isDark ? 5 : 6]
  173. tokens["icon-weak-hover"] = neutral[6]
  174. tokens["icon-weak-active"] = neutral[7]
  175. tokens["icon-weak-selected"] = neutral[8]
  176. tokens["icon-weak-disabled"] = neutral[isDark ? 3 : 5]
  177. tokens["icon-weak-focus"] = neutral[8]
  178. tokens["icon-strong-base"] = neutral[11]
  179. tokens["icon-strong-hover"] = isDark ? "#f6f3f3" : "#151313"
  180. tokens["icon-strong-active"] = isDark ? "#fcfcfc" : "#020202"
  181. tokens["icon-strong-selected"] = isDark ? "#fdfcfc" : "#020202"
  182. tokens["icon-strong-disabled"] = neutral[7]
  183. tokens["icon-strong-focus"] = isDark ? "#fdfcfc" : "#020202"
  184. tokens["icon-brand-base"] = isDark ? "#ffffff" : neutral[11]
  185. tokens["icon-interactive-base"] = interactive[8]
  186. tokens["icon-success-base"] = success[isDark ? 6 : 6]
  187. tokens["icon-success-hover"] = success[7]
  188. tokens["icon-success-active"] = success[10]
  189. tokens["icon-warning-base"] = warning[6]
  190. tokens["icon-warning-hover"] = warning[7]
  191. tokens["icon-warning-active"] = warning[10]
  192. tokens["icon-critical-base"] = error[isDark ? 8 : 9]
  193. tokens["icon-critical-hover"] = error[10]
  194. tokens["icon-critical-active"] = error[11]
  195. tokens["icon-info-base"] = info[isDark ? 6 : 6]
  196. tokens["icon-info-hover"] = info[7]
  197. tokens["icon-info-active"] = info[10]
  198. tokens["icon-on-brand-base"] = neutralAlpha[10]
  199. tokens["icon-on-brand-hover"] = neutralAlpha[11]
  200. tokens["icon-on-brand-selected"] = neutralAlpha[11]
  201. tokens["icon-on-interactive-base"] = isDark ? neutral[11] : neutral[0]
  202. tokens["icon-agent-plan-base"] = info[8]
  203. tokens["icon-agent-docs-base"] = warning[8]
  204. tokens["icon-agent-ask-base"] = interactive[8]
  205. tokens["icon-agent-build-base"] = interactive[isDark ? 10 : 8]
  206. tokens["icon-on-success-base"] = withAlpha(success[8], 0.9) as ColorValue
  207. tokens["icon-on-success-hover"] = withAlpha(success[9], 0.9) as ColorValue
  208. tokens["icon-on-success-selected"] = withAlpha(success[10], 0.9) as ColorValue
  209. tokens["icon-on-warning-base"] = withAlpha(warning[8], 0.9) as ColorValue
  210. tokens["icon-on-warning-hover"] = withAlpha(warning[9], 0.9) as ColorValue
  211. tokens["icon-on-warning-selected"] = withAlpha(warning[10], 0.9) as ColorValue
  212. tokens["icon-on-critical-base"] = withAlpha(error[8], 0.9) as ColorValue
  213. tokens["icon-on-critical-hover"] = withAlpha(error[9], 0.9) as ColorValue
  214. tokens["icon-on-critical-selected"] = withAlpha(error[10], 0.9) as ColorValue
  215. tokens["icon-on-info-base"] = info[8]
  216. tokens["icon-on-info-hover"] = withAlpha(info[9], 0.9) as ColorValue
  217. tokens["icon-on-info-selected"] = withAlpha(info[10], 0.9) as ColorValue
  218. tokens["icon-diff-add-base"] = diffAdd[10]
  219. tokens["icon-diff-add-hover"] = diffAdd[isDark ? 9 : 11]
  220. tokens["icon-diff-add-active"] = diffAdd[isDark ? 10 : 11]
  221. tokens["icon-diff-delete-base"] = diffDelete[isDark ? 8 : 9]
  222. tokens["icon-diff-delete-hover"] = diffDelete[isDark ? 9 : 10]
  223. tokens["syntax-comment"] = "var(--text-weak)"
  224. tokens["syntax-regexp"] = "var(--text-base)"
  225. tokens["syntax-string"] = isDark ? "#00ceb9" : "#006656"
  226. tokens["syntax-keyword"] = "var(--text-weak)"
  227. tokens["syntax-primitive"] = isDark ? "#ffba92" : "#fb4804"
  228. tokens["syntax-operator"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  229. tokens["syntax-variable"] = "var(--text-strong)"
  230. tokens["syntax-property"] = isDark ? "#ff9ae2" : "#ed6dc8"
  231. tokens["syntax-type"] = isDark ? "#ecf58c" : "#596600"
  232. tokens["syntax-constant"] = isDark ? "#93e9f6" : "#007b80"
  233. tokens["syntax-punctuation"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  234. tokens["syntax-object"] = "var(--text-strong)"
  235. tokens["syntax-success"] = success[9]
  236. tokens["syntax-warning"] = warning[9]
  237. tokens["syntax-critical"] = error[isDark ? 9 : 9]
  238. tokens["syntax-info"] = isDark ? "#93e9f6" : "#0092a8"
  239. tokens["syntax-diff-add"] = diffAdd[10]
  240. tokens["syntax-diff-delete"] = diffDelete[10]
  241. tokens["syntax-diff-unknown"] = "#ff0000"
  242. tokens["markdown-heading"] = isDark ? "#9d7cd8" : "#d68c27"
  243. tokens["markdown-text"] = isDark ? "#eeeeee" : "#1a1a1a"
  244. tokens["markdown-link"] = isDark ? "#fab283" : "#3b7dd8"
  245. tokens["markdown-link-text"] = isDark ? "#56b6c2" : "#318795"
  246. tokens["markdown-code"] = isDark ? "#7fd88f" : "#3d9a57"
  247. tokens["markdown-block-quote"] = isDark ? "#e5c07b" : "#b0851f"
  248. tokens["markdown-emph"] = isDark ? "#e5c07b" : "#b0851f"
  249. tokens["markdown-strong"] = isDark ? "#f5a742" : "#d68c27"
  250. tokens["markdown-horizontal-rule"] = isDark ? "#808080" : "#8a8a8a"
  251. tokens["markdown-list-item"] = isDark ? "#fab283" : "#3b7dd8"
  252. tokens["markdown-list-enumeration"] = isDark ? "#56b6c2" : "#318795"
  253. tokens["markdown-image"] = isDark ? "#fab283" : "#3b7dd8"
  254. tokens["markdown-image-text"] = isDark ? "#56b6c2" : "#318795"
  255. tokens["markdown-code-block"] = isDark ? "#eeeeee" : "#1a1a1a"
  256. tokens["avatar-background-pink"] = isDark ? "#501b3f" : "#feeef8"
  257. tokens["avatar-background-mint"] = isDark ? "#033a34" : "#e1fbf4"
  258. tokens["avatar-background-orange"] = isDark ? "#5f2a06" : "#fff1e7"
  259. tokens["avatar-background-purple"] = isDark ? "#432155" : "#f9f1fe"
  260. tokens["avatar-background-cyan"] = isDark ? "#0f3058" : "#e7f9fb"
  261. tokens["avatar-background-lime"] = isDark ? "#2b3711" : "#eefadc"
  262. tokens["avatar-text-pink"] = isDark ? "#e34ba9" : "#cd1d8d"
  263. tokens["avatar-text-mint"] = isDark ? "#95f3d9" : "#147d6f"
  264. tokens["avatar-text-orange"] = isDark ? "#ff802b" : "#ed5f00"
  265. tokens["avatar-text-purple"] = isDark ? "#9d5bd2" : "#8445bc"
  266. tokens["avatar-text-cyan"] = isDark ? "#369eff" : "#0894b3"
  267. tokens["avatar-text-lime"] = isDark ? "#c4f042" : "#5d770d"
  268. for (const [key, value] of Object.entries(overrides)) {
  269. tokens[key] = value
  270. }
  271. return tokens
  272. }
  273. function generateNeutralAlphaScale(neutralScale: HexColor[], isDark: boolean): HexColor[] {
  274. const alphas = isDark
  275. ? [0.02, 0.04, 0.08, 0.12, 0.16, 0.2, 0.26, 0.36, 0.44, 0.52, 0.72, 0.94]
  276. : [0.01, 0.03, 0.06, 0.09, 0.12, 0.15, 0.2, 0.27, 0.46, 0.61, 0.5, 0.87]
  277. return neutralScale.map((hex, i) => {
  278. const baseOklch = hexToOklch(hex)
  279. const targetL = isDark ? 0.1 + alphas[i] * 0.8 : 1 - alphas[i] * 0.8
  280. return oklchToHex({
  281. ...baseOklch,
  282. l: baseOklch.l * alphas[i] + targetL * (1 - alphas[i]),
  283. })
  284. })
  285. }
  286. export function resolveTheme(theme: DesktopTheme): { light: ResolvedTheme; dark: ResolvedTheme } {
  287. return {
  288. light: resolveThemeVariant(theme.light, false),
  289. dark: resolveThemeVariant(theme.dark, true),
  290. }
  291. }
  292. export function themeToCss(tokens: ResolvedTheme): string {
  293. return Object.entries(tokens)
  294. .map(([key, value]) => `--${key}: ${value};`)
  295. .join("\n ")
  296. }