@@ -20,13 +20,14 @@ const neighbors = [
2020 [ 1 , 1 ]
2121]
2222
23+ function isInside ( rgbData , location ) {
24+ const x = location [ 0 ]
25+ const y = location [ 1 ]
26+ return x >= 0 && x < rgbData . length && y >= 0 && y < rgbData [ 0 ] . length
27+ }
28+
2329function checkLocation ( rgbData , location ) {
24- if (
25- location [ 0 ] < 0 ||
26- location [ 0 ] >= rgbData . length ||
27- location [ 1 ] < 0 ||
28- location [ 1 ] >= rgbData [ 0 ] . length
29- ) {
30+ if ( ! isInside ( rgbData , location ) ) {
3031 throw new Error ( 'location should point to a pixel within the rgbData' )
3132 }
3233}
@@ -119,10 +120,12 @@ function depthFirstFill(rgbData, location, targetColor, replacementColor) {
119120 rgbData [ location [ 0 ] ] [ location [ 1 ] ] = replacementColor
120121
121122 for ( let i = 0 ; i < neighbors . length ; i ++ ) {
122- const x = location [ 0 ] + neighbors [ i ] [ 0 ]
123- const y = location [ 1 ] + neighbors [ i ] [ 1 ]
124- if ( x >= 0 && x < rgbData . length && y >= 0 && y < rgbData [ 0 ] . length ) {
125- depthFirstFill ( rgbData , [ x , y ] , targetColor , replacementColor )
123+ const newLocation = [
124+ location [ 0 ] + neighbors [ i ] [ 0 ] ,
125+ location [ 1 ] + neighbors [ i ] [ 1 ]
126+ ]
127+ if ( isInside ( rgbData , newLocation ) ) {
128+ depthFirstFill ( rgbData , newLocation , targetColor , replacementColor )
126129 }
127130 }
128131 }
0 commit comments