Line data Source code
1 : <script setup lang="ts">
2 : import Tooltip from '@comfyorg/design-system/tooltip/Tooltip'
3 : import TooltipContent from '@comfyorg/design-system/tooltip/TooltipContent'
4 : import TooltipTrigger from '@comfyorg/design-system/tooltip/TooltipTrigger'
5 :
6 : import { computed } from 'vue'
7 : import { useI18n } from 'vue-i18n'
8 :
9 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
10 : import { LGraphCanvas, LiteGraph } from '@/lib/litegraph/src/litegraph'
11 : import type { ColorOption } from '@/lib/litegraph/src/litegraph'
12 : import { useThemeStore } from '@/platform/theme/themeStore'
13 : import { adjustColor } from '@comfyorg/shared-frontend-utils/colorUtil'
14 : import { cn } from '@comfyorg/tailwind-utils'
15 :
16 : import LayoutField from '@comfyorg/design-system/field/LayoutField'
17 :
18 : /**
19 : * Good design limits dependencies and simplifies the interface of the abstraction layer.
20 : * Here, we only care about the getColorOption and setColorOption methods,
21 : * and do not concern ourselves with other methods.
22 : */
23 : type PickedNode = Pick<LGraphNode, 'getColorOption' | 'setColorOption'>
24 :
25 : const { nodes } = defineProps<{ nodes: PickedNode[] }>()
26 2355 : const emit = defineEmits<{ (e: 'changed'): void }>()
27 :
28 11 : const { t } = useI18n()
29 :
30 11 : const themeStore = useThemeStore()
31 :
32 : type NodeColorOption = {
33 : name: string
34 : localizedName: () => string
35 : value: {
36 : dark: string
37 : light: string
38 : ringDark: string
39 : ringLight: string
40 : }
41 : }
42 :
43 11 : const nodeColorEntries = Object.entries(LGraphCanvas.node_colors)
44 :
45 110 : function getColorValue(color: string): NodeColorOption['value'] {
46 110 : return {
47 : dark: adjustColor(color, { lightness: 0.3 }),
48 : light: adjustColor(color, { lightness: 0.4 }),
49 : ringDark: adjustColor(color, { lightness: 0.5 }),
50 : ringLight: adjustColor(color, { lightness: 0.1 })
51 : }
52 : }
53 :
54 11 : const NO_COLOR_OPTION: NodeColorOption = {
55 : name: 'noColor',
56 : localizedName: () => t('color.noColor'),
57 : value: getColorValue(LiteGraph.NODE_DEFAULT_BGCOLOR)
58 : }
59 :
60 11 : const colorOptions: NodeColorOption[] = [
61 : NO_COLOR_OPTION,
62 : ...nodeColorEntries.map(([name, color]) => ({
63 : name,
64 : localizedName: () => t(`color.${name}`),
65 : value: getColorValue(color.bgcolor)
66 : }))
67 : ]
68 :
69 11 : const isLightTheme = computed(() => themeStore.colorScheme === 'light')
70 :
71 11 : const nodeColor = computed<NodeColorOption['name'] | null>({
72 : get() {
73 21 : if (nodes.length === 0) return null
74 21 : const theColorOptions = nodes.map((item) => item.getColorOption())
75 :
76 21 : let colorOption: ColorOption | null | false = theColorOptions[0]
77 21 : if (!theColorOptions.every((option) => option === colorOption)) {
78 0 : colorOption = false
79 : }
80 :
81 21 : if (colorOption === false) return null
82 21 : if (colorOption == null || (!colorOption.bgcolor && !colorOption.color))
83 19 : return NO_COLOR_OPTION.name
84 21 : return (
85 : nodeColorEntries.find(
86 : ([_, color]) =>
87 : color.bgcolor === colorOption.bgcolor &&
88 : color.color === colorOption.color
89 : )?.[0] ?? null
90 : )
91 : },
92 : set(colorName) {
93 3 : if (colorName === null) return
94 :
95 3 : const canvasColorOption =
96 : colorName === NO_COLOR_OPTION.name
97 : ? null
98 : : LGraphCanvas.node_colors[colorName]
99 :
100 3 : for (const item of nodes) {
101 3 : item.setColorOption(canvasColorOption)
102 : }
103 :
104 3 : emit('changed')
105 : }
106 : })
107 11 : </script>
108 :
109 : <template>
110 21 : <LayoutField :label="t('rightSidePanel.color')">
111 : <div
112 : class="grid grid-cols-5 justify-items-center gap-1 rounded-lg bg-secondary p-1"
113 : >
114 110 : <Tooltip v-for="option of colorOptions" :key="option.name">
115 : <TooltipTrigger as-child>
116 : <button
117 : :aria-label="option.localizedName()"
118 : :class="
119 : cn(
120 : 'flex size-8 cursor-pointer items-center justify-center rounded-lg border-0 bg-transparent text-left ring-0 outline-0',
121 : option.name === nodeColor ? 'bg-muted' : 'hover:bg-muted'
122 : )
123 : "
124 : @click="nodeColor = option.name"
125 : >
126 : <div
127 : :class="cn('size-4 rounded-full ring-2 ring-gray-500/10')"
128 : :style="{
129 : backgroundColor: isLightTheme
130 : ? option.value.light
131 : : option.value.dark,
132 : '--tw-ring-color':
133 : option.name === nodeColor
134 : ? isLightTheme
135 : ? option.value.ringLight
136 : : option.value.ringDark
137 : : undefined
138 : }"
139 : :data-testid="option.name"
140 : />
141 : </button>
142 : </TooltipTrigger>
143 : <TooltipContent>{{ option.localizedName() }}</TooltipContent>
144 : </Tooltip>
145 : </div>
146 : </LayoutField>
147 : </template>
|