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

Commit d3e5830

Browse files
committed
feat(drawRectangle): add fillColor = 'none' option
Closes: #311
1 parent ae0aeed commit d3e5830

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/draw/__tests__/drawRectangle.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ test('draw rectangle with no options', () => {
168168
expect(result).not.toBe(image);
169169
});
170170

171+
test('fillColor = none', () => {
172+
const image = testUtils.createGreyImage([
173+
[1, 1, 1, 1, 1, 1],
174+
[1, 1, 1, 1, 1, 1],
175+
[1, 1, 1, 1, 1, 1],
176+
[1, 1, 1, 1, 1, 1],
177+
[1, 1, 1, 1, 1, 1],
178+
[1, 1, 1, 1, 1, 1],
179+
]);
180+
const result = image.drawRectangle({
181+
width: image.width,
182+
height: image.height,
183+
fillColor: 'none',
184+
});
185+
expect(result).toMatchImageData([
186+
[0, 0, 0, 0, 0, 0],
187+
[0, 1, 1, 1, 1, 0],
188+
[0, 1, 1, 1, 1, 0],
189+
[0, 1, 1, 1, 1, 0],
190+
[0, 1, 1, 1, 1, 0],
191+
[0, 0, 0, 0, 0, 0],
192+
]);
193+
expect(result).not.toBe(image);
194+
});
195+
171196
test('outside of image', () => {
172197
const image = testUtils.createGreyImage([
173198
[1, 1, 1, 1, 1, 1],

src/draw/drawRectangle.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface DrawRectangleOptions<OutType> {
3434
* Rectangle fill color array of N elements (e.g. R, G, B or G, A), N being the number of channels.
3535
*
3636
*/
37-
fillColor?: number[];
37+
fillColor?: number[] | 'none';
3838
/**
3939
* Image to which the resulting image has to be put.
4040
*/
@@ -65,7 +65,7 @@ export function drawRectangle(
6565
width = image.width,
6666
height = image.height,
6767
strokeColor: color = getDefaultColor(image),
68-
fillColor: fill,
68+
fillColor,
6969
} = options;
7070
const { column, row } = origin;
7171

@@ -96,16 +96,18 @@ export function drawRectangle(
9696
newImage.setVisiblePixel(column, currentRow, color);
9797
newImage.setVisiblePixel(column + width - 1, currentRow, color);
9898

99-
if (fill) {
99+
if (fillColor) {
100+
if (fillColor === 'none') continue;
100101
for (let col = column + 1; col < column + width - 1; col++) {
101-
newImage.setVisiblePixel(col, currentRow, fill);
102-
newImage.setVisiblePixel(col, currentRow, fill);
102+
newImage.setVisiblePixel(col, currentRow, fillColor);
103+
newImage.setVisiblePixel(col, currentRow, fillColor);
103104
}
104105
}
105106
}
106107
}
107108
// color is none but fill is defined
108-
else if (fill) {
109+
else if (fillColor) {
110+
if (fillColor === 'none') return newImage;
109111
for (
110112
let currentRow = row + 1;
111113
currentRow < row + height - 1;
@@ -116,8 +118,8 @@ export function drawRectangle(
116118
currentColumn < column + width - 1;
117119
currentColumn++
118120
) {
119-
newImage.setVisiblePixel(currentColumn, currentRow, fill);
120-
newImage.setVisiblePixel(currentColumn, currentRow, fill);
121+
newImage.setVisiblePixel(currentColumn, currentRow, fillColor);
122+
newImage.setVisiblePixel(currentColumn, currentRow, fillColor);
121123
}
122124
}
123125
}

0 commit comments

Comments
 (0)