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

Commit 86740f9

Browse files
committed
refactor(drawRectangle): avoid duplicate code
Refs: drawRectangle and border #311
1 parent d3e5830 commit 86740f9

File tree

1 file changed

+8
-18
lines changed

1 file changed

+8
-18
lines changed

src/draw/drawRectangle.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export function drawRectangle(
6464
origin = { column: 0, row: 0 },
6565
width = image.width,
6666
height = image.height,
67-
strokeColor: color = getDefaultColor(image),
68-
fillColor,
67+
strokeColor = getDefaultColor(image),
68+
fillColor = 'none',
6969
} = options;
7070
const { column, row } = origin;
7171

@@ -79,35 +79,25 @@ export function drawRectangle(
7979
newImage = maskToOutputMask(image, options, { clone: true });
8080
}
8181

82-
if (color !== 'none') {
82+
if (strokeColor !== 'none') {
8383
for (
8484
let currentColumn = column;
8585
currentColumn < column + width;
8686
currentColumn++
8787
) {
88-
newImage.setVisiblePixel(currentColumn, row, color);
89-
newImage.setVisiblePixel(currentColumn, row + height - 1, color);
88+
newImage.setVisiblePixel(currentColumn, row, strokeColor);
89+
newImage.setVisiblePixel(currentColumn, row + height - 1, strokeColor);
9090
}
9191
for (
9292
let currentRow = row + 1;
9393
currentRow < row + height - 1;
9494
currentRow++
9595
) {
96-
newImage.setVisiblePixel(column, currentRow, color);
97-
newImage.setVisiblePixel(column + width - 1, currentRow, color);
98-
99-
if (fillColor) {
100-
if (fillColor === 'none') continue;
101-
for (let col = column + 1; col < column + width - 1; col++) {
102-
newImage.setVisiblePixel(col, currentRow, fillColor);
103-
newImage.setVisiblePixel(col, currentRow, fillColor);
104-
}
105-
}
96+
newImage.setVisiblePixel(column, currentRow, strokeColor);
97+
newImage.setVisiblePixel(column + width - 1, currentRow, strokeColor);
10698
}
10799
}
108-
// color is none but fill is defined
109-
else if (fillColor) {
110-
if (fillColor === 'none') return newImage;
100+
if (fillColor !== 'none') {
111101
for (
112102
let currentRow = row + 1;
113103
currentRow < row + height - 1;

0 commit comments

Comments
 (0)