@@ -10,6 +10,28 @@ function getColor(doc: Document, x: UnitValue, y: UnitValue): SolidColor
1010 return color ;
1111}
1212
13+ function isSelectionValid ( )
14+ {
15+ try {
16+ let bounds = app . activeDocument . selection . bounds ; // if no selection, raise error
17+
18+ let bound_width = bounds [ 2 ] . as ( 'pt' ) - bounds [ 0 ] . as ( 'pt' ) ;
19+ let bound_height = bounds [ 3 ] . as ( 'pt' ) - bounds [ 1 ] . as ( 'pt' ) ;
20+
21+ log ( "select bounds: " + bounds . toString ( ) + "bound_width=" + bound_width + " bound_height=" + bound_height ) ;
22+ if ( ( bound_width < 20 ) || ( bound_height < 20 ) ) {
23+ log ( "selection too small..." ) ;
24+ return false ;
25+ }
26+
27+ return true ;
28+ }
29+ catch ( e ) {
30+ log ( "no selection..." ) ;
31+ return false ;
32+ }
33+ }
34+
1335// clear dialog
1436// bgLayer: the background layer
1537// overLayer: the overlay layer
@@ -33,6 +55,8 @@ export function dialogClear(doc: Document, bgLayer: ArtLayer, overLayer: ArtLaye
3355 let y = labels [ i ] . y * height ;
3456 let fill_color = getColor ( doc , UnitValue ( x , 'px' ) , UnitValue ( y , 'px' ) ) ;
3557
58+ log ( "point " + i + "(" + x + "," + y + ") color=" + fill_color . rgb . hexValue . toString ( ) ) ;
59+
3660 app . activeDocument . activeLayer = bgLayer ;
3761 doc . selection . deselect ( ) ;
3862 MyAction . magicWand ( x , y , tolerance , false , true , 'addTo' ) ;
@@ -57,12 +81,19 @@ export function dialogClear(doc: Document, bgLayer: ArtLayer, overLayer: ArtLaye
5781 }
5882 MyAction . magicWand ( x , y , 0 , false , true , 'addTo' ) ;
5983 }
60- doc . selection . invert ( ) ;
61- doc . selection . contract ( contract ) ;
84+
6285 tmp_layer . remove ( ) ;
6386
64- app . activeDocument . activeLayer = overLayer ;
65- doc . selection . fill ( fill_color , ColorBlendMode . NORMAL , 100 , false ) ;
87+ doc . selection . invert ( ) ;
88+ if ( isSelectionValid ( ) ) {
89+ doc . selection . contract ( contract ) ;
90+
91+ app . activeDocument . activeLayer = overLayer ;
92+ doc . selection . fill ( fill_color , ColorBlendMode . NORMAL , 100 , false ) ;
93+ }
94+ else {
95+ log ( "selection is not valid, ignore..." ) ;
96+ }
6697 }
6798 return true ;
6899}
0 commit comments