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

Commit 09e739b

Browse files
committed
fix: channel validator
Closes: #371
1 parent 2b771f3 commit 09e739b

File tree

3 files changed

+5
-7
lines changed

3 files changed

+5
-7
lines changed

src/__tests__/Image.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ test('fill channel 2', () => {
279279
test('fill channel invalid channel', () => {
280280
const img = new Image(1, 2);
281281
expect(() => img.fillChannel(4, 50)).toThrow(
282-
/invalid channel: 4. It must be a positive integer smaller than 4/,
282+
/invalid channel: 4. It must be a positive integer smaller than 3/,
283283
);
284284
});
285285

src/draw/__tests__/drawPolygonOnImage.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ test('stroke color not compatible with image', () => {
280280
fillColor: [2, 5],
281281
});
282282
}).toThrow(
283-
'invalid channel: 2. It must be a positive integer smaller than 2',
283+
'invalid channel: 1. It must be a positive integer smaller than 1',
284284
);
285285
});
286286

src/utils/validators/validators.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ export function validateChannels(channels: number[], image: Image): void {
1818
* @param image - The image being processed.
1919
*/
2020
export function validateChannel(channel: number, image: Image): void {
21-
if (!Number.isInteger(channel) || channel > image.channels || channel < 0) {
21+
if (!Number.isInteger(channel) || channel >= image.channels || channel < 0) {
2222
throw new RangeError(
23-
`invalid channel: ${channel}. It must be a positive integer smaller than ${
24-
image.channels + 1
25-
}`,
23+
`invalid channel: ${channel}. It must be a positive integer smaller than ${image.channels}`,
2624
);
2725
}
2826
}
@@ -79,6 +77,6 @@ export function validateForComparison(
7977
* @param image - Image.
8078
*/
8179
export function validateColor(color: number[], image: Image): void {
82-
validateChannel(color.length, image);
80+
validateChannel(color.length - 1, image);
8381
validateValues(color, image);
8482
}

0 commit comments

Comments
 (0)