|
| 1 | +/** |
| 2 | + * COLOR Widget for ComfyUI |
| 3 | + * |
| 4 | + * This integration script is licensed under the GNU General Public License v3.0 (GPL-3.0). |
| 5 | + * If you incorporate or modify this code, please credit AILab as the original source: |
| 6 | + * https://github.com/1038lab |
| 7 | + */ |
| 8 | + |
| 9 | +import { app } from "/scripts/app.js"; |
| 10 | + |
| 11 | +const getContrastTextColor = (hexColor) => { |
| 12 | + const hex = hexColor.replace('#', ''); |
| 13 | + const r = parseInt(hex.substr(0, 2), 16); |
| 14 | + const g = parseInt(hex.substr(2, 2), 16); |
| 15 | + const b = parseInt(hex.substr(4, 2), 16); |
| 16 | + const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255; |
| 17 | + return luminance > 0.5 ? '#333333' : '#cccccc'; |
| 18 | +}; |
| 19 | + |
| 20 | +const AILabColorWidget = { |
| 21 | + COLOR: (key, val) => { |
| 22 | + const widget = {}; |
| 23 | + widget.y = 0; |
| 24 | + widget.name = key; |
| 25 | + widget.type = 'COLOR'; |
| 26 | + widget.options = { default: '#222222' }; |
| 27 | + widget.value = val || '#222222'; |
| 28 | + |
| 29 | + widget.draw = function (ctx, node, widgetWidth, widgetY, height) { |
| 30 | + const hide = this.type !== 'COLOR' && app.canvas.ds.scale > 0.5; |
| 31 | + if (hide) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const margin = 15; |
| 36 | + const radius = 12; |
| 37 | + |
| 38 | + ctx.fillStyle = this.value; |
| 39 | + ctx.beginPath(); |
| 40 | + const x = margin; |
| 41 | + const y = widgetY; |
| 42 | + const w = widgetWidth - margin * 2; |
| 43 | + const h = height; |
| 44 | + ctx.moveTo(x + radius, y); |
| 45 | + ctx.lineTo(x + w - radius, y); |
| 46 | + ctx.quadraticCurveTo(x + w, y, x + w, y + radius); |
| 47 | + ctx.lineTo(x + w, y + h - radius); |
| 48 | + ctx.quadraticCurveTo(x + w, y + h, x + w - radius, y + h); |
| 49 | + ctx.lineTo(x + radius, y + h); |
| 50 | + ctx.quadraticCurveTo(x, y + h, x, y + h - radius); |
| 51 | + ctx.lineTo(x, y + radius); |
| 52 | + ctx.quadraticCurveTo(x, y, x + radius, y); |
| 53 | + ctx.closePath(); |
| 54 | + ctx.fill(); |
| 55 | + |
| 56 | + ctx.strokeStyle = '#555'; |
| 57 | + ctx.lineWidth = 1; |
| 58 | + ctx.stroke(); |
| 59 | + |
| 60 | + ctx.fillStyle = getContrastTextColor(this.value); |
| 61 | + ctx.font = '12px sans-serif'; |
| 62 | + ctx.textAlign = 'center'; |
| 63 | + |
| 64 | + const text = `background_color (${this.value})`; |
| 65 | + ctx.fillText(text, widgetWidth * 0.5, widgetY + height * 0.65); |
| 66 | + }; |
| 67 | + |
| 68 | + widget.mouse = function (e, pos, node) { |
| 69 | + if (e.type === 'pointerdown') { |
| 70 | + const margin = 15; |
| 71 | + |
| 72 | + if (pos[0] >= margin && pos[0] <= node.size[0] - margin) { |
| 73 | + const picker = document.createElement('input'); |
| 74 | + picker.type = 'color'; |
| 75 | + picker.value = this.value; |
| 76 | + |
| 77 | + picker.style.position = 'absolute'; |
| 78 | + picker.style.left = '999999px'; |
| 79 | + picker.style.top = '999999px'; |
| 80 | + |
| 81 | + document.body.appendChild(picker); |
| 82 | + |
| 83 | + picker.addEventListener('change', () => { |
| 84 | + this.value = picker.value; |
| 85 | + node.graph._version++; |
| 86 | + node.setDirtyCanvas(true, true); |
| 87 | + picker.remove(); |
| 88 | + }); |
| 89 | + |
| 90 | + picker.click(); |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + return false; |
| 95 | + }; |
| 96 | + |
| 97 | + widget.computeSize = function (width) { |
| 98 | + return [width, 32]; |
| 99 | + }; |
| 100 | + |
| 101 | + return widget; |
| 102 | + } |
| 103 | +}; |
| 104 | + |
| 105 | +app.registerExtension({ |
| 106 | + name: "AILab.colorWidget", |
| 107 | + |
| 108 | + getCustomWidgets() { |
| 109 | + return { |
| 110 | + COLOR: (node, inputName, inputData) => { |
| 111 | + return { |
| 112 | + widget: node.addCustomWidget( |
| 113 | + AILabColorWidget.COLOR(inputName, inputData[1]?.default || '#222222') |
| 114 | + ), |
| 115 | + minWidth: 150, |
| 116 | + minHeight: 32, |
| 117 | + }; |
| 118 | + } |
| 119 | + }; |
| 120 | + } |
| 121 | +}); |
0 commit comments