File tree 1 file changed +13
-16
lines changed
1 file changed +13
-16
lines changed Original file line number Diff line number Diff line change @@ -20,6 +20,17 @@ const neighbors = [
20
20
[ 1 , 1 ]
21
21
]
22
22
23
+ 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
+ throw new Error ( 'location should point to a pixel within the rgbData' )
31
+ }
32
+ }
33
+
23
34
/**
24
35
* Implements the flood fill algorithm through a breadth-first approach using a queue.
25
36
*
@@ -34,14 +45,7 @@ export function breadthFirstSearch(
34
45
targetColor ,
35
46
replacementColor
36
47
) {
37
- if (
38
- location [ 0 ] < 0 ||
39
- location [ 0 ] >= rgbData . length ||
40
- location [ 1 ] < 0 ||
41
- location [ 1 ] >= rgbData [ 0 ] . length
42
- ) {
43
- throw new Error ( 'location should point to a pixel within the rgbData' )
44
- }
48
+ checkLocation ( rgbData , location )
45
49
46
50
const queue = [ ]
47
51
queue . push ( location )
@@ -65,14 +69,7 @@ export function depthFirstSearch(
65
69
targetColor ,
66
70
replacementColor
67
71
) {
68
- if (
69
- location [ 0 ] < 0 ||
70
- location [ 0 ] >= rgbData . length ||
71
- location [ 1 ] < 0 ||
72
- location [ 1 ] >= rgbData [ 0 ] . length
73
- ) {
74
- throw new Error ( 'location should point to a pixel within the rgbData' )
75
- }
72
+ checkLocation ( rgbData , location )
76
73
77
74
depthFirstFill ( rgbData , location , targetColor , replacementColor )
78
75
}
You can’t perform that action at this time.
0 commit comments