@@ -20,13 +20,14 @@ const neighbors = [
20
20
[ 1 , 1 ]
21
21
]
22
22
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
+
23
29
function 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 ) ) {
30
31
throw new Error ( 'location should point to a pixel within the rgbData' )
31
32
}
32
33
}
@@ -119,10 +120,12 @@ function depthFirstFill(rgbData, location, targetColor, replacementColor) {
119
120
rgbData [ location [ 0 ] ] [ location [ 1 ] ] = replacementColor
120
121
121
122
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 )
126
129
}
127
130
}
128
131
}
0 commit comments