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

Commit 4d254d3

Browse files
feat: add setClampedValue and setClampedValueByIndex (#443)
1 parent 7fe130e commit 4d254d3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/Image.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,24 @@ export class Image {
431431
this.data[(row * this.width + column) * this.channels + channel] = value;
432432
}
433433

434+
/**
435+
* Set the value of a specific pixel channel. Select pixel using coordinates.
436+
* If the value is out of range it is set to the closest extremety.
437+
* @param column - Column index.
438+
* @param row - Row index.
439+
* @param channel - Channel index.
440+
* @param value - Value to set.
441+
*/
442+
public setClampedValue(
443+
column: number,
444+
row: number,
445+
channel: number,
446+
value: number,
447+
): void {
448+
if (value < 0) value = 0;
449+
else if (value > this.maxValue) value = this.maxValue;
450+
this.data[(row * this.width + column) * this.channels + channel] = value;
451+
}
434452
/**
435453
* Get the value of a specific pixel channel. Select pixel using index.
436454
* @param index - Index of the pixel.
@@ -450,6 +468,23 @@ export class Image {
450468
this.data[index * this.channels + channel] = value;
451469
}
452470

471+
/**
472+
* Set the value of a specific pixel channel. Select pixel using index.
473+
* If the value is out of range it is set to the closest extremety.
474+
* @param index - Index of the pixel.
475+
* @param channel - Channel index.
476+
* @param value - Value to set.
477+
*/
478+
public setClampedValueByIndex(
479+
index: number,
480+
channel: number,
481+
value: number,
482+
): void {
483+
if (value < 0) value = 0;
484+
else if (value > this.maxValue) value = this.maxValue;
485+
this.data[index * this.channels + channel] = value;
486+
}
487+
453488
/**
454489
* Get the value of a specific pixel channel. Select pixel using a point.
455490
* @param point - Coordinates of the desired pixel.

src/__tests__/Image.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,21 @@ describe('get and set pixels', () => {
155155
img.setValueByPoint(point, 0, 50);
156156
expect(img.getValueByPoint(point, 0)).toBe(50);
157157
});
158+
it('should set clamped value', () => {
159+
const img = new Image(10, 20);
160+
img.setClampedValue(15, 0, 0, -50);
161+
expect(img.getValue(15, 0, 0)).toBe(0);
162+
img.setClampedValue(15, 0, 0, 99999);
163+
expect(img.getValue(15, 0, 0)).toBe(img.maxValue);
164+
});
165+
it('should set clamped value by index', () => {
166+
const img = new Image(10, 20);
167+
expect(img.getValueByIndex(15, 0)).toBe(0);
168+
img.setClampedValueByIndex(15, 0, -50);
169+
expect(img.getValueByIndex(15, 0)).toBe(0);
170+
img.setClampedValueByIndex(15, 0, 999999);
171+
expect(img.getValueByIndex(15, 0)).toBe(img.maxValue);
172+
});
158173
});
159174

160175
test('createFrom', () => {

0 commit comments

Comments
 (0)