File size: 10,085 Bytes
1e3b872
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { app } from '../../../scripts/app.js'
import { $el } from '../../../scripts/ui.js' 

const getLocalData = key => {
  let data = {}
  try {
    data = JSON.parse(localStorage.getItem(key)) || {}
  } catch (error) {
    return {}
  }
  return data
}
function get_position_style (ctx, widget_width, y, node_height) {
  const MARGIN = 4 // the margin around the html element

  /* Create a transform that deals with all the scrolling and zooming */
  const elRect = ctx.canvas.getBoundingClientRect()
  const transform = new DOMMatrix()
    .scaleSelf(
      elRect.width / ctx.canvas.width,
      elRect.height / ctx.canvas.height
    )
    .multiplySelf(ctx.getTransform())
    .translateSelf(MARGIN, MARGIN + y)

  return {
    transformOrigin: '0 0',
    transform: transform,
    left: `0`,
    top: `0`,
    cursor: 'pointer',
    position: 'absolute',
    maxWidth: `${widget_width - MARGIN * 2}px`,
    // maxHeight: `${node_height - MARGIN * 2}px`, // we're assuming we have the whole height of the node
    width: `${widget_width - MARGIN * 2}px`,
    // height: `${node_height * 0.3 - MARGIN * 2}px`,
    // background: '#EEEEEE',
    display: 'flex',
    flexDirection: 'column',
    // alignItems: 'center',
    justifyContent: 'space-around'
  }
}

function hexToRGBA (hexColor) {
  var hex = hexColor.replace('#', '')
  var r = parseInt(hex.substring(0, 2), 16)
  var g = parseInt(hex.substring(2, 4), 16)
  var b = parseInt(hex.substring(4, 6), 16)

  // 获取透明度的十六进制值
  var alphaHex = hex.substring(6)

  // 将透明度的十六进制值转换为十进制值
  var alpha = parseInt(alphaHex, 16) / 255

  return [r, g, b, alpha]
}

app.registerExtension({
  name: 'Mixlab.utils.Color',
  init () {
    $el('link', {
      rel: 'stylesheet',
      href: '/extensions/comfyui-mixlab-nodes/lib/classic.min.css',
      parent: document.head
    })

    $el('style', {
      textContent: `
      .pickr{
        display: flex;
        justify-content: center;
        align-items: center;
      }
				.pickr .pcr-button {
					width: 56px;
          height: 56px;
          outline: 1px solid white;
				}
				 
			`,
      parent: document.body
    })
  },
  async getCustomWidgets (app) {
    return {
      TCOLOR (node, inputName, inputData, app) {
        // console.log('##node', node)
        const widget = {
          type: inputData[0], // the type, CHEESE
          name: inputName, // the name, slice
          size: [128, 32], // a default size
          draw (ctx, node, width, y) {},
          computeSize (...args) {
            return [128, 32] // a method to compute the current size of the widget
          },
          async serializeValue (nodeId, widgetIndex) {
            // let data = getLocalData('_mixlab_utils_color')
            // let hex = data[node.id] || '#000000'
            let hex = widget.value || '#000000'
            let [r, g, b, a] = hexToRGBA(hex)
            return {
              hex,
              r,
              g,
              b,
              a
            }
          }
        }
        //  widget.something = something;          // maybe adds stuff to it
        node.addCustomWidget(widget) // adds it to the node
        return widget // and returns it.
      }
    }
  },

  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeType.comfyClass == 'Color') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = function () {
        orig_nodeCreated?.apply(this, arguments)

        // console.log('Color nodeData', this.widgets)

        const widget = {
          type: 'div',
          name: 'input_color',
          draw (ctx, node, widget_width, y, widget_height) {
            Object.assign(
              this.div.style,
              get_position_style(ctx, widget_width, 44, node.size[1])
            )
            // console.log('draw',y,node.widgets[0].last_y)
          }
        }

        widget.div = $el('div', {})

        document.body.appendChild(widget.div)

        const inputDiv = () => {
          let div = document.createElement('div')
          div.id = `color_picker_${this.id}`
          return div
        }

        let inputColor = inputDiv('_mixlab_utils_color', 'Color', '#000000')

        widget.div.appendChild(inputColor)

        this.addCustomWidget(widget)

        const pickr = Pickr.create({
          el: `#${inputColor.id}`,
          theme: 'classic', // or 'monolith', or 'nano'
          // closeOnScroll: true,
          default: '#000000',
          swatches: [
            'rgba(244, 67, 54, 1)',
            'rgba(233, 30, 99, 0.95)',
            'rgba(156, 39, 176, 0.9)',
            'rgba(103, 58, 183, 0.85)',
            'rgba(63, 81, 181, 0.8)',
            'rgba(33, 150, 243, 0.75)',
            'rgba(3, 169, 244, 0.7)',
            'rgba(0, 188, 212, 0.7)',
            'rgba(0, 150, 136, 0.75)',
            'rgba(76, 175, 80, 0.8)',
            'rgba(139, 195, 74, 0.85)',
            'rgba(205, 220, 57, 0.9)',
            'rgba(255, 235, 59, 0.95)',
            'rgba(255, 193, 7, 1)'
          ],

          components: {
            // Main components
            preview: true,
            opacity: true,
            hue: true,
            // Input / output Options
            interaction: {
              hex: true,
              rgba: true,
              hsla: true,
              hsva: true,
              cmyk: true,
              input: true,
              // clear: true,
              save: true,
              cancel: true
            }
          }
        })

        pickr
          .on('save', (color, instance) => {
            // console.log('Event: "save"', color.toHEXA().toString())
            // let data = getLocalData('_mixlab_utils_color')
            // data[this.id] = color.toHEXA().toString()
            // localStorage.setItem('_mixlab_utils_color', JSON.stringify(data))

            try {
              let tc = this.widgets.filter(w => w.type == 'TCOLOR')[0]
              tc.value = color.toHEXA().toString()
            } catch (error) {}
          })
          .on('cancel', instance => {
            pickr && pickr.hide()
          })
        this.pickr = pickr
        const handleMouseWheel = () => {
          try {
            this.pickr && this.pickr.hide()
          } catch (error) {}
        }

        document.addEventListener('wheel', handleMouseWheel)

        const onRemoved = this.onRemoved
        this.onRemoved = () => {
          inputColor.remove()
          widget.div.remove()

          try {
            this.pickr.destroyAndRemove()
            this.pickr = null
            document.removeEventListener('wheel', handleMouseWheel)
          } catch (error) {
            console.log(error)
          }

          return onRemoved?.()
        }

        this.serialize_widgets = true //需要保存参数
      }
    }
  },
  async loadedGraphNode (node, app) {
    // Fires every time a node is constructed
    // You can modify widgets/add handlers/etc here

    if (node.type === 'Color') {
      try {
        let TCOLOR = node.widgets.filter(w => w.type == 'TCOLOR')[0]

        setTimeout(() => node.pickr.setColor(TCOLOR.value || '#000000'), 1000)
      } catch (error) {}
    }
  }
})

app.registerExtension({
  name: 'Mixlab.utils.TextToNumber',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    if (nodeType.comfyClass == 'TextToNumber') {
      const onExecuted = nodeType.prototype.onExecuted
      nodeType.prototype.onExecuted = function (message) {
        onExecuted?.apply(this, arguments)
        const random_number = this.widgets.filter(
          w => w.name === 'random_number'
        )[0]
        if (random_number.value === 'enable') {
          const n = this.widgets.filter(w => w.name === 'number')[0]
          n.value = message.num[0]
        }
        console.log('TextToNumber', random_number.value)
      }
    }
  }
})

const min_max = node => {
  if(node.widgets){
    const min_value = node.widgets.filter(w => w.name === 'min_value')[0]
    const max_value = node.widgets.filter(w => w.name === 'max_value')[0]
  
    const number = node.widgets.filter(w => w.name === 'number')[0]
    if (number) {
      number.options.min = min_value.value
      number.options.max = max_value.value
  
      number.value = Math.min(number.options.max, number.value)
      number.value = Math.max(number.options.min, number.value)
    }
  
    if (min_value)
      min_value.callback = e => {
        number.options.min = e
        number.value = e
      }
    if (max_value)
      max_value.callback = e => {
        number.options.max = e
        number.value = e
      }
  }
  
}

app.registerExtension({
  name: 'Mixlab.utils.FloatSlider',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {

    if (nodeType.comfyClass == 'FloatSlider') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated;
      nodeType.prototype.onNodeCreated = function () {
        orig_nodeCreated?.apply(this, arguments)
        min_max(this)
      }
    }

   
  },
  async loadedGraphNode (node, app) {
    if (node.type === 'FloatSlider') {
      min_max(node)
    }
  }
})
app.registerExtension({
  name: 'Mixlab.utils.IntNumber',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    
    if (nodeType.comfyClass == 'IntNumber') {
      const orig_nodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = function () {
        orig_nodeCreated?.apply(this, arguments)
        min_max(this)
      }
    }
   
  },
  async loadedGraphNode (node, app) {
    if (node.type === 'IntNumber') {
      min_max(node)
    }
  }
})


app.registerExtension({
  name: 'Mixlab.utils.TESTNODE_',
  async beforeRegisterNodeDef (nodeType, nodeData, app) {
    
    if (nodeType.comfyClass == 'TESTNODE_') {
    
      const onExecuted = nodeType.prototype.onExecuted;
			nodeType.prototype.onExecuted = function (message) {
				onExecuted?.apply(this, arguments);
        console.log('##',message)
				 
			};


    }
   
  }, 
})