99import { app } from "/scripts/app.js" ;
1010
1111const getContrastTextColor = ( hexColor ) => {
12+ if ( typeof hexColor !== 'string' || ! / ^ # ? [ 0 - 9 a - f A - 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