@adea-ai/themes 0.11.19 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/NOTICE +34 -0
  2. package/README.md +9 -0
  3. package/dist/adapters/shiki.d.ts.map +1 -1
  4. package/dist/adapters/shiki.js +7 -9
  5. package/dist/adapters/shiki.js.map +1 -1
  6. package/dist/adapters/vscode.d.ts +99 -0
  7. package/dist/adapters/vscode.d.ts.map +1 -0
  8. package/dist/adapters/vscode.js +280 -0
  9. package/dist/adapters/vscode.js.map +1 -0
  10. package/dist/derive.d.ts +10 -5
  11. package/dist/derive.d.ts.map +1 -1
  12. package/dist/derive.js +25 -1
  13. package/dist/derive.js.map +1 -1
  14. package/dist/generated/themes/adea-dark-colorblind.d.ts.map +1 -1
  15. package/dist/generated/themes/adea-dark-colorblind.js +34 -1
  16. package/dist/generated/themes/adea-dark-colorblind.js.map +1 -1
  17. package/dist/generated/themes/adea-dark-high-contrast.d.ts.map +1 -1
  18. package/dist/generated/themes/adea-dark-high-contrast.js +34 -1
  19. package/dist/generated/themes/adea-dark-high-contrast.js.map +1 -1
  20. package/dist/generated/themes/adea-dark.d.ts.map +1 -1
  21. package/dist/generated/themes/adea-dark.js +34 -1
  22. package/dist/generated/themes/adea-dark.js.map +1 -1
  23. package/dist/generated/themes/adea-light-colorblind.d.ts.map +1 -1
  24. package/dist/generated/themes/adea-light-colorblind.js +34 -1
  25. package/dist/generated/themes/adea-light-colorblind.js.map +1 -1
  26. package/dist/generated/themes/adea-light-high-contrast.d.ts.map +1 -1
  27. package/dist/generated/themes/adea-light-high-contrast.js +34 -1
  28. package/dist/generated/themes/adea-light-high-contrast.js.map +1 -1
  29. package/dist/generated/themes/adea-light.d.ts.map +1 -1
  30. package/dist/generated/themes/adea-light.js +34 -1
  31. package/dist/generated/themes/adea-light.js.map +1 -1
  32. package/dist/generated/themes/ayu-light.d.ts.map +1 -1
  33. package/dist/generated/themes/ayu-light.js +34 -1
  34. package/dist/generated/themes/ayu-light.js.map +1 -1
  35. package/dist/generated/themes/ayu-mirage.d.ts.map +1 -1
  36. package/dist/generated/themes/ayu-mirage.js +34 -1
  37. package/dist/generated/themes/ayu-mirage.js.map +1 -1
  38. package/dist/generated/themes/ayu.d.ts.map +1 -1
  39. package/dist/generated/themes/ayu.js +34 -1
  40. package/dist/generated/themes/ayu.js.map +1 -1
  41. package/dist/generated/themes/catppuccin-frappe.d.ts.map +1 -1
  42. package/dist/generated/themes/catppuccin-frappe.js +34 -1
  43. package/dist/generated/themes/catppuccin-frappe.js.map +1 -1
  44. package/dist/generated/themes/catppuccin-latte.d.ts.map +1 -1
  45. package/dist/generated/themes/catppuccin-latte.js +34 -1
  46. package/dist/generated/themes/catppuccin-latte.js.map +1 -1
  47. package/dist/generated/themes/catppuccin-macchiato.d.ts.map +1 -1
  48. package/dist/generated/themes/catppuccin-macchiato.js +34 -1
  49. package/dist/generated/themes/catppuccin-macchiato.js.map +1 -1
  50. package/dist/generated/themes/catppuccin-mocha.d.ts.map +1 -1
  51. package/dist/generated/themes/catppuccin-mocha.js +34 -1
  52. package/dist/generated/themes/catppuccin-mocha.js.map +1 -1
  53. package/dist/generated/themes/dracula.d.ts.map +1 -1
  54. package/dist/generated/themes/dracula.js +34 -1
  55. package/dist/generated/themes/dracula.js.map +1 -1
  56. package/dist/generated/themes/everforest-dark.d.ts.map +1 -1
  57. package/dist/generated/themes/everforest-dark.js +34 -1
  58. package/dist/generated/themes/everforest-dark.js.map +1 -1
  59. package/dist/generated/themes/everforest-light.d.ts.map +1 -1
  60. package/dist/generated/themes/everforest-light.js +34 -1
  61. package/dist/generated/themes/everforest-light.js.map +1 -1
  62. package/dist/generated/themes/gruvbox-dark.d.ts.map +1 -1
  63. package/dist/generated/themes/gruvbox-dark.js +34 -1
  64. package/dist/generated/themes/gruvbox-dark.js.map +1 -1
  65. package/dist/generated/themes/gruvbox-light.d.ts.map +1 -1
  66. package/dist/generated/themes/gruvbox-light.js +34 -1
  67. package/dist/generated/themes/gruvbox-light.js.map +1 -1
  68. package/dist/generated/themes/kanagawa.d.ts.map +1 -1
  69. package/dist/generated/themes/kanagawa.js +34 -1
  70. package/dist/generated/themes/kanagawa.js.map +1 -1
  71. package/dist/generated/themes/monokai.d.ts.map +1 -1
  72. package/dist/generated/themes/monokai.js +34 -1
  73. package/dist/generated/themes/monokai.js.map +1 -1
  74. package/dist/generated/themes/nord.d.ts.map +1 -1
  75. package/dist/generated/themes/nord.js +34 -1
  76. package/dist/generated/themes/nord.js.map +1 -1
  77. package/dist/generated/themes/one-dark.d.ts.map +1 -1
  78. package/dist/generated/themes/one-dark.js +34 -1
  79. package/dist/generated/themes/one-dark.js.map +1 -1
  80. package/dist/generated/themes/rosepine-dawn.d.ts.map +1 -1
  81. package/dist/generated/themes/rosepine-dawn.js +34 -1
  82. package/dist/generated/themes/rosepine-dawn.js.map +1 -1
  83. package/dist/generated/themes/rosepine-moon.d.ts.map +1 -1
  84. package/dist/generated/themes/rosepine-moon.js +34 -1
  85. package/dist/generated/themes/rosepine-moon.js.map +1 -1
  86. package/dist/generated/themes/rosepine.d.ts.map +1 -1
  87. package/dist/generated/themes/rosepine.js +34 -1
  88. package/dist/generated/themes/rosepine.js.map +1 -1
  89. package/dist/generated/themes/solarized-dark.d.ts.map +1 -1
  90. package/dist/generated/themes/solarized-dark.js +34 -1
  91. package/dist/generated/themes/solarized-dark.js.map +1 -1
  92. package/dist/generated/themes/solarized-light.d.ts.map +1 -1
  93. package/dist/generated/themes/solarized-light.js +34 -1
  94. package/dist/generated/themes/solarized-light.js.map +1 -1
  95. package/dist/generated/themes/tokyonight-night.d.ts.map +1 -1
  96. package/dist/generated/themes/tokyonight-night.js +34 -1
  97. package/dist/generated/themes/tokyonight-night.js.map +1 -1
  98. package/dist/generated/themes/tokyonight-storm.d.ts.map +1 -1
  99. package/dist/generated/themes/tokyonight-storm.js +34 -1
  100. package/dist/generated/themes/tokyonight-storm.js.map +1 -1
  101. package/dist/generated/themes/vesper.d.ts.map +1 -1
  102. package/dist/generated/themes/vesper.js +34 -1
  103. package/dist/generated/themes/vesper.js.map +1 -1
  104. package/dist/index.d.ts +4 -2
  105. package/dist/index.d.ts.map +1 -1
  106. package/dist/index.js +2 -1
  107. package/dist/index.js.map +1 -1
  108. package/dist/schema.d.ts +25 -1
  109. package/dist/schema.d.ts.map +1 -1
  110. package/dist/schema.js +35 -0
  111. package/dist/schema.js.map +1 -1
  112. package/dist/sources.d.ts +39 -0
  113. package/dist/sources.d.ts.map +1 -1
  114. package/dist/sources.js +207 -0
  115. package/dist/sources.js.map +1 -1
  116. package/package.json +7 -1
  117. package/palettes/syntax/adea-dark-colorblind.json +34 -0
  118. package/palettes/syntax/adea-dark-high-contrast.json +34 -0
  119. package/palettes/syntax/adea-dark.json +34 -0
  120. package/palettes/syntax/adea-light-colorblind.json +34 -0
  121. package/palettes/syntax/adea-light-high-contrast.json +34 -0
  122. package/palettes/syntax/adea-light.json +34 -0
  123. package/palettes/syntax/ayu-light.json +34 -0
  124. package/palettes/syntax/ayu-mirage.json +34 -0
  125. package/palettes/syntax/ayu.json +34 -0
  126. package/palettes/syntax/catppuccin-frappe.json +34 -0
  127. package/palettes/syntax/catppuccin-latte.json +34 -0
  128. package/palettes/syntax/catppuccin-macchiato.json +34 -0
  129. package/palettes/syntax/catppuccin-mocha.json +34 -0
  130. package/palettes/syntax/dracula.json +34 -0
  131. package/palettes/syntax/everforest-dark.json +34 -0
  132. package/palettes/syntax/everforest-light.json +34 -0
  133. package/palettes/syntax/gruvbox-dark.json +34 -0
  134. package/palettes/syntax/gruvbox-light.json +34 -0
  135. package/palettes/syntax/kanagawa.json +34 -0
  136. package/palettes/syntax/monokai.json +34 -0
  137. package/palettes/syntax/nord.json +34 -0
  138. package/palettes/syntax/one-dark.json +34 -0
  139. package/palettes/syntax/rosepine-dawn.json +34 -0
  140. package/palettes/syntax/rosepine-moon.json +34 -0
  141. package/palettes/syntax/rosepine.json +34 -0
  142. package/palettes/syntax/solarized-dark.json +34 -0
  143. package/palettes/syntax/solarized-light.json +34 -0
  144. package/palettes/syntax/tokyonight-night.json +34 -0
  145. package/palettes/syntax/tokyonight-storm.json +34 -0
  146. package/palettes/syntax/vesper.json +34 -0
  147. package/src/adapters/shiki.ts +7 -9
  148. package/src/adapters/vscode.ts +319 -0
  149. package/src/derive.ts +36 -24
  150. package/src/generated/themes/adea-dark-colorblind.ts +34 -1
  151. package/src/generated/themes/adea-dark-high-contrast.ts +34 -1
  152. package/src/generated/themes/adea-dark.ts +34 -1
  153. package/src/generated/themes/adea-light-colorblind.ts +34 -1
  154. package/src/generated/themes/adea-light-high-contrast.ts +34 -1
  155. package/src/generated/themes/adea-light.ts +34 -1
  156. package/src/generated/themes/ayu-light.ts +34 -1
  157. package/src/generated/themes/ayu-mirage.ts +34 -1
  158. package/src/generated/themes/ayu.ts +34 -1
  159. package/src/generated/themes/catppuccin-frappe.ts +34 -1
  160. package/src/generated/themes/catppuccin-latte.ts +34 -1
  161. package/src/generated/themes/catppuccin-macchiato.ts +34 -1
  162. package/src/generated/themes/catppuccin-mocha.ts +34 -1
  163. package/src/generated/themes/dracula.ts +34 -1
  164. package/src/generated/themes/everforest-dark.ts +34 -1
  165. package/src/generated/themes/everforest-light.ts +34 -1
  166. package/src/generated/themes/gruvbox-dark.ts +34 -1
  167. package/src/generated/themes/gruvbox-light.ts +34 -1
  168. package/src/generated/themes/kanagawa.ts +34 -1
  169. package/src/generated/themes/monokai.ts +34 -1
  170. package/src/generated/themes/nord.ts +34 -1
  171. package/src/generated/themes/one-dark.ts +34 -1
  172. package/src/generated/themes/rosepine-dawn.ts +34 -1
  173. package/src/generated/themes/rosepine-moon.ts +34 -1
  174. package/src/generated/themes/rosepine.ts +34 -1
  175. package/src/generated/themes/solarized-dark.ts +34 -1
  176. package/src/generated/themes/solarized-light.ts +34 -1
  177. package/src/generated/themes/tokyonight-night.ts +34 -1
  178. package/src/generated/themes/tokyonight-storm.ts +34 -1
  179. package/src/generated/themes/vesper.ts +34 -1
  180. package/src/index.ts +12 -1
  181. package/src/schema.ts +55 -1
  182. package/src/sources.ts +247 -0
@@ -0,0 +1,319 @@
1
+ /**
2
+ * The editor adapter in the other direction: a VS Code colour theme as syntax roles.
3
+ *
4
+ * A palette's ANSI colours say what its author wanted a terminal to look like.
5
+ * What they wanted *code* to look like is in their editor theme, a VS Code
6
+ * `tokenColors` list of TextMate scope selectors, and that list is the de facto
7
+ * standard every editor theme is published in: Shiki, Zed's importer and most
8
+ * web highlighters read it. Reading the roles from that list, with the rules VS
9
+ * Code itself uses to pick a rule for a token, is what makes a theme picked here
10
+ * look the way it does in VS Code.
11
+ *
12
+ * ## How a role is resolved
13
+ *
14
+ * A role is not a scope, it is a token, so each role names the scope stack a
15
+ * TypeScript grammar gives a representative token of it ({@link ROLE_SCOPES}): an
16
+ * object key is `meta.objectliteral meta.object.member meta.object-literal.key`,
17
+ * a parameter `meta.parameters variable.parameter`. The stack is resolved the way
18
+ * `vscode-textmate` resolves it: each scope, from the outermost in, takes the
19
+ * foreground of the most specific rule that matches it (deepest selector first,
20
+ * then the rule with more matching parent selectors, then the later rule), and a
21
+ * scope no rule colours keeps its parent's colour, down to the theme's default
22
+ * ink.
23
+ *
24
+ * Themes that turn on semantic highlighting are coloured for TypeScript by the
25
+ * language server, not the grammar, for a handful of roles: VS Code reads those
26
+ * from `semanticTokenColors`, or failing that from the scope each semantic token
27
+ * falls back to ({@link SEMANTIC_ROLES}). The same order is applied here, so a
28
+ * theme like Catppuccin, whose object keys are coloured only semantically, keeps
29
+ * them.
30
+ */
31
+
32
+ import { SYNTAX_ROLES, type SyntaxRole, type ThemeSyntax } from '../schema.js'
33
+
34
+ /** One entry of a VS Code theme's `tokenColors`. */
35
+ export interface VsCodeTokenColor {
36
+ name?: string
37
+ scope?: string | readonly string[]
38
+ settings?: { foreground?: string; background?: string; fontStyle?: string }
39
+ }
40
+
41
+ /** The parts of a VS Code colour theme this adapter reads. */
42
+ export interface VsCodeTheme {
43
+ name?: string
44
+ type?: string
45
+ colors?: Readonly<Record<string, string | null | undefined>>
46
+ tokenColors?: readonly VsCodeTokenColor[]
47
+ semanticHighlighting?: boolean
48
+ semanticTokenColors?: Readonly<
49
+ Record<string, string | { foreground?: string } | null | undefined>
50
+ >
51
+ }
52
+
53
+ /**
54
+ * Role → the scope stack of a representative token, outermost first.
55
+ *
56
+ * TypeScript's grammar, because it is the language most of this product's code
57
+ * is in and the one every editor theme is tuned against. The three property roles
58
+ * are three different tokens to that grammar, and some themes colour them apart:
59
+ * Tokyo Night gives object keys a teal and member reads a blue of their own.
60
+ * `searchMatch` has no token: VS Code paints a match as a background, so it
61
+ * stays derived.
62
+ */
63
+ export const ROLE_SCOPES: Readonly<Partial<Record<SyntaxRole, readonly string[]>>> = Object.freeze({
64
+ keyword: ['source.ts', 'keyword.control.flow.ts'],
65
+ string: ['source.ts', 'string.quoted.single.ts'],
66
+ number: ['source.ts', 'constant.numeric.decimal.ts'],
67
+ comment: ['source.ts', 'comment.line.double-slash.ts'],
68
+ function: ['source.ts', 'meta.function-call.ts', 'entity.name.function.ts'],
69
+ variable: ['source.ts', 'variable.other.readwrite.ts'],
70
+ parameter: ['source.ts', 'meta.parameters.ts', 'variable.parameter.ts'],
71
+ property: [
72
+ 'source.ts',
73
+ 'meta.objectliteral.ts',
74
+ 'meta.object.member.ts',
75
+ 'meta.object-literal.key.ts',
76
+ ],
77
+ field: [
78
+ 'source.ts',
79
+ 'meta.interface.ts',
80
+ 'meta.field.declaration.ts',
81
+ 'meta.definition.property.ts',
82
+ 'variable.object.property.ts',
83
+ ],
84
+ member: ['source.ts', 'meta.var.expr.ts', 'variable.other.property.ts'],
85
+ storage: ['source.ts', 'meta.var.expr.ts', 'storage.type.ts'],
86
+ type: ['source.ts', 'meta.type.annotation.ts', 'entity.name.type.ts'],
87
+ tag: ['text.html.basic', 'meta.tag.structure.div.start.html', 'entity.name.tag.html'],
88
+ attribute: [
89
+ 'text.html.basic',
90
+ 'meta.tag.structure.div.start.html',
91
+ 'meta.attribute.class.html',
92
+ 'entity.other.attribute-name.html',
93
+ ],
94
+ operator: ['source.ts', 'keyword.operator.assignment.ts'],
95
+ heading: ['text.html.markdown', 'markup.heading.markdown', 'entity.name.section.markdown'],
96
+ link: ['text.html.markdown', 'meta.link.inline.markdown', 'markup.underline.link.markdown'],
97
+ constant: ['source.ts', 'constant.language.boolean.true.ts'],
98
+ punctuation: ['source.ts', 'punctuation.separator.comma.ts'],
99
+ diffAdd: ['source.diff', 'markup.inserted.diff'],
100
+ diffDelete: ['source.diff', 'markup.deleted.diff'],
101
+ diffHunk: ['source.diff', 'meta.diff.range.unified'],
102
+ })
103
+
104
+ /**
105
+ * Roles TypeScript's language server colours when a theme enables semantic
106
+ * highlighting: the `semanticTokenColors` keys VS Code looks up, most specific
107
+ * first, and the TextMate scope it falls back to when the theme names none.
108
+ */
109
+ export const SEMANTIC_ROLES: Readonly<
110
+ Partial<Record<SyntaxRole, { keys: readonly string[]; scope: string }>>
111
+ > = Object.freeze({
112
+ property: { keys: ['property.declaration', 'property'], scope: 'variable.other.property' },
113
+ field: { keys: ['property.declaration', 'property'], scope: 'variable.other.property' },
114
+ member: { keys: ['property'], scope: 'variable.other.property' },
115
+ parameter: { keys: ['parameter.declaration', 'parameter'], scope: 'variable.parameter' },
116
+ variable: { keys: ['variable'], scope: 'variable.other.readwrite' },
117
+ function: { keys: ['function'], scope: 'entity.name.function' },
118
+ type: { keys: ['type'], scope: 'entity.name.type' },
119
+ })
120
+
121
+ type Rule = {
122
+ /** The selector's last segment: the scope the rule colours. */
123
+ scope: string
124
+ /** The segments before it, innermost first, as `vscode-textmate` stores them. */
125
+ parents: readonly string[]
126
+ foreground: string
127
+ index: number
128
+ }
129
+
130
+ const HEX = /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i
131
+
132
+ function selectorsOf(scope: VsCodeTokenColor['scope']): string[] {
133
+ if (scope === undefined) return []
134
+ const list = typeof scope === 'string' ? scope.split(',') : scope.flatMap((s) => s.split(','))
135
+ return list.map((selector) => selector.trim()).filter(Boolean)
136
+ }
137
+
138
+ /** The theme's rules, one per selector, as `vscode-textmate`'s `parseTheme` reads them. */
139
+ function rulesOf(theme: VsCodeTheme): Rule[] {
140
+ const rules: Rule[] = []
141
+ for (const [index, entry] of (theme.tokenColors ?? []).entries()) {
142
+ const foreground = entry.settings?.foreground
143
+ if (typeof foreground !== 'string' || !HEX.test(foreground)) continue
144
+ for (const selector of selectorsOf(entry.scope)) {
145
+ const segments = selector.split(/\s+/).filter((segment) => segment !== '>')
146
+ const scope = segments.pop()
147
+ if (!scope) continue
148
+ rules.push({ scope, parents: segments.toReversed(), foreground, index })
149
+ }
150
+ }
151
+ return rules
152
+ }
153
+
154
+ /** True when `selector` names `scope` or one of its ancestors in the dot hierarchy. */
155
+ function scopeMatches(selector: string, scope: string): boolean {
156
+ return scope === selector || scope.startsWith(`${selector}.`)
157
+ }
158
+
159
+ /** True when each parent selector matches an enclosing scope, innermost first, in order. */
160
+ function parentsMatch(parents: readonly string[], enclosing: readonly string[]): boolean {
161
+ let at = enclosing.length - 1
162
+ for (const parent of parents) {
163
+ while (at >= 0 && !scopeMatches(parent, enclosing[at] as string)) at--
164
+ if (at < 0) return false
165
+ at--
166
+ }
167
+ return true
168
+ }
169
+
170
+ function depthOf(scope: string): number {
171
+ return scope.split('.').length
172
+ }
173
+
174
+ /** The foreground of the most specific rule that colours `scope` within `enclosing`. */
175
+ function matchScope(
176
+ rules: readonly Rule[],
177
+ scope: string,
178
+ enclosing: readonly string[]
179
+ ): string | undefined {
180
+ let best: Rule | undefined
181
+ for (const rule of rules) {
182
+ if (!scopeMatches(rule.scope, scope) || !parentsMatch(rule.parents, enclosing)) continue
183
+ if (!best) {
184
+ best = rule
185
+ continue
186
+ }
187
+ const depth = depthOf(rule.scope) - depthOf(best.scope)
188
+ const parents = rule.parents.length - best.parents.length
189
+ if (depth > 0 || (depth === 0 && (parents > 0 || (parents === 0 && rule.index > best.index)))) {
190
+ best = rule
191
+ }
192
+ }
193
+ return best?.foreground
194
+ }
195
+
196
+ /** The theme's default ink: the scopeless rule's foreground, else `editor.foreground`. */
197
+ function defaultForeground(theme: VsCodeTheme): string | undefined {
198
+ let found: string | undefined
199
+ for (const entry of theme.tokenColors ?? []) {
200
+ const foreground = entry.settings?.foreground
201
+ if (selectorsOf(entry.scope).length === 0 && typeof foreground === 'string') {
202
+ if (HEX.test(foreground)) found = foreground
203
+ }
204
+ }
205
+ const editor = theme.colors?.['editor.foreground']
206
+ return found ?? (typeof editor === 'string' && HEX.test(editor) ? editor : undefined)
207
+ }
208
+
209
+ /**
210
+ * The foreground VS Code gives a token with this scope stack (outermost first), or
211
+ * `undefined` when neither a rule nor the theme's default names one.
212
+ */
213
+ export function resolveScopeStack(
214
+ theme: VsCodeTheme,
215
+ stack: readonly string[],
216
+ options: { inherit?: boolean } = {}
217
+ ): string | undefined {
218
+ const rules = rulesOf(theme)
219
+ let foreground = options.inherit === false ? undefined : defaultForeground(theme)
220
+ for (const [at, scope] of stack.entries()) {
221
+ foreground = matchScope(rules, scope, stack.slice(0, at)) ?? foreground
222
+ }
223
+ return foreground
224
+ }
225
+
226
+ function semanticForeground(theme: VsCodeTheme, role: SyntaxRole): string | undefined {
227
+ const semantic = SEMANTIC_ROLES[role]
228
+ if (!semantic || theme.semanticHighlighting !== true) return undefined
229
+ // A rule for TypeScript is more specific than the same rule for every language.
230
+ for (const key of semantic.keys.flatMap((name) => [`${name}:typescript`, name])) {
231
+ const entry = theme.semanticTokenColors?.[key]
232
+ const foreground = typeof entry === 'string' ? entry : entry?.foreground
233
+ if (typeof foreground === 'string' && HEX.test(foreground)) return foreground
234
+ }
235
+ // A semantic token's fallback scope is matched on its own, with no grammar
236
+ // scopes around it, and colours the token only when some rule names it.
237
+ return resolveScopeStack(theme, [semantic.scope], { inherit: false })
238
+ }
239
+
240
+ function channel(hex: string, at: number): number {
241
+ return Number.parseInt(hex.slice(at, at + 2), 16)
242
+ }
243
+
244
+ function expand(hex: string): string {
245
+ const digits = hex.slice(1)
246
+ return digits.length <= 4
247
+ ? `#${[...digits].map((digit) => digit + digit).join('')}`
248
+ : `#${digits}`
249
+ }
250
+
251
+ /**
252
+ * A `#rgb[a]`/`#rrggbb[aa]` colour as opaque `#rrggbb`, with any alpha composited
253
+ * over `background` the way an editor paints it.
254
+ */
255
+ export function opaqueHex(color: string, background: string | undefined): string {
256
+ const full = expand(color.toLowerCase())
257
+ if (full.length === 7) return full
258
+ const alpha = channel(full, 7) / 255
259
+ const canvas = background && HEX.test(background) ? expand(background.toLowerCase()) : '#000000'
260
+ const mixed = [1, 3, 5].map((at) =>
261
+ Math.round(channel(full, at) * alpha + channel(canvas, at) * (1 - alpha))
262
+ )
263
+ return `#${mixed.map((value) => value.toString(16).padStart(2, '0')).join('')}`
264
+ }
265
+
266
+ /**
267
+ * The syntax roles a VS Code theme assigns, as opaque lowercase hex.
268
+ *
269
+ * Every role with a representative token is returned, including those the theme
270
+ * leaves to its default ink, because the default ink *is* what VS Code draws
271
+ * them in. Only `searchMatch`, which is not a token, is left out.
272
+ */
273
+ export function vsCodeSyntax(theme: VsCodeTheme): ThemeSyntax {
274
+ const background = theme.colors?.['editor.background'] ?? undefined
275
+ const syntax: Partial<Record<SyntaxRole, string>> = {}
276
+ for (const role of SYNTAX_ROLES) {
277
+ const stack = ROLE_SCOPES[role]
278
+ if (!stack) continue
279
+ const foreground = semanticForeground(theme, role) ?? resolveScopeStack(theme, stack)
280
+ if (foreground) syntax[role] = opaqueHex(foreground, background)
281
+ }
282
+ return syntax
283
+ }
284
+
285
+ /**
286
+ * Parses a VS Code theme file, which is JSON with comments and trailing commas
287
+ * (VS Code reads every theme as JSONC, and Nord, Tokyo Night and Vesper ship
288
+ * their themes that way).
289
+ */
290
+ export function parseVsCodeTheme(text: string): VsCodeTheme {
291
+ let out = ''
292
+ let inString = false
293
+ for (let at = 0; at < text.length; at++) {
294
+ const char = text[at] as string
295
+ if (inString) {
296
+ out += char
297
+ if (char === '\\') out += text[++at] ?? ''
298
+ else if (char === '"') inString = false
299
+ continue
300
+ }
301
+ if (char === '"') {
302
+ inString = true
303
+ out += char
304
+ } else if (char === '/' && text[at + 1] === '/') {
305
+ while (at < text.length && text[at] !== '\n') at++
306
+ out += '\n'
307
+ } else if (char === '/' && text[at + 1] === '*') {
308
+ const end = text.indexOf('*/', at + 2)
309
+ at = end === -1 ? text.length : end + 1
310
+ } else {
311
+ out += char
312
+ }
313
+ }
314
+ const parsed: unknown = JSON.parse(out.replace(/,(\s*[}\]])/g, '$1'))
315
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
316
+ throw new Error('a VS Code theme is a JSON object')
317
+ }
318
+ return parsed as VsCodeTheme
319
+ }
package/src/derive.ts CHANGED
@@ -29,49 +29,43 @@
29
29
  * palettes today.
30
30
  */
31
31
 
32
- import type { AdeaTheme } from './schema.js'
32
+ import type { AdeaTheme, SyntaxRole } from './schema.js'
33
33
  import type { Oklch } from './oklch.js'
34
34
  import { contrastRatio, formatOklch, mix, oklchToHex, parseColor, repairContrast } from './oklch.js'
35
35
  import { CONTRAST_FLOORS, clearsFloor } from './policy.js'
36
36
  import { mostContrasting, repairToHex, toHex } from './render.js'
37
37
 
38
- /** The syntax roles, matching the names Shiki's theme contract expects. */
39
- export type SyntaxRole =
40
- | 'keyword'
41
- | 'string'
42
- | 'number'
43
- | 'comment'
44
- | 'function'
45
- | 'variable'
46
- | 'type'
47
- | 'tag'
48
- | 'attribute'
49
- | 'operator'
50
- | 'heading'
51
- | 'link'
52
- | 'constant'
53
- | 'punctuation'
54
- | 'diffAdd'
55
- | 'diffDelete'
56
- | 'diffHunk'
57
- | 'searchMatch'
38
+ export type { SyntaxRole } from './schema.js'
58
39
 
59
- /** Roles in the product's editor palette, which are all read as small text. */
60
- export type EditorRole = Exclude<SyntaxRole, 'constant' | 'punctuation'>
40
+ /**
41
+ * Roles in the product's editor palette, which are all read as small text.
42
+ *
43
+ * Every syntax role: `constant` and `punctuation` joined it when the editor
44
+ * started painting from the theme's own editor theme, where `true` and a comma
45
+ * are each given a colour of their own.
46
+ */
47
+ export type EditorRole = SyntaxRole
61
48
 
62
49
  const EDITOR_ROLES = [
63
50
  'keyword',
51
+ 'storage',
64
52
  'string',
65
53
  'number',
66
54
  'comment',
67
55
  'function',
68
56
  'variable',
57
+ 'parameter',
58
+ 'property',
59
+ 'field',
60
+ 'member',
69
61
  'type',
70
62
  'tag',
71
63
  'attribute',
72
64
  'operator',
73
65
  'heading',
74
66
  'link',
67
+ 'constant',
68
+ 'punctuation',
75
69
  'diffAdd',
76
70
  'diffDelete',
77
71
  'diffHunk',
@@ -95,6 +89,10 @@ const EDITOR_ROLES = [
95
89
  const LIGHT_SYNTAX_SOURCE: Readonly<Partial<Record<SyntaxRole, keyof AdeaTheme['ansi']>>> =
96
90
  Object.freeze({
97
91
  variable: 'black',
92
+ parameter: 'black',
93
+ property: 'black',
94
+ field: 'black',
95
+ member: 'black',
98
96
  operator: 'black',
99
97
  })
100
98
 
@@ -110,6 +108,16 @@ const SYNTAX_SOURCE: Readonly<Record<SyntaxRole, keyof AdeaTheme['ansi']>> = Obj
110
108
  comment: 'brightBlack',
111
109
  function: 'blue',
112
110
  variable: 'white',
111
+ // A palette with no editor theme of its own says nothing about how its author
112
+ // would set these apart, and most editor themes do not: Dracula, Nord, One Dark
113
+ // and Catppuccin draw keys, fields, member reads and declaration keywords in
114
+ // the same ink as variables and keywords. Inventing a distinction would be the
115
+ // one colour in the palette nobody chose.
116
+ storage: 'magenta',
117
+ parameter: 'white',
118
+ property: 'white',
119
+ field: 'white',
120
+ member: 'white',
113
121
  type: 'cyan',
114
122
  tag: 'red',
115
123
  attribute: 'yellow',
@@ -159,7 +167,11 @@ export function syntaxRoles(
159
167
  keyof AdeaTheme['ansi'],
160
168
  ][]) {
161
169
  const slot = theme.appearance === 'light' ? (LIGHT_SYNTAX_SOURCE[role] ?? source) : source
162
- const value: Oklch | undefined = parseColor(theme.ansi[slot])
170
+ // The theme's own editor colour wins over the ANSI reading: it is what the
171
+ // palette's author chose for this token, in the editor this palette ships for.
172
+ const authored = theme.syntax?.[role]
173
+ const value: Oklch | undefined =
174
+ (authored ? parseColor(authored) : undefined) ?? parseColor(theme.ansi[slot])
163
175
  if (!value) continue
164
176
 
165
177
  if (role === 'comment' || role === 'punctuation') {
@@ -51,6 +51,30 @@ const theme: AdeaThemeRecord = {
51
51
  },
52
52
  "cursor": "oklch(0.7153 0.1518 253.31)",
53
53
  "selection": "oklch(0.8569 0.0141 247.99)",
54
+ "syntax": {
55
+ "keyword": "#ec8e2c",
56
+ "storage": "#ec8e2c",
57
+ "string": "#a5d6ff",
58
+ "number": "#79c0ff",
59
+ "comment": "#8b949e",
60
+ "function": "#d2a8ff",
61
+ "variable": "#c9d1d9",
62
+ "parameter": "#fdac54",
63
+ "property": "#c9d1d9",
64
+ "field": "#c9d1d9",
65
+ "member": "#c9d1d9",
66
+ "type": "#fdac54",
67
+ "tag": "#a5d6ff",
68
+ "attribute": "#79c0ff",
69
+ "operator": "#ec8e2c",
70
+ "heading": "#79c0ff",
71
+ "link": "#c9d1d9",
72
+ "constant": "#79c0ff",
73
+ "punctuation": "#c9d1d9",
74
+ "diffAdd": "#a5d6ff",
75
+ "diffDelete": "#fdac54",
76
+ "diffHunk": "#d2a8ff"
77
+ },
54
78
  "family": "adea",
55
79
  "familyLabel": "Adea",
56
80
  "label": "Dark Colorblind",
@@ -67,6 +91,15 @@ const theme: AdeaThemeRecord = {
67
91
  "dark",
68
92
  "default",
69
93
  "colorblind"
70
- ]
94
+ ],
95
+ "syntaxProvenance": {
96
+ "project": "GitHub VS Code Theme",
97
+ "url": "https://github.com/primer/github-vscode-theme",
98
+ "license": "MIT",
99
+ "revision": "GitHub/github-vscode-theme@6.3.5 sha256:a85236a58ad490b1600ec5a0fd0cea7eec56fb9d33660a1ac82c6da639ad3af4",
100
+ "bootstrappedFrom": [
101
+ "GitHub/github-vscode-theme@6.3.5/themes/dark-colorblind.json"
102
+ ]
103
+ }
71
104
  }
72
105
  export default theme
@@ -51,6 +51,30 @@ const theme: AdeaThemeRecord = {
51
51
  },
52
52
  "cursor": "oklch(0.7625 0.1261 250.42)",
53
53
  "selection": "oklch(0.9627 0.0051 247.88)",
54
+ "syntax": {
55
+ "keyword": "#ff9492",
56
+ "storage": "#ff9492",
57
+ "string": "#addcff",
58
+ "number": "#91cbff",
59
+ "comment": "#bdc4cc",
60
+ "function": "#dbb7ff",
61
+ "variable": "#f0f3f6",
62
+ "parameter": "#ffb757",
63
+ "property": "#f0f3f6",
64
+ "field": "#f0f3f6",
65
+ "member": "#f0f3f6",
66
+ "type": "#ffb757",
67
+ "tag": "#72f088",
68
+ "attribute": "#91cbff",
69
+ "operator": "#ff9492",
70
+ "heading": "#91cbff",
71
+ "link": "#f0f3f6",
72
+ "constant": "#91cbff",
73
+ "punctuation": "#f0f3f6",
74
+ "diffAdd": "#72f088",
75
+ "diffDelete": "#ffb1af",
76
+ "diffHunk": "#dbb7ff"
77
+ },
54
78
  "family": "adea",
55
79
  "familyLabel": "Adea",
56
80
  "label": "Dark High Contrast",
@@ -67,6 +91,15 @@ const theme: AdeaThemeRecord = {
67
91
  "dark",
68
92
  "default",
69
93
  "high-contrast"
70
- ]
94
+ ],
95
+ "syntaxProvenance": {
96
+ "project": "GitHub VS Code Theme",
97
+ "url": "https://github.com/primer/github-vscode-theme",
98
+ "license": "MIT",
99
+ "revision": "GitHub/github-vscode-theme@6.3.5 sha256:a85236a58ad490b1600ec5a0fd0cea7eec56fb9d33660a1ac82c6da639ad3af4",
100
+ "bootstrappedFrom": [
101
+ "GitHub/github-vscode-theme@6.3.5/themes/dark-high-contrast.json"
102
+ ]
103
+ }
71
104
  }
72
105
  export default theme
@@ -51,6 +51,30 @@ const theme: AdeaThemeRecord = {
51
51
  },
52
52
  "cursor": "oklch(0.6182 0.1935 258.33)",
53
53
  "selection": "oklch(0.9425 0.0111 243.66)",
54
+ "syntax": {
55
+ "keyword": "#ff7b72",
56
+ "storage": "#ff7b72",
57
+ "string": "#a5d6ff",
58
+ "number": "#79c0ff",
59
+ "comment": "#8b949e",
60
+ "function": "#d2a8ff",
61
+ "variable": "#e6edf3",
62
+ "parameter": "#ffa657",
63
+ "property": "#e6edf3",
64
+ "field": "#e6edf3",
65
+ "member": "#e6edf3",
66
+ "type": "#ffa657",
67
+ "tag": "#7ee787",
68
+ "attribute": "#79c0ff",
69
+ "operator": "#ff7b72",
70
+ "heading": "#79c0ff",
71
+ "link": "#e6edf3",
72
+ "constant": "#79c0ff",
73
+ "punctuation": "#e6edf3",
74
+ "diffAdd": "#7ee787",
75
+ "diffDelete": "#ffa198",
76
+ "diffHunk": "#d2a8ff"
77
+ },
54
78
  "family": "adea",
55
79
  "familyLabel": "Adea",
56
80
  "label": "Dark",
@@ -67,6 +91,15 @@ const theme: AdeaThemeRecord = {
67
91
  "dark",
68
92
  "default",
69
93
  "neutral"
70
- ]
94
+ ],
95
+ "syntaxProvenance": {
96
+ "project": "GitHub VS Code Theme",
97
+ "url": "https://github.com/primer/github-vscode-theme",
98
+ "license": "MIT",
99
+ "revision": "GitHub/github-vscode-theme@6.3.5 sha256:a85236a58ad490b1600ec5a0fd0cea7eec56fb9d33660a1ac82c6da639ad3af4",
100
+ "bootstrappedFrom": [
101
+ "GitHub/github-vscode-theme@6.3.5/themes/dark-default.json"
102
+ ]
103
+ }
71
104
  }
72
105
  export default theme
@@ -51,6 +51,30 @@ const theme: AdeaThemeRecord = {
51
51
  },
52
52
  "cursor": "oklch(0.5399 0.1906 257.48)",
53
53
  "selection": "oklch(0.2785 0.0132 253.04)",
54
+ "syntax": {
55
+ "keyword": "#b35900",
56
+ "storage": "#b35900",
57
+ "string": "#0a3069",
58
+ "number": "#0550ae",
59
+ "comment": "#6e7781",
60
+ "function": "#8250df",
61
+ "variable": "#24292f",
62
+ "parameter": "#8a4600",
63
+ "property": "#24292f",
64
+ "field": "#24292f",
65
+ "member": "#24292f",
66
+ "type": "#8a4600",
67
+ "tag": "#0550ae",
68
+ "attribute": "#0550ae",
69
+ "operator": "#b35900",
70
+ "heading": "#0550ae",
71
+ "link": "#24292f",
72
+ "constant": "#0550ae",
73
+ "punctuation": "#24292f",
74
+ "diffAdd": "#0550ae",
75
+ "diffDelete": "#6f3800",
76
+ "diffHunk": "#8250df"
77
+ },
54
78
  "family": "adea",
55
79
  "familyLabel": "Adea",
56
80
  "label": "Light Colorblind",
@@ -68,6 +92,15 @@ const theme: AdeaThemeRecord = {
68
92
  "light",
69
93
  "default",
70
94
  "colorblind"
71
- ]
95
+ ],
96
+ "syntaxProvenance": {
97
+ "project": "GitHub VS Code Theme",
98
+ "url": "https://github.com/primer/github-vscode-theme",
99
+ "license": "MIT",
100
+ "revision": "GitHub/github-vscode-theme@6.3.5 sha256:a85236a58ad490b1600ec5a0fd0cea7eec56fb9d33660a1ac82c6da639ad3af4",
101
+ "bootstrappedFrom": [
102
+ "GitHub/github-vscode-theme@6.3.5/themes/light-colorblind.json"
103
+ ]
104
+ }
72
105
  }
73
106
  export default theme
@@ -51,6 +51,30 @@ const theme: AdeaThemeRecord = {
51
51
  },
52
52
  "cursor": "oklch(0.441 0.1809 260.43)",
53
53
  "selection": "oklch(0.1767 0.0114 260.64)",
54
+ "syntax": {
55
+ "keyword": "#a0111f",
56
+ "storage": "#a0111f",
57
+ "string": "#032563",
58
+ "number": "#023b95",
59
+ "comment": "#66707b",
60
+ "function": "#622cbc",
61
+ "variable": "#0e1116",
62
+ "parameter": "#702c00",
63
+ "property": "#0e1116",
64
+ "field": "#0e1116",
65
+ "member": "#0e1116",
66
+ "type": "#702c00",
67
+ "tag": "#024c1a",
68
+ "attribute": "#023b95",
69
+ "operator": "#a0111f",
70
+ "heading": "#023b95",
71
+ "link": "#0e1116",
72
+ "constant": "#023b95",
73
+ "punctuation": "#0e1116",
74
+ "diffAdd": "#024c1a",
75
+ "diffDelete": "#6e011a",
76
+ "diffHunk": "#622cbc"
77
+ },
54
78
  "family": "adea",
55
79
  "familyLabel": "Adea",
56
80
  "label": "Light High Contrast",
@@ -68,6 +92,15 @@ const theme: AdeaThemeRecord = {
68
92
  "light",
69
93
  "default",
70
94
  "high-contrast"
71
- ]
95
+ ],
96
+ "syntaxProvenance": {
97
+ "project": "GitHub VS Code Theme",
98
+ "url": "https://github.com/primer/github-vscode-theme",
99
+ "license": "MIT",
100
+ "revision": "GitHub/github-vscode-theme@6.3.5 sha256:a85236a58ad490b1600ec5a0fd0cea7eec56fb9d33660a1ac82c6da639ad3af4",
101
+ "bootstrappedFrom": [
102
+ "GitHub/github-vscode-theme@6.3.5/themes/light-high-contrast.json"
103
+ ]
104
+ }
72
105
  }
73
106
  export default theme