Line data Source code
1 : import {
2 : DEFAULT_DARK_THEME_PRESET,
3 : DEFAULT_LIGHT_THEME_PRESET
4 : } from './builtInThemePresets'
5 : import type {
6 : Colors,
7 : CompletedThemePreset,
8 : ThemePreset,
9 : ThemeTokens
10 : } from './themePresetSchema'
11 :
12 47703 : export function usesThemeAliases(id: string) {
13 47703 : return id === 'dark' || id === 'light'
14 : }
15 :
16 20 : function deriveLegacyTheme({
17 : comfy_base,
18 : litegraph_base
19 : }: Colors): ThemeTokens {
20 20 : const surface =
21 : comfy_base['interface-panel-surface'] ?? comfy_base['comfy-menu-bg']
22 20 : const contrast = comfy_base['contrast-mix-color'] ?? '#fff'
23 20 : const mixWithContrast = (surfacePercent: number) =>
24 : `color-mix(in srgb, ${surface} ${surfacePercent}%, ${contrast})`
25 20 : return {
26 : background: surface,
27 : foreground: comfy_base['fg-color'],
28 : 'muted-foreground': comfy_base['descrip-text'],
29 : border: comfy_base['interface-stroke'] ?? mixWithContrast(75.5),
30 : accent:
31 : comfy_base['interface-panel-hover-surface'] ??
32 : comfy_base['interface-button-hover-surface'] ??
33 : mixWithContrast(92.5),
34 : muted:
35 : comfy_base['interface-panel-selected-surface'] ?? mixWithContrast(87.5),
36 : card: comfy_base['comfy-menu-secondary-bg'],
37 : secondary: litegraph_base.NODE_DEFAULT_COLOR,
38 : input: litegraph_base.WIDGET_BGCOLOR,
39 : node: litegraph_base.NODE_DEFAULT_BGCOLOR,
40 : 'node-foreground': litegraph_base.NODE_TEXT_COLOR,
41 : 'node-header': litegraph_base.NODE_DEFAULT_COLOR,
42 : 'node-border': litegraph_base.WIDGET_OUTLINE_COLOR
43 : }
44 : }
45 :
46 2356 : const DERIVED_THEME_DEFAULTS: ThemeTokens = {
47 : popover: 'var(--background)',
48 : input: 'var(--accent)',
49 : ring: 'var(--muted-foreground)',
50 : node: 'var(--card)',
51 : 'node-foreground': 'var(--muted-foreground)',
52 : 'node-header': 'var(--secondary)',
53 : 'node-border': 'var(--border)'
54 : }
55 :
56 7122 : export function resolveTheme(preset: ThemePreset) {
57 7122 : const defaults = preset.light_theme
58 : ? DEFAULT_LIGHT_THEME_PRESET
59 : : DEFAULT_DARK_THEME_PRESET
60 7122 : const authoredLegacy = preset.colors.comfy_base
61 7122 : const secondaryBackground =
62 : authoredLegacy['comfy-menu-bg'] &&
63 : !authoredLegacy['comfy-menu-secondary-bg']
64 : ? { 'comfy-menu-secondary-bg': authoredLegacy['comfy-menu-bg'] }
65 : : {}
66 7122 : const completedPreset: CompletedThemePreset = {
67 : ...preset,
68 : colors: {
69 : ...preset.colors,
70 : node_slot: { ...defaults.colors.node_slot, ...preset.colors.node_slot },
71 : litegraph_base: {
72 : ...defaults.colors.litegraph_base,
73 : ...preset.colors.litegraph_base
74 : },
75 : comfy_base: {
76 : ...defaults.colors.comfy_base,
77 : ...authoredLegacy,
78 : ...secondaryBackground
79 : }
80 : }
81 : }
82 7122 : const legacy = usesThemeAliases(preset.id)
83 : ? {}
84 : : deriveLegacyTheme(completedPreset.colors)
85 7122 : const canvas = completedPreset.colors.litegraph_base
86 7122 : return {
87 : id: preset.id,
88 : name: preset.name,
89 : colorScheme: preset.light_theme ? ('light' as const) : ('dark' as const),
90 : preset: completedPreset,
91 : cssTokens: {
92 : ...DERIVED_THEME_DEFAULTS,
93 : ...defaults.colors.theme,
94 : ...legacy,
95 : ...preset.colors.theme
96 : },
97 : legacyCss: completedPreset.colors.comfy_base,
98 : controls: {
99 : inputTextColor: completedPreset.colors.comfy_base['input-text']
100 : },
101 : dataTypeColors: completedPreset.colors.node_slot,
102 : canvas: {
103 : backgroundImage: canvas.BACKGROUND_IMAGE,
104 : clearBackgroundColor: canvas.CLEAR_BACKGROUND_COLOR,
105 : nodeTitleColor: canvas.NODE_TITLE_COLOR,
106 : nodeSelectedTitleColor: canvas.NODE_SELECTED_TITLE_COLOR,
107 : nodeTextColor: canvas.NODE_TEXT_COLOR,
108 : nodeDefaultColor: canvas.NODE_DEFAULT_COLOR,
109 : widgetBackgroundColor: canvas.WIDGET_BGCOLOR,
110 : widgetTextColor: canvas.WIDGET_TEXT_COLOR,
111 : widgetSecondaryTextColor: canvas.WIDGET_SECONDARY_TEXT_COLOR,
112 : linkColor: canvas.LINK_COLOR,
113 : badgeForegroundColor: canvas.BADGE_FG_COLOR,
114 : badgeBackgroundColor: canvas.BADGE_BG_COLOR
115 : }
116 : }
117 : }
118 :
119 : export type Theme = ReturnType<typeof resolveTheme>
|