resolve.ts 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. import type { ColorValue, DesktopTheme, HexColor, ResolvedTheme, ThemeVariant } from "./types"
  2. import { blend, generateNeutralScale, generateScale, hexToOklch, hexToRgb, shift, withAlpha } from "./color"
  3. export function resolveThemeVariant(variant: ThemeVariant, isDark: boolean): ResolvedTheme {
  4. const colors = getColors(variant)
  5. const { overrides = {} } = variant
  6. const neutral = generateNeutralScale(colors.neutral, isDark, colors.ink)
  7. const primary = generateScale(colors.primary, isDark)
  8. const accent = generateScale(colors.accent, isDark)
  9. const success = generateScale(colors.success, isDark)
  10. const warning = generateScale(colors.warning, isDark)
  11. const error = generateScale(colors.error, isDark)
  12. const info = generateScale(colors.info, isDark)
  13. const interactive = generateScale(colors.interactive, isDark)
  14. const hasInk = colors.compact && Boolean(colors.ink)
  15. const noInk = colors.compact && !hasInk
  16. const shadow = noInk && !isDark ? generateNeutralScale(colors.neutral, true) : neutral
  17. const amber = generateScale(
  18. shift(colors.warning, isDark ? { h: -16, l: -0.058, c: 1.14 } : { h: -22, l: -0.082, c: 0.94 }),
  19. isDark,
  20. )
  21. const blue = generateScale(shift(colors.interactive, { h: -12, l: 0.128, c: 1.12 }), isDark)
  22. const brandl = noInk && isDark ? generateScale(colors.primary, false) : primary
  23. const successl = noInk && isDark ? generateScale(colors.success, false) : success
  24. const warningl = noInk && isDark ? generateScale(colors.warning, false) : warning
  25. const infol = noInk && isDark ? generateScale(colors.info, false) : info
  26. const interl = noInk && isDark ? generateScale(colors.interactive, false) : interactive
  27. const diffAdd = generateScale(
  28. colors.diffAdd ??
  29. (noInk
  30. ? shift(colors.success, { c: isDark ? 0.54 : 0.6, l: isDark ? 0.22 : 0.16 })
  31. : shift(colors.success, { c: isDark ? 0.7 : 0.55, l: isDark ? -0.18 : 0.14 })),
  32. isDark,
  33. )
  34. const diffDelete = generateScale(
  35. colors.diffDelete ??
  36. (noInk ? colors.error : shift(colors.error, { c: isDark ? 0.82 : 0.7, l: isDark ? -0.08 : 0.08 })),
  37. isDark,
  38. )
  39. const ink = colors.ink ?? colors.neutral
  40. const tint = hasInk ? hexToOklch(ink) : undefined
  41. const body = tint
  42. ? shift(ink, {
  43. l: isDark ? Math.max(0, 0.88 - tint.l) * 0.4 : -Math.max(0, tint.l - 0.18) * 0.24,
  44. c: isDark ? 1.04 : 1.02,
  45. })
  46. : undefined
  47. const backgroundOverride = overrides["background-base"]
  48. const backgroundHex = getHex(backgroundOverride)
  49. const overlay = noInk || (Boolean(backgroundOverride) && !backgroundHex)
  50. const content = (seed: HexColor, scale: HexColor[]) => {
  51. const base = hexToOklch(seed)
  52. const value = isDark ? (base.l > 0.84 ? shift(seed, { c: 1.18 }) : scale[10]) : scale[10]
  53. return shift(value, { l: isDark ? 0.034 : -0.024, c: isDark ? 1.3 : 1.18 })
  54. }
  55. const modified = () => {
  56. if (!colors.compact) return isDark ? "#ffba92" : "#FF8C00"
  57. if (!hasInk) return isDark ? "#ffba92" : "#FF8C00"
  58. const warningHue = hexToOklch(colors.warning).h
  59. const deleteHue = hexToOklch(colors.diffDelete ?? colors.error).h
  60. const delta = Math.abs(((((deleteHue - warningHue) % 360) + 540) % 360) - 180)
  61. if (delta < 48) return isDark ? "#ffba92" : "#FF8C00"
  62. return content(colors.warning, warning)
  63. }
  64. const surface = (
  65. seed: HexColor,
  66. alpha: { base: number; weak: number; weaker: number; strong: number; stronger: number },
  67. ) => {
  68. const base = alphaTone(seed, alpha.base)
  69. return {
  70. base,
  71. weak: alphaTone(seed, alpha.weak),
  72. weaker: alphaTone(seed, alpha.weaker),
  73. strong: alphaTone(seed, alpha.strong),
  74. stronger: alphaTone(seed, alpha.stronger),
  75. }
  76. }
  77. const compactBackground =
  78. colors.compact && !hasInk
  79. ? isDark
  80. ? {
  81. base: shift(blend(colors.neutral, "#000000", 0.145), { c: 0 }),
  82. weak: shift(blend(colors.neutral, "#000000", 0.27), { c: 0 }),
  83. strong: shift(blend(colors.neutral, "#000000", 0.165), { c: 0 }),
  84. stronger: shift(blend(colors.neutral, "#000000", 0.19), { c: 0 }),
  85. }
  86. : {
  87. base: blend(colors.neutral, "#ffffff", 0.066),
  88. weak: blend(colors.neutral, "#ffffff", 0.11),
  89. strong: blend(colors.neutral, "#ffffff", 0.024),
  90. stronger: blend(colors.neutral, "#ffffff", 0.024),
  91. }
  92. : undefined
  93. const compactInkBackground =
  94. colors.compact && hasInk && isDark
  95. ? {
  96. base: neutral[0],
  97. weak: neutral[1],
  98. strong: neutral[0],
  99. stronger: neutral[2],
  100. }
  101. : undefined
  102. const background = backgroundHex ?? compactInkBackground?.base ?? compactBackground?.base ?? neutral[0]
  103. const alphaTone = (color: HexColor, alpha: number) =>
  104. overlay ? (withAlpha(color, alpha) as ColorValue) : blend(color, background, alpha)
  105. const borderTone = (light: number, dark: number) =>
  106. alphaTone(
  107. ink,
  108. isDark ? Math.min(1, dark + 0.024 + (colors.compact && hasInk ? 0.08 : 0)) : Math.min(1, light + 0.024),
  109. )
  110. const diffHiddenSurface = noInk
  111. ? {
  112. base: blue[isDark ? 1 : 2],
  113. weak: blue[isDark ? 0 : 1],
  114. weaker: blue[isDark ? 2 : 0],
  115. strong: blue[4],
  116. stronger: blue[isDark ? 10 : 8],
  117. }
  118. : surface(
  119. isDark ? shift(colors.interactive, { c: 0.55, l: 0 }) : shift(colors.interactive, { c: 0.45, l: 0.08 }),
  120. isDark
  121. ? { base: 0.14, weak: 0.08, weaker: 0.18, strong: 0.26, stronger: 0.42 }
  122. : { base: 0.12, weak: 0.08, weaker: 0.16, strong: 0.24, stronger: 0.36 },
  123. )
  124. const neutralAlpha = noInk ? generateNeutralOverlayScale(neutral, isDark) : generateNeutralAlphaScale(neutral, isDark)
  125. const brandb = brandl[isDark ? 9 : 8]
  126. const brandh = brandl[isDark ? 10 : 9]
  127. const interb = interactive[isDark ? 5 : 4]
  128. const interh = interactive[isDark ? 6 : 5]
  129. const interw = interactive[isDark ? 4 : 3]
  130. const succb = success[isDark ? 5 : 4]
  131. const succw = success[isDark ? 4 : 3]
  132. const succs = success[10]
  133. const warnb = (noInk && isDark ? warningl : warning)[isDark ? 5 : 4]
  134. const warnw = (noInk && isDark ? warningl : warning)[isDark ? 4 : 3]
  135. const warns = (noInk && isDark ? warningl : warning)[10]
  136. const critb = error[isDark ? 5 : 4]
  137. const critw = error[isDark ? 4 : 3]
  138. const crits = error[10]
  139. const infob = (noInk && isDark ? infol : info)[isDark ? 5 : 4]
  140. const infow = (noInk && isDark ? infol : info)[isDark ? 4 : 3]
  141. const infos = (noInk && isDark ? infol : info)[10]
  142. const lum = (hex: HexColor) => {
  143. const rgb = hexToRgb(hex)
  144. const lift = (v: number) => (v <= 0.04045 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4))
  145. return 0.2126 * lift(rgb.r) + 0.7152 * lift(rgb.g) + 0.0722 * lift(rgb.b)
  146. }
  147. const hit = (a: HexColor, b: HexColor) => {
  148. const x = lum(a)
  149. const y = lum(b)
  150. const light = Math.max(x, y)
  151. const dark = Math.min(x, y)
  152. return (light + 0.05) / (dark + 0.05)
  153. }
  154. const on = (fill: HexColor) => {
  155. const light = "#ffffff" as HexColor
  156. const dark = "#000000" as HexColor
  157. return hit(light, fill) > hit(dark, fill) ? light : dark
  158. }
  159. const tokens: ResolvedTheme = {}
  160. tokens["background-base"] = compactInkBackground?.base ?? compactBackground?.base ?? neutral[0]
  161. tokens["background-weak"] = compactInkBackground?.weak ?? compactBackground?.weak ?? neutral[2]
  162. tokens["background-strong"] = compactInkBackground?.strong ?? compactBackground?.strong ?? neutral[0]
  163. tokens["background-stronger"] =
  164. compactInkBackground?.stronger ?? compactBackground?.stronger ?? (isDark ? neutral[1] : "#fcfcfc")
  165. tokens["surface-base"] = neutralAlpha[1]
  166. tokens["base"] = neutralAlpha[1]
  167. tokens["surface-base-hover"] = neutralAlpha[2]
  168. tokens["surface-base-active"] = neutralAlpha[2]
  169. tokens["surface-base-interactive-active"] = withAlpha(interactive[2], 0.3) as ColorValue
  170. tokens["base2"] = neutralAlpha[1]
  171. tokens["base3"] = neutralAlpha[1]
  172. tokens["surface-inset-base"] = neutralAlpha[1]
  173. tokens["surface-inset-base-hover"] = neutralAlpha[2]
  174. tokens["surface-inset-strong"] = isDark
  175. ? (withAlpha(neutral[0], 0.5) as ColorValue)
  176. : (withAlpha(neutral[3], 0.09) as ColorValue)
  177. tokens["surface-inset-strong-hover"] = tokens["surface-inset-strong"]
  178. tokens["surface-raised-base"] = neutralAlpha[0]
  179. tokens["surface-float-base"] = isDark ? (hasInk ? neutral[1] : neutral[0]) : noInk ? shadow[0] : neutral[11]
  180. tokens["surface-float-base-hover"] = isDark ? (hasInk ? neutral[2] : neutral[1]) : noInk ? shadow[1] : neutral[10]
  181. tokens["surface-raised-base-hover"] = neutralAlpha[1]
  182. tokens["surface-raised-base-active"] = neutralAlpha[2]
  183. tokens["surface-raised-strong"] = isDark ? neutralAlpha[3] : neutral[0]
  184. tokens["surface-raised-strong-hover"] = isDark ? neutralAlpha[5] : "#ffffff"
  185. tokens["surface-raised-stronger"] = isDark ? neutralAlpha[5] : "#ffffff"
  186. tokens["surface-raised-stronger-hover"] = isDark ? neutralAlpha[6] : "#ffffff"
  187. tokens["surface-weak"] = neutralAlpha[2]
  188. tokens["surface-weaker"] = neutralAlpha[3]
  189. tokens["surface-strong"] = isDark ? neutralAlpha[6] : "#ffffff"
  190. tokens["surface-raised-stronger-non-alpha"] = isDark ? neutral[2] : "#ffffff"
  191. tokens["surface-brand-base"] = brandb
  192. tokens["surface-brand-hover"] = brandh
  193. tokens["surface-interactive-base"] = interb
  194. tokens["surface-interactive-hover"] = interh
  195. tokens["surface-interactive-weak"] = interw
  196. tokens["surface-interactive-weak-hover"] = noInk && isDark ? interl[5] : interb
  197. tokens["surface-success-base"] = succb
  198. tokens["surface-success-weak"] = succw
  199. tokens["surface-success-strong"] = succs
  200. tokens["surface-warning-base"] = warnb
  201. tokens["surface-warning-weak"] = warnw
  202. tokens["surface-warning-strong"] = warns
  203. tokens["surface-critical-base"] = critb
  204. tokens["surface-critical-weak"] = critw
  205. tokens["surface-critical-strong"] = crits
  206. tokens["surface-info-base"] = infob
  207. tokens["surface-info-weak"] = infow
  208. tokens["surface-info-strong"] = infos
  209. tokens["surface-diff-unchanged-base"] = isDark ? neutral[0] : "#ffffff00"
  210. tokens["surface-diff-skip-base"] = isDark ? neutralAlpha[0] : neutral[1]
  211. tokens["surface-diff-hidden-base"] = diffHiddenSurface.base
  212. tokens["surface-diff-hidden-weak"] = diffHiddenSurface.weak
  213. tokens["surface-diff-hidden-weaker"] = diffHiddenSurface.weaker
  214. tokens["surface-diff-hidden-strong"] = diffHiddenSurface.strong
  215. tokens["surface-diff-hidden-stronger"] = diffHiddenSurface.stronger
  216. tokens["surface-diff-add-base"] = diffAdd[2]
  217. tokens["surface-diff-add-weak"] = diffAdd[isDark ? 3 : 1]
  218. tokens["surface-diff-add-weaker"] = diffAdd[isDark ? 2 : 0]
  219. tokens["surface-diff-add-strong"] = diffAdd[4]
  220. tokens["surface-diff-add-stronger"] = diffAdd[isDark ? 10 : 8]
  221. tokens["surface-diff-delete-base"] = diffDelete[2]
  222. tokens["surface-diff-delete-weak"] = diffDelete[isDark ? 3 : 1]
  223. tokens["surface-diff-delete-weaker"] = diffDelete[isDark ? 2 : 0]
  224. tokens["surface-diff-delete-strong"] = diffDelete[isDark ? 4 : 5]
  225. tokens["surface-diff-delete-stronger"] = diffDelete[isDark ? 10 : 8]
  226. tokens["input-base"] = isDark ? neutral[1] : neutral[0]
  227. tokens["input-hover"] = neutral[1]
  228. tokens["input-active"] = interactive[0]
  229. tokens["input-selected"] = interactive[3]
  230. tokens["input-focus"] = interactive[0]
  231. tokens["input-disabled"] = neutral[3]
  232. tokens["text-base"] = hasInk ? (body as HexColor) : noInk ? (isDark ? neutralAlpha[10] : neutral[10]) : neutral[10]
  233. tokens["text-weak"] = hasInk
  234. ? shift(body as HexColor, { l: isDark ? -0.11 : 0.11, c: 0.9 })
  235. : noInk
  236. ? isDark
  237. ? neutralAlpha[8]
  238. : neutral[8]
  239. : neutral[8]
  240. tokens["text-weaker"] = hasInk
  241. ? shift(body as HexColor, { l: isDark ? -0.2 : 0.21, c: isDark ? 0.78 : 0.72 })
  242. : noInk
  243. ? isDark
  244. ? neutralAlpha[7]
  245. : neutral[7]
  246. : neutral[7]
  247. tokens["text-strong"] = hasInk
  248. ? isDark && colors.compact
  249. ? blend("#ffffff", body as HexColor, 0.9)
  250. : shift(body as HexColor, { l: isDark ? 0.055 : -0.07, c: 1.04 })
  251. : noInk
  252. ? isDark
  253. ? neutralAlpha[11]
  254. : neutral[11]
  255. : neutral[11]
  256. if (noInk && isDark) {
  257. tokens["text-base"] = withAlpha("#ffffff", 0.618) as ColorValue
  258. tokens["text-weak"] = withAlpha("#ffffff", 0.422) as ColorValue
  259. tokens["text-weaker"] = withAlpha("#ffffff", 0.284) as ColorValue
  260. tokens["text-strong"] = withAlpha("#ffffff", 0.936) as ColorValue
  261. }
  262. tokens["text-invert-base"] = isDark ? neutral[10] : neutral[1]
  263. tokens["text-invert-weak"] = isDark ? neutral[8] : neutral[2]
  264. tokens["text-invert-weaker"] = isDark ? neutral[7] : neutral[3]
  265. tokens["text-invert-strong"] = isDark ? neutral[11] : neutral[0]
  266. tokens["text-interactive-base"] = interactive[isDark ? 10 : 9]
  267. tokens["text-on-brand-base"] = on(brandb)
  268. tokens["text-on-interactive-base"] = on(interb)
  269. tokens["text-on-interactive-weak"] = on(interb)
  270. tokens["text-on-success-base"] = on(succb)
  271. tokens["text-on-critical-base"] = on(critb)
  272. tokens["text-on-critical-weak"] = on(critb)
  273. tokens["text-on-critical-strong"] = on(crits)
  274. tokens["text-on-warning-base"] = on(warnb)
  275. tokens["text-on-info-base"] = on(infob)
  276. tokens["text-diff-add-base"] = diffAdd[10]
  277. tokens["text-diff-delete-base"] = diffDelete[isDark ? 8 : 9]
  278. tokens["text-diff-delete-strong"] = diffDelete[11]
  279. tokens["text-diff-add-strong"] = diffAdd[isDark ? 7 : 11]
  280. tokens["text-on-info-weak"] = on(infob)
  281. tokens["text-on-info-strong"] = on(infos)
  282. tokens["text-on-warning-weak"] = on(warnb)
  283. tokens["text-on-warning-strong"] = on(warns)
  284. tokens["text-on-success-weak"] = on(succb)
  285. tokens["text-on-success-strong"] = on(succs)
  286. tokens["text-on-brand-weak"] = on(brandb)
  287. tokens["text-on-brand-weaker"] = on(brandb)
  288. tokens["text-on-brand-strong"] = on(brandh)
  289. tokens["button-primary-base"] = neutral[11]
  290. tokens["button-secondary-base"] = noInk ? (isDark ? neutral[1] : neutral[0]) : isDark ? neutral[2] : neutral[0]
  291. tokens["button-secondary-hover"] = noInk ? (isDark ? neutral[1] : neutral[1]) : isDark ? neutral[3] : neutral[1]
  292. tokens["button-ghost-hover"] = neutralAlpha[1]
  293. tokens["button-ghost-hover2"] = neutralAlpha[2]
  294. if (noInk) {
  295. const tone = (alpha: number) => alphaTone((isDark ? "#ffffff" : "#000000") as HexColor, alpha)
  296. if (isDark) {
  297. tokens["surface-base"] = tone(0.045)
  298. tokens["surface-base-hover"] = tone(0.065)
  299. tokens["surface-base-active"] = tone(0.095)
  300. tokens["surface-raised-base"] = tone(0.085)
  301. tokens["surface-raised-base-hover"] = tone(0.115)
  302. tokens["surface-raised-base-active"] = tone(0.15)
  303. tokens["surface-raised-strong"] = tone(0.115)
  304. tokens["surface-raised-strong-hover"] = tone(0.17)
  305. tokens["surface-raised-stronger"] = tone(0.17)
  306. tokens["surface-raised-stronger-hover"] = tone(0.22)
  307. tokens["surface-weak"] = tone(0.115)
  308. tokens["surface-weaker"] = tone(0.15)
  309. tokens["surface-strong"] = tone(0.22)
  310. tokens["surface-raised-stronger-non-alpha"] = neutral[1]
  311. tokens["surface-inset-base"] = withAlpha("#000000", 0.5) as ColorValue
  312. tokens["surface-inset-base-hover"] = tokens["surface-inset-base"]
  313. tokens["surface-inset-strong"] = withAlpha("#000000", 0.8) as ColorValue
  314. tokens["surface-inset-strong-hover"] = tokens["surface-inset-strong"]
  315. tokens["button-secondary-hover"] = tone(0.065)
  316. tokens["button-ghost-hover"] = tone(0.045)
  317. tokens["button-ghost-hover2"] = tone(0.095)
  318. tokens["input-base"] = neutral[1]
  319. tokens["input-hover"] = neutral[1]
  320. tokens["input-selected"] = interactive[1]
  321. tokens["surface-diff-skip-base"] = "#00000000"
  322. }
  323. if (!isDark) {
  324. tokens["surface-base"] = tone(0.045)
  325. tokens["surface-base-hover"] = tone(0.08)
  326. tokens["surface-base-active"] = tone(0.105)
  327. tokens["surface-raised-base"] = tone(0.05)
  328. tokens["surface-raised-base-hover"] = tone(0.08)
  329. tokens["surface-raised-base-active"] = tone(0.125)
  330. tokens["surface-raised-strong"] = neutral[0]
  331. tokens["surface-raised-strong-hover"] = "#ffffff"
  332. tokens["surface-raised-stronger"] = "#ffffff"
  333. tokens["surface-raised-stronger-hover"] = "#ffffff"
  334. tokens["surface-weak"] = tone(0.08)
  335. tokens["surface-weaker"] = tone(0.105)
  336. tokens["surface-strong"] = "#ffffff"
  337. tokens["surface-raised-stronger-non-alpha"] = "#ffffff"
  338. tokens["surface-inset-strong"] = tone(0.09)
  339. tokens["surface-inset-strong-hover"] = tokens["surface-inset-strong"]
  340. tokens["button-secondary-hover"] = blend("#ffffff", background, 0.04)
  341. tokens["button-ghost-hover"] = tone(0.045)
  342. tokens["button-ghost-hover2"] = tone(0.08)
  343. tokens["input-base"] = neutral[0]
  344. tokens["input-hover"] = neutral[1]
  345. }
  346. tokens["surface-base-interactive-active"] = withAlpha(colors.interactive, isDark ? 0.18 : 0.12) as ColorValue
  347. }
  348. tokens["border-base"] = hasInk ? borderTone(0.22, 0.16) : neutralAlpha[6]
  349. tokens["border-hover"] = hasInk ? borderTone(0.28, 0.2) : neutralAlpha[7]
  350. tokens["border-active"] = hasInk ? borderTone(0.34, 0.24) : neutralAlpha[8]
  351. tokens["border-selected"] = noInk
  352. ? isDark
  353. ? interactive[10]
  354. : (withAlpha(colors.interactive, 0.99) as ColorValue)
  355. : (withAlpha(interactive[8], isDark ? 0.9 : 0.99) as ColorValue)
  356. tokens["border-disabled"] = hasInk ? borderTone(0.18, 0.12) : neutralAlpha[7]
  357. tokens["border-focus"] = hasInk ? borderTone(0.34, 0.24) : neutralAlpha[8]
  358. tokens["border-weak-base"] = hasInk
  359. ? borderTone(0.1, 0.08)
  360. : noInk
  361. ? isDark
  362. ? neutral[3]
  363. : blend(neutral[4], neutral[5], 0.5)
  364. : neutralAlpha[isDark ? 5 : 4]
  365. tokens["border-strong-base"] = hasInk ? borderTone(0.34, 0.24) : neutralAlpha[isDark ? 7 : 6]
  366. tokens["border-strong-hover"] = hasInk ? borderTone(0.4, 0.28) : neutralAlpha[7]
  367. tokens["border-strong-active"] = hasInk ? borderTone(0.46, 0.32) : neutralAlpha[isDark ? 7 : 6]
  368. tokens["border-strong-selected"] = noInk
  369. ? (withAlpha(colors.interactive, isDark ? 0.62 : 0.31) as ColorValue)
  370. : (withAlpha(interactive[5], 0.6) as ColorValue)
  371. tokens["border-strong-disabled"] = hasInk ? borderTone(0.14, 0.1) : neutralAlpha[5]
  372. tokens["border-strong-focus"] = hasInk ? borderTone(0.46, 0.32) : neutralAlpha[isDark ? 7 : 6]
  373. tokens["border-weak-hover"] = hasInk ? borderTone(0.16, 0.12) : neutralAlpha[isDark ? 6 : 5]
  374. tokens["border-weak-active"] = hasInk ? borderTone(0.22, 0.16) : neutralAlpha[isDark ? 7 : 6]
  375. tokens["border-weak-selected"] = noInk
  376. ? (withAlpha(colors.interactive, isDark ? 0.62 : 0.24) as ColorValue)
  377. : (withAlpha(interactive[4], isDark ? 0.6 : 0.5) as ColorValue)
  378. tokens["border-weak-disabled"] = hasInk ? borderTone(0.08, 0.06) : neutralAlpha[5]
  379. tokens["border-weak-focus"] = hasInk ? borderTone(0.22, 0.16) : neutralAlpha[isDark ? 7 : 6]
  380. tokens["border-weaker-base"] = hasInk
  381. ? borderTone(0.06, 0.04)
  382. : noInk
  383. ? isDark
  384. ? blend(neutral[1], neutral[2], 0.5)
  385. : blend(neutral[2], neutral[3], 0.5)
  386. : neutralAlpha[2]
  387. if (noInk) {
  388. const line = (l: number, d: number) => alphaTone((isDark ? "#ffffff" : "#000000") as HexColor, isDark ? d : l)
  389. tokens["border-base"] = line(0.162, 0.195)
  390. tokens["border-hover"] = line(0.236, 0.284)
  391. tokens["border-active"] = line(0.46, 0.418)
  392. tokens["border-disabled"] = tokens["border-hover"]
  393. tokens["border-focus"] = tokens["border-active"]
  394. }
  395. tokens["border-interactive-base"] = (noInk && isDark ? interl : interactive)[7]
  396. tokens["border-interactive-hover"] = (noInk && isDark ? interl : interactive)[8]
  397. tokens["border-interactive-active"] = (noInk && isDark ? interl : interactive)[9]
  398. tokens["border-interactive-selected"] = (noInk && isDark ? interl : interactive)[9]
  399. tokens["border-interactive-disabled"] = neutral[7]
  400. tokens["border-interactive-focus"] = (noInk && isDark ? interl : interactive)[9]
  401. tokens["border-success-base"] = (noInk && isDark ? successl : success)[6]
  402. tokens["border-success-hover"] = (noInk && isDark ? successl : success)[7]
  403. tokens["border-success-selected"] = (noInk && isDark ? successl : success)[9]
  404. tokens["border-warning-base"] = (noInk && isDark ? warningl : warning)[6]
  405. tokens["border-warning-hover"] = (noInk && isDark ? warningl : warning)[7]
  406. tokens["border-warning-selected"] = (noInk && isDark ? warningl : warning)[9]
  407. tokens["border-critical-base"] = error[isDark ? 5 : 6]
  408. tokens["border-critical-hover"] = error[7]
  409. tokens["border-critical-selected"] = error[9]
  410. tokens["border-info-base"] = (noInk && isDark ? infol : info)[6]
  411. tokens["border-info-hover"] = (noInk && isDark ? infol : info)[7]
  412. tokens["border-info-selected"] = (noInk && isDark ? infol : info)[9]
  413. tokens["border-color"] = "#ffffff"
  414. tokens["icon-base"] = hasInk && !isDark ? tokens["text-weak"] : neutral[isDark ? 9 : 8]
  415. tokens["icon-hover"] = hasInk && !isDark ? tokens["text-base"] : neutral[10]
  416. tokens["icon-active"] = hasInk && !isDark ? tokens["text-strong"] : neutral[11]
  417. tokens["icon-selected"] = hasInk && !isDark ? tokens["text-strong"] : neutral[11]
  418. tokens["icon-disabled"] = neutral[isDark ? 6 : 7]
  419. tokens["icon-focus"] = hasInk && !isDark ? tokens["text-strong"] : neutral[11]
  420. tokens["icon-invert-base"] = isDark ? neutral[0] : "#ffffff"
  421. tokens["icon-weak-base"] = neutral[isDark ? 5 : 6]
  422. tokens["icon-weak-hover"] = noInk && isDark ? blend(neutral[11], neutral[10], 0.74) : neutral[isDark ? 11 : 7]
  423. tokens["icon-weak-active"] = noInk && isDark ? blend(neutral[11], neutral[10], 0.52) : neutral[8]
  424. tokens["icon-weak-selected"] = neutral[isDark ? 8 : 9]
  425. tokens["icon-weak-disabled"] = noInk && isDark ? neutral[11] : neutral[isDark ? 3 : 5]
  426. tokens["icon-weak-focus"] = neutral[8]
  427. tokens["icon-strong-base"] = neutral[11]
  428. tokens["icon-strong-hover"] = isDark ? "#f6f3f3" : "#151313"
  429. tokens["icon-strong-active"] = isDark ? "#fcfcfc" : "#020202"
  430. tokens["icon-strong-selected"] = isDark ? "#fdfcfc" : "#020202"
  431. tokens["icon-strong-disabled"] = noInk && isDark ? neutral[6] : neutral[7]
  432. tokens["icon-strong-focus"] = isDark ? "#fdfcfc" : "#020202"
  433. tokens["icon-brand-base"] = isDark ? "#ffffff" : neutral[11]
  434. tokens["icon-interactive-base"] = interactive[9]
  435. tokens["icon-success-base"] = success[isDark ? 8 : 6]
  436. tokens["icon-success-hover"] = success[isDark ? 9 : 7]
  437. tokens["icon-success-active"] = success[10]
  438. tokens["icon-warning-base"] = amber[isDark ? 8 : 6]
  439. tokens["icon-warning-hover"] = amber[7]
  440. tokens["icon-warning-active"] = amber[10]
  441. tokens["icon-critical-base"] = error[isDark ? 8 : 9]
  442. tokens["icon-critical-hover"] = error[10]
  443. tokens["icon-critical-active"] = error[11]
  444. tokens["icon-info-base"] = info[isDark ? 6 : 6]
  445. tokens["icon-info-hover"] = info[7]
  446. tokens["icon-info-active"] = info[10]
  447. tokens["icon-on-brand-base"] = on(brandb)
  448. tokens["icon-on-brand-hover"] = on(brandh)
  449. tokens["icon-on-brand-selected"] = on(brandh)
  450. tokens["icon-on-interactive-base"] = on(interb)
  451. tokens["icon-agent-plan-base"] = info[8]
  452. tokens["icon-agent-docs-base"] = amber[8]
  453. tokens["icon-agent-ask-base"] = blue[8]
  454. tokens["icon-agent-build-base"] = interactive[isDark ? 10 : 8]
  455. tokens["icon-on-success-base"] = on(succb)
  456. tokens["icon-on-success-hover"] = on(succs)
  457. tokens["icon-on-success-selected"] = on(succs)
  458. tokens["icon-on-warning-base"] = on(warnb)
  459. tokens["icon-on-warning-hover"] = on(warns)
  460. tokens["icon-on-warning-selected"] = on(warns)
  461. tokens["icon-on-critical-base"] = on(critb)
  462. tokens["icon-on-critical-hover"] = on(crits)
  463. tokens["icon-on-critical-selected"] = on(crits)
  464. tokens["icon-on-info-base"] = on(infob)
  465. tokens["icon-on-info-hover"] = on(infos)
  466. tokens["icon-on-info-selected"] = on(infos)
  467. tokens["icon-diff-add-base"] = diffAdd[10]
  468. tokens["icon-diff-add-hover"] = diffAdd[isDark ? 9 : 11]
  469. tokens["icon-diff-add-active"] = diffAdd[isDark ? 10 : 11]
  470. tokens["icon-diff-delete-base"] = diffDelete[isDark ? 8 : 9]
  471. tokens["icon-diff-delete-hover"] = diffDelete[isDark ? 9 : 10]
  472. tokens["icon-diff-modified-base"] = modified()
  473. if (colors.compact) {
  474. if (!hasInk) {
  475. tokens["syntax-comment"] = "var(--text-weak)"
  476. tokens["syntax-regexp"] = "var(--text-base)"
  477. tokens["syntax-string"] = isDark ? "#00ceb9" : "#006656"
  478. tokens["syntax-keyword"] = content(colors.accent, accent)
  479. tokens["syntax-primitive"] = isDark ? "#ffba92" : "#fb4804"
  480. tokens["syntax-operator"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  481. tokens["syntax-variable"] = "var(--text-strong)"
  482. tokens["syntax-property"] = isDark ? "#ff9ae2" : "#ed6dc8"
  483. tokens["syntax-type"] = isDark ? "#ecf58c" : "#596600"
  484. tokens["syntax-constant"] = isDark ? "#93e9f6" : "#007b80"
  485. tokens["syntax-punctuation"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  486. tokens["syntax-object"] = "var(--text-strong)"
  487. tokens["syntax-success"] = success[10]
  488. tokens["syntax-warning"] = amber[10]
  489. tokens["syntax-critical"] = error[10]
  490. tokens["syntax-info"] = isDark ? "#93e9f6" : "#0092a8"
  491. tokens["syntax-diff-add"] = diffAdd[10]
  492. tokens["syntax-diff-delete"] = diffDelete[10]
  493. tokens["syntax-diff-unknown"] = "#ff0000"
  494. tokens["markdown-heading"] = isDark ? "#9d7cd8" : "#d68c27"
  495. tokens["markdown-text"] = isDark ? "#eeeeee" : "#1a1a1a"
  496. tokens["markdown-link"] = isDark ? "#fab283" : "#3b7dd8"
  497. tokens["markdown-link-text"] = isDark ? "#56b6c2" : "#318795"
  498. tokens["markdown-code"] = isDark ? "#7fd88f" : "#3d9a57"
  499. tokens["markdown-block-quote"] = isDark ? "#e5c07b" : "#b0851f"
  500. tokens["markdown-emph"] = isDark ? "#e5c07b" : "#b0851f"
  501. tokens["markdown-strong"] = isDark ? "#f5a742" : "#d68c27"
  502. tokens["markdown-horizontal-rule"] = isDark ? "#808080" : "#8a8a8a"
  503. tokens["markdown-list-item"] = isDark ? "#fab283" : "#3b7dd8"
  504. tokens["markdown-list-enumeration"] = isDark ? "#56b6c2" : "#318795"
  505. tokens["markdown-image"] = isDark ? "#fab283" : "#3b7dd8"
  506. tokens["markdown-image-text"] = isDark ? "#56b6c2" : "#318795"
  507. tokens["markdown-code-block"] = isDark ? "#eeeeee" : "#1a1a1a"
  508. }
  509. if (hasInk) {
  510. tokens["syntax-comment"] = "var(--text-weak)"
  511. tokens["syntax-regexp"] = "var(--text-base)"
  512. tokens["syntax-string"] = content(colors.success, success)
  513. tokens["syntax-keyword"] = content(colors.accent, accent)
  514. tokens["syntax-primitive"] = content(colors.primary, primary)
  515. tokens["syntax-operator"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  516. tokens["syntax-variable"] = "var(--text-strong)"
  517. tokens["syntax-property"] = content(colors.info, info)
  518. tokens["syntax-type"] = content(colors.warning, warning)
  519. tokens["syntax-constant"] = content(colors.accent, accent)
  520. tokens["syntax-punctuation"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  521. tokens["syntax-object"] = "var(--text-strong)"
  522. tokens["syntax-success"] = success[10]
  523. tokens["syntax-warning"] = amber[10]
  524. tokens["syntax-critical"] = error[10]
  525. tokens["syntax-info"] = content(colors.info, info)
  526. tokens["syntax-diff-add"] = diffAdd[10]
  527. tokens["syntax-diff-delete"] = diffDelete[10]
  528. tokens["syntax-diff-unknown"] = "#ff0000"
  529. tokens["markdown-heading"] = content(colors.primary, primary)
  530. tokens["markdown-text"] = tokens["text-base"]
  531. tokens["markdown-link"] = content(colors.interactive, interactive)
  532. tokens["markdown-link-text"] = content(colors.info, info)
  533. tokens["markdown-code"] = content(colors.success, success)
  534. tokens["markdown-block-quote"] = content(colors.warning, warning)
  535. tokens["markdown-emph"] = content(colors.warning, warning)
  536. tokens["markdown-strong"] = content(colors.accent, accent)
  537. tokens["markdown-horizontal-rule"] = tokens["border-base"]
  538. tokens["markdown-list-item"] = content(colors.interactive, interactive)
  539. tokens["markdown-list-enumeration"] = content(colors.info, info)
  540. tokens["markdown-image"] = content(colors.interactive, interactive)
  541. tokens["markdown-image-text"] = content(colors.info, info)
  542. tokens["markdown-code-block"] = tokens["text-base"]
  543. }
  544. }
  545. if (!colors.compact) {
  546. tokens["syntax-comment"] = "var(--text-weak)"
  547. tokens["syntax-regexp"] = "var(--text-base)"
  548. tokens["syntax-string"] = isDark ? "#00ceb9" : "#006656"
  549. tokens["syntax-keyword"] = "var(--text-weak)"
  550. tokens["syntax-primitive"] = isDark ? "#ffba92" : "#fb4804"
  551. tokens["syntax-operator"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  552. tokens["syntax-variable"] = "var(--text-strong)"
  553. tokens["syntax-property"] = isDark ? "#ff9ae2" : "#ed6dc8"
  554. tokens["syntax-type"] = isDark ? "#ecf58c" : "#596600"
  555. tokens["syntax-constant"] = isDark ? "#93e9f6" : "#007b80"
  556. tokens["syntax-punctuation"] = isDark ? "var(--text-weak)" : "var(--text-base)"
  557. tokens["syntax-object"] = "var(--text-strong)"
  558. tokens["syntax-success"] = success[10]
  559. tokens["syntax-warning"] = amber[10]
  560. tokens["syntax-critical"] = error[10]
  561. tokens["syntax-info"] = isDark ? "#93e9f6" : "#0092a8"
  562. tokens["syntax-diff-add"] = diffAdd[10]
  563. tokens["syntax-diff-delete"] = diffDelete[10]
  564. tokens["syntax-diff-unknown"] = "#ff0000"
  565. tokens["markdown-heading"] = isDark ? "#9d7cd8" : "#d68c27"
  566. tokens["markdown-text"] = isDark ? "#eeeeee" : "#1a1a1a"
  567. tokens["markdown-link"] = isDark ? "#fab283" : "#3b7dd8"
  568. tokens["markdown-link-text"] = isDark ? "#56b6c2" : "#318795"
  569. tokens["markdown-code"] = isDark ? "#7fd88f" : "#3d9a57"
  570. tokens["markdown-block-quote"] = isDark ? "#e5c07b" : "#b0851f"
  571. tokens["markdown-emph"] = isDark ? "#e5c07b" : "#b0851f"
  572. tokens["markdown-strong"] = isDark ? "#f5a742" : "#d68c27"
  573. tokens["markdown-horizontal-rule"] = isDark ? "#808080" : "#8a8a8a"
  574. tokens["markdown-list-item"] = isDark ? "#fab283" : "#3b7dd8"
  575. tokens["markdown-list-enumeration"] = isDark ? "#56b6c2" : "#318795"
  576. tokens["markdown-image"] = isDark ? "#fab283" : "#3b7dd8"
  577. tokens["markdown-image-text"] = isDark ? "#56b6c2" : "#318795"
  578. tokens["markdown-code-block"] = isDark ? "#eeeeee" : "#1a1a1a"
  579. }
  580. tokens["avatar-background-pink"] = isDark ? "#501b3f" : "#feeef8"
  581. tokens["avatar-background-mint"] = isDark ? "#033a34" : "#e1fbf4"
  582. tokens["avatar-background-orange"] = isDark ? "#5f2a06" : "#fff1e7"
  583. tokens["avatar-background-purple"] = isDark ? "#432155" : "#f9f1fe"
  584. tokens["avatar-background-cyan"] = isDark ? "#0f3058" : "#e7f9fb"
  585. tokens["avatar-background-lime"] = isDark ? "#2b3711" : "#eefadc"
  586. tokens["avatar-text-pink"] = isDark ? "#e34ba9" : "#cd1d8d"
  587. tokens["avatar-text-mint"] = isDark ? "#95f3d9" : "#147d6f"
  588. tokens["avatar-text-orange"] = isDark ? "#ff802b" : "#ed5f00"
  589. tokens["avatar-text-purple"] = isDark ? "#9d5bd2" : "#8445bc"
  590. tokens["avatar-text-cyan"] = isDark ? "#369eff" : "#0894b3"
  591. tokens["avatar-text-lime"] = isDark ? "#c4f042" : "#5d770d"
  592. for (const [key, value] of Object.entries(overrides)) {
  593. tokens[key] = value
  594. }
  595. if (hasInk && "text-weak" in overrides && !("text-weaker" in overrides)) {
  596. const weak = tokens["text-weak"]
  597. if (weak.startsWith("#")) {
  598. tokens["text-weaker"] = shift(weak as HexColor, { l: isDark ? -0.12 : 0.12, c: 0.75 })
  599. } else {
  600. tokens["text-weaker"] = weak
  601. }
  602. }
  603. if (colors.compact && hasInk) {
  604. if (!("markdown-text" in overrides)) {
  605. tokens["markdown-text"] = tokens["text-base"]
  606. }
  607. if (!("markdown-code-block" in overrides)) {
  608. tokens["markdown-code-block"] = tokens["text-base"]
  609. }
  610. }
  611. if (!("text-stronger" in overrides)) {
  612. tokens["text-stronger"] = tokens["text-strong"]
  613. }
  614. return tokens
  615. }
  616. interface ThemeColors {
  617. compact: boolean
  618. neutral: HexColor
  619. ink?: HexColor
  620. primary: HexColor
  621. accent: HexColor
  622. success: HexColor
  623. warning: HexColor
  624. error: HexColor
  625. info: HexColor
  626. interactive: HexColor
  627. diffAdd?: HexColor
  628. diffDelete?: HexColor
  629. }
  630. function getColors(variant: ThemeVariant): ThemeColors {
  631. const input = variant as { palette?: unknown; seeds?: unknown }
  632. if (input.palette && input.seeds) {
  633. throw new Error("Theme variant cannot define both `palette` and `seeds`")
  634. }
  635. if (variant.palette) {
  636. return {
  637. compact: true,
  638. neutral: variant.palette.neutral,
  639. ink: variant.palette.ink,
  640. primary: variant.palette.primary,
  641. accent: variant.palette.accent ?? variant.palette.info,
  642. success: variant.palette.success,
  643. warning: variant.palette.warning,
  644. error: variant.palette.error,
  645. info: variant.palette.info,
  646. interactive: variant.palette.interactive ?? variant.palette.primary,
  647. diffAdd: variant.palette.diffAdd,
  648. diffDelete: variant.palette.diffDelete,
  649. }
  650. }
  651. if (variant.seeds) {
  652. return {
  653. compact: false,
  654. neutral: variant.seeds.neutral,
  655. ink: undefined,
  656. primary: variant.seeds.primary,
  657. accent: variant.seeds.info,
  658. success: variant.seeds.success,
  659. warning: variant.seeds.warning,
  660. error: variant.seeds.error,
  661. info: variant.seeds.info,
  662. interactive: variant.seeds.interactive,
  663. diffAdd: variant.seeds.diffAdd,
  664. diffDelete: variant.seeds.diffDelete,
  665. }
  666. }
  667. throw new Error("Theme variant requires `palette` or `seeds`")
  668. }
  669. function generateNeutralOverlayScale(neutralScale: HexColor[], isDark: boolean): ColorValue[] {
  670. const alphas = isDark
  671. ? [0, 0.034, 0.063, 0.084, 0.109, 0.138, 0.181, 0.266, 0.404, 0.468, 0.603, 0.928]
  672. : [0.014, 0.034, 0.055, 0.075, 0.096, 0.118, 0.151, 0.232, 0.453, 0.492, 0.574, 0.915]
  673. const color = (isDark ? "#ffffff" : "#000000") as HexColor
  674. return alphas.map((alpha) => withAlpha(color, alpha) as ColorValue)
  675. }
  676. function generateNeutralAlphaScale(neutralScale: HexColor[], isDark: boolean): HexColor[] {
  677. const alphas = isDark
  678. ? [0.05, 0.085, 0.13, 0.18, 0.24, 0.31, 0.4, 0.52, 0.64, 0.76, 0.88, 0.98]
  679. : [0.03, 0.06, 0.1, 0.145, 0.2, 0.265, 0.35, 0.47, 0.61, 0.74, 0.86, 0.97]
  680. return alphas.map((alpha) => blend(neutralScale[11], neutralScale[0], alpha))
  681. }
  682. function getHex(value: ColorValue | undefined): HexColor | undefined {
  683. if (!value?.startsWith("#")) return
  684. return value as HexColor
  685. }
  686. export function resolveTheme(theme: DesktopTheme): { light: ResolvedTheme; dark: ResolvedTheme } {
  687. return {
  688. light: resolveThemeVariant(theme.light, false),
  689. dark: resolveThemeVariant(theme.dark, true),
  690. }
  691. }
  692. export function themeToCss(tokens: ResolvedTheme): string {
  693. return Object.entries(tokens)
  694. .map(([key, value]) => `--${key}: ${value};`)
  695. .join("\n ")
  696. }