11import {
22 createVividThemeColors ,
3+ getStandardThemeColors ,
34 getThemeModes ,
45 parseThemeFile ,
56 themeColorToHex ,
@@ -14,10 +15,10 @@ import {
1415 *
1516 * VS Code themes describe editor chrome, not an app palette: they carry a few
1617 * hundred workbench keys, leave most of them unset, and freely use 8-digit
17- * hex with alpha for overlays. So the conversion derives a complete, contrast
18- * -solved palette from the theme's editor background and accent , then layers
19- * the workbench colors it did specify on top. Anything the file omits keeps
20- * the derived value instead of falling back to an unrelated palette .
18+ * hex with alpha for overlays. The conversion derives a complete palette from
19+ * a visible accent and the editor background, then layers usable workbench
20+ * colors on top. Foregrounds must remain readable, while authored focus and
21+ * control candidates must remain distinct from adjacent surfaces and states .
2122 */
2223
2324type VsCodeRgba = { r : number ; g : number ; b : number ; a : number } ;
@@ -210,26 +211,61 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
210211 const canvas = { r : canvasColor . r , g : canvasColor . g , b : canvasColor . b } ;
211212 const appearance = resolveAppearance ( value , canvas ) ;
212213
213- const accentColor = pick (
214+ const canvasHex = toHex ( canvas ) ;
215+ const raisedSurfaceCandidateHex = solidOver (
216+ canvas ,
217+ "editorWidget.background" ,
218+ "dropdown.background" ,
219+ ) ;
220+
221+ /** Accent and control candidates must clear this small separation floor
222+ * from every adjacent surface or state checked by the importer. */
223+ const standsApart = ( first : string , second : string ) =>
224+ contrastRatio ( hexToRgb ( first ) , hexToRgb ( second ) ) >= 1.1 ;
225+
226+ let accentColor : VsCodeRgba | null = null ;
227+ let accentHex : string | null = null ;
228+ for ( const key of [
214229 "focusBorder" ,
215230 "button.background" ,
216231 "textLink.foreground" ,
217232 "activityBarBadge.background" ,
218233 "progressBar.background" ,
219234 "badge.background" ,
220- ) ;
221- const canvasHex = toHex ( canvas ) ;
222- const accentHex = accentColor ? flattenOver ( accentColor , canvas ) : null ;
235+ ] ) {
236+ const candidate = parseVsCodeColor ( colors [ key ] ) ;
237+ if ( ! candidate ) continue ;
238+ const candidateHex = flattenOver ( candidate , canvas ) ;
239+ if (
240+ ! standsApart ( candidateHex , canvasHex ) ||
241+ ( raisedSurfaceCandidateHex !== null && ! standsApart ( candidateHex , raisedSurfaceCandidateHex ) )
242+ )
243+ continue ;
244+ accentColor = candidate ;
245+ accentHex = candidateHex ;
246+ break ;
247+ }
248+ if ( ! accentColor || ! accentHex ) {
249+ const standardAccentHex = themeColorToHex ( getStandardThemeColors ( appearance ) . accent ) ! ;
250+ accentHex =
251+ [ standardAccentHex , "#ffffff" , "#000000" ] . find (
252+ ( candidate ) =>
253+ standsApart ( candidate , canvasHex ) &&
254+ ( raisedSurfaceCandidateHex === null || standsApart ( candidate , raisedSurfaceCandidateHex ) ) ,
255+ ) ?? standardAccentHex ;
256+ accentColor = parseVsCodeColor ( accentHex ) ! ;
257+ }
223258
224259 // The derived palette is the floor: every role starts contrast-solved, then
225260 // the theme's own workbench colors replace what it actually specified. The
226261 // floor derives from a muted accent -- the vivid engine carries the accent
227262 // hue into every surface, which washes an imported neutral palette (a gray
228263 // theme with a blue focusBorder would get blue code and text surfaces).
229- const mutedAccentHex = accentColor
230- ? flattenOver ( { r : accentColor . r , g : accentColor . g , b : accentColor . b , a : 0.2 } , canvas )
231- : null ;
232- const derived = createVividThemeColors ( appearance , canvasHex , mutedAccentHex ?? canvasHex ) ;
264+ const mutedAccentHex = flattenOver (
265+ { r : accentColor . r , g : accentColor . g , b : accentColor . b , a : 0.2 } ,
266+ canvas ,
267+ ) ;
268+ const derived = createVividThemeColors ( appearance , canvasHex , mutedAccentHex ) ;
233269 const sidebarHex =
234270 solidOver ( canvas , "sideBar.background" , "activityBar.background" ) ?? derived . sidebar ;
235271 const sidebar = hexToRgb ( sidebarHex ) ;
@@ -256,6 +292,34 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
256292 return relativeLuminance ( surfaceRgb ) < 0.179 ? "#ffffff" : "#000000" ;
257293 } ;
258294
295+ const surfaceRaisedHex = raisedSurfaceCandidateHex ?? derived . surfaceRaised ;
296+ // The checked switch track maps to messageAction, so resolve it before
297+ // choosing the input role used by the unchecked track.
298+ const buttonHex = solidOver ( canvas , "button.background" ) ;
299+ const actionHex =
300+ buttonHex && standsApart ( buttonHex , canvasHex ) && standsApart ( buttonHex , surfaceRaisedHex )
301+ ? buttonHex
302+ : accentHex ;
303+ const inputCandidates = [
304+ derived . input ,
305+ derived . surfaceRaised ,
306+ getStandardThemeColors ( appearance ) . input ,
307+ "#000000" ,
308+ "#ffffff" ,
309+ "#808080" ,
310+ ] ;
311+ let inputHex =
312+ inputCandidates . find (
313+ ( candidate ) => standsApart ( candidate , canvasHex ) && standsApart ( candidate , actionHex ) ,
314+ ) ?? "#808080" ;
315+ for ( const key of [ "input.background" , "input.border" ] ) {
316+ const candidate = solidOver ( canvas , key ) ;
317+ if ( candidate && standsApart ( candidate , canvasHex ) && standsApart ( candidate , actionHex ) ) {
318+ inputHex = candidate ;
319+ break ;
320+ }
321+ }
322+
259323 const overrides : Partial < Record < ThemeColorRole , string > > = {
260324 canvas : canvasHex ,
261325 text : readableOn ( canvasHex , derived . text , "editor.foreground" , "foreground" ) ,
@@ -266,15 +330,14 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
266330 "disabledForeground" ,
267331 ) ,
268332 surface : solidOver ( canvas , "editorWidget.background" ) ?? derived . surface ,
269- surfaceRaised :
270- solidOver ( canvas , "editorWidget.background" , "dropdown.background" ) ?? derived . surfaceRaised ,
333+ surfaceRaised : surfaceRaisedHex ,
271334 surfaceOverlay :
272335 solidOver ( canvas , "menu.background" , "quickInput.background" , "dropdown.background" ) ??
273336 derived . surfaceOverlay ,
274337 border :
275338 solidOver ( canvas , "panel.border" , "editorGroup.border" , "contrastBorder" ) ?? derived . border ,
276- input : solidOver ( canvas , "input.border" , "dropdown.border" ) ?? derived . input ,
277- placeholder : readableOn ( canvasHex , derived . placeholder , "input.placeholderForeground" ) ,
339+ input : inputHex ,
340+ placeholder : readableOn ( surfaceRaisedHex , derived . placeholder , "input.placeholderForeground" ) ,
278341 error : readableOn ( canvasHex , derived . error , "editorError.foreground" , "errorForeground" ) ,
279342 warning : readableOn ( canvasHex , derived . warning , "editorWarning.foreground" ) ,
280343 accentSurface :
@@ -301,23 +364,15 @@ export function parseVsCodeThemeFile(value: unknown): ThemeDefinition {
301364 terminalScrollbar :
302365 solidOver ( terminal , "scrollbarSlider.background" ) ?? derived . terminalScrollbar ,
303366 } ;
304- if ( accentHex ) {
305- overrides . accent = accentHex ;
306- overrides . focus = accentHex ;
307- // The button pair is the closest thing VS Code has to our action color.
308- const actionHex = solidOver ( canvas , "button.background" ) ?? accentHex ;
309- overrides . messageAction = actionHex ;
310- overrides . messageActionForeground = readableOn (
311- actionHex ,
312- derived . messageActionForeground ,
313- "button.foreground" ,
314- ) ;
315- overrides . accentForeground = readableOn (
316- accentHex ,
317- derived . accentForeground ,
318- "button.foreground" ,
319- ) ;
320- }
367+ overrides . accent = accentHex ;
368+ overrides . focus = accentHex ;
369+ overrides . messageAction = actionHex ;
370+ overrides . messageActionForeground = readableOn (
371+ actionHex ,
372+ derived . messageActionForeground ,
373+ "button.foreground" ,
374+ ) ;
375+ overrides . accentForeground = readableOn ( accentHex , derived . accentForeground , "button.foreground" ) ;
321376
322377 // Reuse the theme-file parser so ids, names, and color values go through the
323378 // same validation as a hand-written file.
0 commit comments