Skip to content

Commit 5640586

Browse files
authored
Add files via upload
1 parent ff0af69 commit 5640586

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

web/js/colorWidget.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
import { app } from "/scripts/app.js";
1010

1111
const getContrastTextColor = (hexColor) => {
12+
if (typeof hexColor !== 'string' || !/^#?[0-9a-fA-F]{6}$/.test(hexColor)) {
13+
return '#cccccc'; // fallback text color
14+
}
15+
1216
const hex = hexColor.replace('#', '');
1317
const r = parseInt(hex.substr(0, 2), 16);
1418
const g = parseInt(hex.substr(2, 2), 16);
1519
const b = parseInt(hex.substr(4, 2), 16);
1620
const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
21+
1722
return luminance > 0.5 ? '#333333' : '#cccccc';
1823
};
1924

@@ -24,7 +29,7 @@ const AILabColorWidget = {
2429
widget.name = key;
2530
widget.type = 'COLOR';
2631
widget.options = { default: '#222222' };
27-
widget.value = val || '#222222';
32+
widget.value = typeof val === 'string' ? val : '#222222'; // validate incoming value
2833

2934
widget.draw = function (ctx, node, widgetWidth, widgetY, height) {
3035
const hide = this.type !== 'COLOR' && app.canvas.ds.scale > 0.5;
@@ -75,8 +80,8 @@ const AILabColorWidget = {
7580
picker.value = this.value;
7681

7782
picker.style.position = 'absolute';
78-
picker.style.left = '999999px';
79-
picker.style.top = '999999px';
83+
picker.style.left = '-9999px';
84+
picker.style.top = '-9999px';
8085

8186
document.body.appendChild(picker);
8287

@@ -110,7 +115,7 @@ app.registerExtension({
110115
COLOR: (node, inputName, inputData) => {
111116
return {
112117
widget: node.addCustomWidget(
113-
AILabColorWidget.COLOR(inputName, inputData[1]?.default || '#222222')
118+
AILabColorWidget.COLOR(inputName, inputData?.[1]?.default || '#222222')
114119
),
115120
minWidth: 150,
116121
minHeight: 32,

0 commit comments

Comments
 (0)