mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-29 18:52:19 +00:00
Shift node color's brightness for light mode (#123)
* Shift node color's brightness for light mode * nit * Fix test
This commit is contained in:
@@ -15,7 +15,7 @@ const jestConfig: JestConfigWithTsJest = {
|
|||||||
resetModules: true,
|
resetModules: true,
|
||||||
testTimeout: 10000,
|
testTimeout: 10000,
|
||||||
moduleNameMapper: {
|
moduleNameMapper: {
|
||||||
"^src/(.*)$": "<rootDir>/src/$1",
|
"^@/(.*)$": "<rootDir>/src/$1",
|
||||||
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
|
'\\.(css|less|scss|sass)$': 'identity-obj-proxy',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { lightenColor } from "@/utils/colorUtil";
|
||||||
import { app } from "../../scripts/app";
|
import { app } from "../../scripts/app";
|
||||||
import { $el } from "../../scripts/ui";
|
import { $el } from "../../scripts/ui";
|
||||||
import type { ColorPalettes, Palette } from "@/types/colorPalette";
|
import type { ColorPalettes, Palette } from "@/types/colorPalette";
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import {
|
|||||||
parseComfyWorkflow,
|
parseComfyWorkflow,
|
||||||
} from "../types/comfyWorkflow";
|
} from "../types/comfyWorkflow";
|
||||||
import { ComfyNodeDef } from "@/types/apiTypes";
|
import { ComfyNodeDef } from "@/types/apiTypes";
|
||||||
|
import { lightenColor } from "@/utils/colorUtil";
|
||||||
import { ComfyAppMenu } from "./ui/menu/index";
|
import { ComfyAppMenu } from "./ui/menu/index";
|
||||||
import { getStorageValue } from "./utils";
|
import { getStorageValue } from "./utils";
|
||||||
import { ComfyWorkflowManager, ComfyWorkflow } from "./workflows";
|
import { ComfyWorkflowManager, ComfyWorkflow } from "./workflows";
|
||||||
@@ -1538,7 +1539,8 @@ export class ComfyApp {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
LGraphCanvas.prototype.drawNode = function (node, ctx) {
|
LGraphCanvas.prototype.drawNode = function (node, ctx) {
|
||||||
var editor_alpha = this.editor_alpha;
|
var editor_alpha = this.editor_alpha;
|
||||||
var old_color = node.bgcolor;
|
var old_color = node.color;
|
||||||
|
var old_bgcolor = node.bgcolor;
|
||||||
|
|
||||||
if (node.mode === 2) {
|
if (node.mode === 2) {
|
||||||
// never
|
// never
|
||||||
@@ -1551,10 +1553,19 @@ export class ComfyApp {
|
|||||||
// this.editor_alpha = 0.2;
|
// 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 res = origDrawNode.apply(this, arguments);
|
const res = origDrawNode.apply(this, arguments);
|
||||||
|
|
||||||
this.editor_alpha = editor_alpha;
|
this.editor_alpha = editor_alpha;
|
||||||
node.bgcolor = old_color;
|
node.color = old_color;
|
||||||
|
node.bgcolor = old_bgcolor;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|||||||
100
src/utils/colorUtil.ts
Normal file
100
src/utils/colorUtil.ts
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
type RGB = { r: number; g: number; b: number };
|
||||||
|
type HSL = { h: number; s: number; l: number };
|
||||||
|
|
||||||
|
function rgbToHsl({ r, g, b }: RGB): HSL {
|
||||||
|
(r /= 255), (g /= 255), (b /= 255);
|
||||||
|
const max = Math.max(r, g, b),
|
||||||
|
min = Math.min(r, g, b);
|
||||||
|
let h: number,
|
||||||
|
s: number,
|
||||||
|
l: number = (max + min) / 2;
|
||||||
|
|
||||||
|
if (max === min) {
|
||||||
|
h = s = 0; // achromatic
|
||||||
|
} else {
|
||||||
|
const d = max - min;
|
||||||
|
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
||||||
|
switch (max) {
|
||||||
|
case r:
|
||||||
|
h = (g - b) / d + (g < b ? 6 : 0);
|
||||||
|
break;
|
||||||
|
case g:
|
||||||
|
h = (b - r) / d + 2;
|
||||||
|
break;
|
||||||
|
case b:
|
||||||
|
h = (r - g) / d + 4;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
h /= 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
return { h, s, l };
|
||||||
|
}
|
||||||
|
|
||||||
|
function hslToRgb({ h, s, l }: HSL): RGB {
|
||||||
|
let r: number, g: number, b: number;
|
||||||
|
|
||||||
|
if (s === 0) {
|
||||||
|
r = g = b = l; // achromatic
|
||||||
|
} else {
|
||||||
|
const hue2rgb = (p: number, q: number, t: number) => {
|
||||||
|
if (t < 0) t += 1;
|
||||||
|
if (t > 1) t -= 1;
|
||||||
|
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
||||||
|
if (t < 1 / 2) return q;
|
||||||
|
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
||||||
|
return p;
|
||||||
|
};
|
||||||
|
|
||||||
|
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
||||||
|
const p = 2 * l - q;
|
||||||
|
r = hue2rgb(p, q, h + 1 / 3);
|
||||||
|
g = hue2rgb(p, q, h);
|
||||||
|
b = hue2rgb(p, q, h - 1 / 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
r: Math.round(r * 255),
|
||||||
|
g: Math.round(g * 255),
|
||||||
|
b: Math.round(b * 255),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function hexToRgb(hex: string): RGB {
|
||||||
|
let r = 0,
|
||||||
|
g = 0,
|
||||||
|
b = 0;
|
||||||
|
// 3 digits
|
||||||
|
if (hex.length == 4) {
|
||||||
|
r = parseInt(hex[1] + hex[1], 16);
|
||||||
|
g = parseInt(hex[2] + hex[2], 16);
|
||||||
|
b = parseInt(hex[3] + hex[3], 16);
|
||||||
|
}
|
||||||
|
// 6 digits
|
||||||
|
else if (hex.length == 7) {
|
||||||
|
r = parseInt(hex.slice(1, 3), 16);
|
||||||
|
g = parseInt(hex.slice(3, 5), 16);
|
||||||
|
b = parseInt(hex.slice(5, 7), 16);
|
||||||
|
}
|
||||||
|
return { r, g, b };
|
||||||
|
}
|
||||||
|
|
||||||
|
function rgbToHex({ r, g, b }: RGB): string {
|
||||||
|
return (
|
||||||
|
"#" +
|
||||||
|
[r, g, b]
|
||||||
|
.map((x) => {
|
||||||
|
const hex = x.toString(16);
|
||||||
|
return hex.length === 1 ? "0" + hex : hex;
|
||||||
|
})
|
||||||
|
.join("")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function lightenColor(hex: string, amount: number): string {
|
||||||
|
let rgb = hexToRgb(hex);
|
||||||
|
let hsl = rgbToHsl(rgb);
|
||||||
|
hsl.l = Math.min(1, hsl.l + amount);
|
||||||
|
rgb = hslToRgb(hsl);
|
||||||
|
return rgbToHex(rgb);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user