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

Commit 3b92425

Browse files
committed
feat: expose image.minMax
Refs: #307
1 parent fc6c3d5 commit 3b92425

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

src/Image.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ import {
8888
} from './operations';
8989
import { cropAlpha, CropAlphaOptions } from './operations/cropAlpha';
9090
import { ImageColorModel, colorModels } from './utils/constants/colorModels';
91+
import { getMinMax } from './utils/getMinMax';
9192
import { validateChannel, validateValue } from './utils/validators';
9293

9394
import {
@@ -447,6 +448,15 @@ export class Image {
447448
this.setValue(point.column, point.row, channel, value);
448449
}
449450

451+
/**
452+
* Find the min and max values of each channel of the image.
453+
*
454+
* @returns An object with arrays of the min and max values.
455+
*/
456+
public minMax(): { min: number[]; max: number[] } {
457+
return getMinMax(this);
458+
}
459+
450460
/**
451461
* Return the raw image data.
452462
*

src/utils/__tests__/getMinMax.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,15 @@ test('rgba image', () => {
3535
max: [4, 9, 7, 7],
3636
});
3737
});
38+
39+
test('image.minMax', () => {
40+
const image = testUtils.createRgbaImage([
41+
[1, 2, 3, 8],
42+
[5, 1, 0, 5],
43+
[7, 9, 2, 7],
44+
]);
45+
expect(image.minMax()).toStrictEqual({
46+
min: [1, 1, 0, 5],
47+
max: [7, 9, 3, 8],
48+
});
49+
});

src/utils/getMinMax.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { Image } from '../Image';
1+
import { Image } from '..';
22

33
/**
44
* Find the min and max values of each channel of the image.
55
*
66
* @param image - Image to process.
77
* @returns An object with arrays of the min and max values.
88
*/
9-
export function getMinMax(image: Image) {
9+
export function getMinMax(image: Image): { min: number[]; max: number[] } {
1010
const min = new Array(image.channels).fill(image.maxValue);
1111
const max = new Array(image.channels).fill(0);
1212

0 commit comments

Comments
 (0)