Skip to content

Commit

Permalink
Apply node opacity setting to all node colors (#947)
Browse files Browse the repository at this point in the history
* Apply opacity to node colors. Resolves #928

* Handle default and custom colors all in draw handler

* Add colorUtil unit tests

* Add Playwright test

* Remove comment

* Revert colorPalette.ts changes

* Remove unused imports

* Fix typo

* Update test expectations [skip ci]

---------

Co-authored-by: github-actions <[email protected]>
  • Loading branch information
christian-byrne and github-actions committed Sep 24, 2024
1 parent 5d8e8a2 commit b21c0f5
Show file tree
Hide file tree
Showing 19 changed files with 841 additions and 146 deletions.
504 changes: 504 additions & 0 deletions browser_tests/assets/every_node_color.json

Large diffs are not rendered by default.

82 changes: 81 additions & 1 deletion browser_tests/colorPalette.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,21 @@ test.describe('Color Palette', () => {
await comfyPage.nextFrame()
await expect(comfyPage.canvas).toHaveScreenshot('default-color-palette.png')
})
})

test.describe('Node Color Adjustments', () => {
test.beforeEach(async ({ comfyPage }) => {
await comfyPage.loadWorkflow('every_node_color')
})

test('Can change node opacity setting', async ({ comfyPage }) => {
test.afterEach(async ({ comfyPage }) => {
await comfyPage.setSetting('Comfy.Node.Opacity', 1.0)
await comfyPage.setSetting('Comfy.ColorPalette', 'dark')
})

test('should adjust opacity via node opacity setting', async ({
comfyPage
}) => {
await comfyPage.setSetting('Comfy.Node.Opacity', 0.5)
await comfyPage.page.waitForTimeout(128)

Expand All @@ -166,4 +179,71 @@ test.describe('Color Palette', () => {
await comfyPage.page.mouse.move(8, 8)
await expect(comfyPage.canvas).toHaveScreenshot('node-opacity-1.png')
})

test('should persist color adjustments when changing themes', async ({
comfyPage
}) => {
await comfyPage.setSetting('Comfy.Node.Opacity', 0.2)
await comfyPage.setSetting('Comfy.ColorPalette', 'arc')
await comfyPage.nextFrame()
await comfyPage.page.mouse.move(0, 0)
await expect(comfyPage.canvas).toHaveScreenshot(
'node-opacity-0.2-arc-theme.png'
)
})

test('should not serialize color adjustments in workflow', async ({
comfyPage
}) => {
await comfyPage.setSetting('Comfy.Node.Opacity', 0.5)
await comfyPage.setSetting('Comfy.ColorPalette', 'light')
const saveWorkflowInterval = 1000
await comfyPage.page.waitForTimeout(saveWorkflowInterval)
const workflow = await comfyPage.page.evaluate(() => {
return localStorage.getItem('workflow')
})
for (const node of JSON.parse(workflow).nodes) {
if (node.bgcolor) expect(node.bgcolor).not.toMatch(/hsla/)
if (node.color) expect(node.color).not.toMatch(/hsla/)
}
})

test('should lighten node colors when switching to light theme', async ({
comfyPage
}) => {
await comfyPage.setSetting('Comfy.ColorPalette', 'light')
await comfyPage.nextFrame()
await expect(comfyPage.canvas).toHaveScreenshot('node-lightened-colors.png')
})

test.describe('Context menu color adjustments', () => {
test.beforeEach(async ({ comfyPage }) => {
await comfyPage.setSetting('Comfy.ColorPalette', 'light')
await comfyPage.setSetting('Comfy.Node.Opacity', 0.3)
const node = await comfyPage.getFirstNodeRef()
await node.clickContextMenuOption('Colors')
})

test('should persist color adjustments when changing custom node colors', async ({
comfyPage
}) => {
await comfyPage.page
.locator('.litemenu-entry.submenu span:has-text("red")')
.click()
await expect(comfyPage.canvas).toHaveScreenshot(
'node-opacity-0.3-color-changed.png'
)
})

test('should persist color adjustments when removing custom node color', async ({
comfyPage
}) => {
await comfyPage.page
.locator('.litemenu-entry.submenu span:has-text("No color")')
.click()
await expect(comfyPage.canvas).toHaveScreenshot(
'node-opacity-0.3-color-removed.png'
)
})
})
})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 0 additions & 21 deletions src/components/graph/GraphCanvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ import {
import type { RenderedTreeExplorerNode } from '@/types/treeExplorerTypes'
import { useNodeBookmarkStore } from '@/stores/nodeBookmarkStore'
import { useCanvasStore } from '@/stores/graphStore'
import { applyOpacity } from '@/utils/colorUtil'
import { getColorPalette } from '@/extensions/core/colorPalette'
import { debounce } from 'lodash'
const emit = defineEmits(['ready'])
const canvasRef = ref<HTMLCanvasElement | null>(null)
Expand Down Expand Up @@ -93,24 +90,6 @@ watchEffect(() => {
})
})
const updateNodeOpacity = (nodeOpacity: number) => {
const colorPalette = getColorPalette()
if (!canvasStore.canvas) return
const nodeBgColor = colorPalette?.colors?.litegraph_base?.NODE_DEFAULT_BGCOLOR
if (nodeBgColor) {
LiteGraph.NODE_DEFAULT_BGCOLOR = applyOpacity(nodeBgColor, nodeOpacity)
}
}
const debouncedUpdateNodeOpacity = debounce(updateNodeOpacity, 128)
watchEffect(() => {
const nodeOpacity = settingStore.get('Comfy.Node.Opacity')
debouncedUpdateNodeOpacity(nodeOpacity)
})
let dropTargetCleanup = () => {}
onMounted(async () => {
Expand Down
10 changes: 1 addition & 9 deletions src/extensions/core/colorPalette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { app } from '../../scripts/app'
import { $el } from '../../scripts/ui'
import type { ColorPalettes, Palette } from '@/types/colorPalette'
import { LGraphCanvas, LiteGraph } from '@comfyorg/litegraph'
import { applyOpacity } from '@/utils/colorUtil'
import { useSettingStore } from '@/stores/settingStore'

// Manage color palettes

Expand Down Expand Up @@ -684,13 +682,7 @@ app.registerExtension({
colorPalette.colors.litegraph_base.hasOwnProperty(key) &&
LiteGraph.hasOwnProperty(key)
) {
LiteGraph[key] =
key === 'NODE_DEFAULT_BGCOLOR'
? applyOpacity(
colorPalette.colors.litegraph_base[key],
useSettingStore().get('Comfy.Node.Opacity')
)
: colorPalette.colors.litegraph_base[key]
LiteGraph[key] = colorPalette.colors.litegraph_base[key]
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/scripts/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
validateComfyWorkflow
} from '../types/comfyWorkflow'
import { ComfyNodeDef, StatusWsMessageStatus } from '@/types/apiTypes'
import { lightenColor } from '@/utils/colorUtil'
import { adjustColor, ColorAdjustOptions } from '@/utils/colorUtil'
import { ComfyAppMenu } from './ui/menu/index'
import { getStorageValue } from './utils'
import { ComfyWorkflowManager, ComfyWorkflow } from './workflows'
Expand Down Expand Up @@ -1568,14 +1568,25 @@ export class ComfyApp {
this.editor_alpha = 0.2
}

const adjustColor = (color?: string) => {
return color ? lightenColor(color, 0.5) : color
}
if (app.ui.settings.getSettingValue('Comfy.ColorPalette') === 'light') {
node.bgcolor = adjustColor(node.bgcolor)
node.color = adjustColor(node.color)
const adjustments: ColorAdjustOptions = {}

const opacity = useSettingStore().get('Comfy.Node.Opacity')
if (opacity) adjustments.opacity = opacity

if (useSettingStore().get('Comfy.ColorPalette') === 'light') {
adjustments.lightness = 0.5

// Lighten title bar of colored nodes on light theme
if (old_color) {
node.color = adjustColor(old_color, { lightness: 0.5 })
}
}

node.bgcolor = adjustColor(
old_bgcolor || LiteGraph.NODE_DEFAULT_BGCOLOR,
adjustments
)

const res = origDrawNode.apply(this, arguments)

this.editor_alpha = editor_alpha
Expand Down
Loading

0 comments on commit b21c0f5

Please sign in to comment.