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

Commit ede94d3

Browse files
committed
refactor: update passing of color argument for blended pixels
1 parent 688e98a commit ede94d3

File tree

9 files changed

+33
-94
lines changed

9 files changed

+33
-94
lines changed

src/draw/drawCircleOnImage.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,20 @@ export function drawCircleOnImage(
6161
radius = Math.round(radius);
6262

6363
if (radius === 0) {
64-
setBlendedVisiblePixel(newImage, center.column, center.row, { color });
64+
setBlendedVisiblePixel(newImage, center.column, center.row, color);
6565
return newImage;
6666
}
6767

6868
if (!fill) {
6969
circle(center.column, center.row, radius, (column: number, row: number) => {
70-
setBlendedVisiblePixel(newImage, column, row, { color });
70+
setBlendedVisiblePixel(newImage, column, row, color);
7171
});
7272
} else {
7373
if (radius === 1) {
74-
setBlendedVisiblePixel(newImage, center.column, center.row, {
75-
color: fill,
76-
});
74+
setBlendedVisiblePixel(newImage, center.column, center.row, fill);
7775
}
7876
circle(center.column, center.row, radius, (column: number, row: number) => {
79-
setBlendedVisiblePixel(newImage, column, row, { color });
77+
setBlendedVisiblePixel(newImage, column, row, color);
8078

8179
//todo: fill is not optimal we can fill symmetrically
8280
if (column - 1 > center.column) {

src/draw/drawLineOnImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function drawLineOnImage(
5757
Math.round(origin.column + to.column),
5858
Math.round(origin.row + to.row),
5959
(column: number, row: number) => {
60-
setBlendedVisiblePixel(newImage, column, row, { color });
60+
setBlendedVisiblePixel(newImage, column, row, color);
6161
},
6262
);
6363
return newImage;

src/draw/drawPoints.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function drawPoints(
6666
newImage,
6767
Math.round(origin.column + point.column),
6868
Math.round(origin.row + point.row),
69-
{ color },
69+
color,
7070
);
7171
}
7272

src/draw/drawPolygonOnImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export function drawPolygonOnImage(
6666
newImage,
6767
Math.round(origin.column) + column,
6868
Math.round(origin.row) + row,
69-
{ color: fillColor },
69+
fillColor,
7070
);
7171
}
7272
}

src/draw/drawRectangle.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,26 @@ export function drawRectangle(
8383
currentColumn < column + width;
8484
currentColumn++
8585
) {
86-
setBlendedVisiblePixel(newImage, currentColumn, row, {
87-
color: strokeColor,
88-
});
89-
setBlendedVisiblePixel(newImage, currentColumn, row + height - 1, {
90-
color: strokeColor,
91-
});
86+
setBlendedVisiblePixel(newImage, currentColumn, row, strokeColor);
87+
setBlendedVisiblePixel(
88+
newImage,
89+
currentColumn,
90+
row + height - 1,
91+
strokeColor,
92+
);
9293
}
9394
for (
9495
let currentRow = row + 1;
9596
currentRow < row + height - 1;
9697
currentRow++
9798
) {
98-
setBlendedVisiblePixel(newImage, column, currentRow, {
99-
color: strokeColor,
100-
});
101-
setBlendedVisiblePixel(newImage, column + width - 1, currentRow, {
102-
color: strokeColor,
103-
});
99+
setBlendedVisiblePixel(newImage, column, currentRow, strokeColor);
100+
setBlendedVisiblePixel(
101+
newImage,
102+
column + width - 1,
103+
currentRow,
104+
strokeColor,
105+
);
104106
}
105107
}
106108
if (fillColor !== 'none') {
@@ -114,12 +116,7 @@ export function drawRectangle(
114116
currentColumn < column + width - 1;
115117
currentColumn++
116118
) {
117-
/* setBlendedVisiblePixel(newImage, currentColumn, currentRow, {
118-
color: fillColor,
119-
});*/
120-
setBlendedVisiblePixel(newImage, currentColumn, currentRow, {
121-
color: fillColor,
122-
});
119+
setBlendedVisiblePixel(newImage, currentColumn, currentRow, fillColor);
123120
}
124121
}
125122
}

src/operations/copyTo.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,7 @@ export function copyTo(
6969
currentColumn - column,
7070
currentRow - row,
7171
);
72-
setBlendedPixel(result, currentColumn, currentRow, {
73-
color: sourcePixel,
74-
});
72+
setBlendedPixel(result, currentColumn, currentRow, sourcePixel);
7573
}
7674
}
7775

src/operations/paintMaskOnImage.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ export function paintMaskOnImage(
6868
currentColumn++
6969
) {
7070
if (mask.getBit(currentColumn - column, currentRow - row)) {
71-
setBlendedPixel(result, currentColumn, currentRow, {
72-
color,
73-
});
71+
setBlendedPixel(result, currentColumn, currentRow, color);
7472
}
7573
}
7674
}

src/utils/setBlendedPixel.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,21 @@ import { Mask } from '../Mask';
44
import { getDefaultColor } from './getDefaultColor';
55
import { assert } from './validators/assert';
66

7-
export interface SetBlendedPixelOptions {
8-
/**
9-
* Color with which to blend the image pixel.
10-
* @default `'Opaque black'`.
11-
*/
12-
color?: number[];
13-
}
14-
157
/**
168
* Blend the given pixel with the pixel at the specified location in the image.
179
* @param image - The image with which to blend.
1810
* @param column - Column of the target pixel.
1911
* @param row - Row of the target pixel.
20-
* @param options - Set blended pixel options.
12+
* @param color - Color with which to blend the image pixel. @default `'Opaque black'`.
2113
*/
14+
2215
export function setBlendedPixel(
2316
image: Image | Mask,
2417
column: number,
2518
row: number,
26-
options: SetBlendedPixelOptions = {},
19+
color?: number[],
2720
) {
28-
const { color = getDefaultColor(image) } = options;
21+
color = color ?? getDefaultColor(image);
2922

3023
if (!image.alpha) {
3124
image.setPixel(column, row, color);

src/utils/setBlendedVisiblePixel.ts

Lines changed: 5 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,22 @@
11
import { Image } from '../Image';
22
import { Mask } from '../Mask';
33

4-
import { getDefaultColor } from './getDefaultColor';
5-
import { assert } from './validators/assert';
6-
7-
export interface SetBlendedVisiblePixelOptions {
8-
/**
9-
* Color with which to blend the image pixel.
10-
* @default `'Opaque black'`.
11-
*/
12-
color?: number[];
13-
}
4+
import { setBlendedPixel } from './setBlendedPixel';
145

156
/**
167
* Blend the given pixel with the pixel at the specified location in the image if the pixel is in image's bounds.
178
* @param image - The image with which to blend.
189
* @param column - Column of the target pixel.
1910
* @param row - Row of the target pixel.
20-
* @param options - Set blended pixel options.
11+
* @param color - Color with which to blend the image pixel. @default `'Opaque black'`.
2112
*/
2213
export function setBlendedVisiblePixel(
2314
image: Image | Mask,
2415
column: number,
2516
row: number,
26-
options: SetBlendedVisiblePixelOptions = {},
17+
color?: number[],
2718
) {
28-
const { color = getDefaultColor(image) } = options;
29-
30-
if (!image.alpha) {
31-
image.setVisiblePixel(column, row, color);
32-
} else {
33-
assert(image instanceof Image);
34-
35-
const sourceAlpha = color.at(-1) as number;
36-
37-
if (sourceAlpha === image.maxValue) {
38-
image.setVisiblePixel(column, row, color);
39-
return;
40-
}
41-
42-
const targetAlpha = image.getValue(column, row, image.channels - 1);
43-
44-
const newAlpha =
45-
sourceAlpha + targetAlpha * (1 - sourceAlpha / image.maxValue);
46-
if (column >= 0 && column < image.width && row >= 0 && row < image.height) {
47-
image.setValue(column, row, image.channels - 1, newAlpha);
48-
}
49-
for (let component = 0; component < image.components; component++) {
50-
const sourceComponent = color[component];
51-
const targetComponent = image.getValue(column, row, component);
52-
53-
const newComponent =
54-
(sourceComponent * sourceAlpha +
55-
targetComponent * targetAlpha * (1 - sourceAlpha / image.maxValue)) /
56-
newAlpha;
57-
if (
58-
column >= 0 &&
59-
column < image.width &&
60-
row >= 0 &&
61-
row < image.height
62-
) {
63-
image.setValue(column, row, component, newComponent);
64-
}
65-
}
19+
if (column >= 0 && column < image.width && row >= 0 && row < image.height) {
20+
setBlendedPixel(image, column, row, color);
6621
}
6722
}

0 commit comments

Comments
 (0)