Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 98d27a4

Browse files
authored
feat: add color options to clearBorder (#291)
1 parent b7894a8 commit 98d27a4

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/morphology/__tests__/clearBorder.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ test('Diagonal of 1, allow corners', () => {
8888
]);
8989
});
9090

91+
test('Diagonal of 1, color=black', () => {
92+
let image = testUtils.createMask([
93+
[1, 0, 0, 0, 0],
94+
[0, 1, 0, 1, 0],
95+
[0, 0, 1, 0, 0],
96+
[0, 0, 0, 1, 0],
97+
[0, 0, 0, 0, 1],
98+
]);
99+
image = image.invert();
100+
let result = image.clearBorder({ color: 'black' });
101+
result = result.invert();
102+
expect(result).toMatchMaskData([
103+
[0, 0, 0, 0, 0],
104+
[0, 1, 0, 1, 0],
105+
[0, 0, 1, 0, 0],
106+
[0, 0, 0, 1, 0],
107+
[0, 0, 0, 0, 0],
108+
]);
109+
});
110+
91111
test('5x5 mask, full', () => {
92112
let image = testUtils.createMask([
93113
[1, 1, 1, 1, 1],

src/morphology/clearBorder.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ export interface ClearBorderOptions {
1414
* Image to which the resulting image has to be put.
1515
*/
1616
out?: Mask;
17+
/**
18+
* Clear either white or black area touching the border.
19+
* In practice it will invert the color.
20+
*
21+
* @default 'white'
22+
*/
23+
color?: 'white' | 'black';
1724
}
1825
/**
1926
* Set the pixels connected to the border of the mask to zero. You can chose to allow corner connection of not with the `allowCorners` option.
@@ -26,11 +33,11 @@ export function clearBorder(
2633
mask: Mask,
2734
options: ClearBorderOptions = {},
2835
): Mask {
29-
let { allowCorners = false, out } = options;
36+
let { allowCorners = false, out, color = 'white' } = options;
3037
return multipleFloodFill(mask, {
3138
startPixels: borderIterator(mask),
32-
startPixelValue: 1,
33-
newPixelValue: 0,
39+
startPixelValue: color === 'white' ? 1 : 0,
40+
newPixelValue: color === 'white' ? 0 : 1,
3441
allowCorners,
3542
out,
3643
});

0 commit comments

Comments
 (0)