-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathjimp-image-processor.class.ts
35 lines (34 loc) · 1.15 KB
/
jimp-image-processor.class.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import Jimp from "jimp";
import { Image } from "../../image.class";
import { Point } from "../../point.class";
import { ImageProcessor } from "../image-processor.interface";
import { imageToJimp } from "../io/imageToJimp.function";
import { RGBA } from "../../rgba.class";
export default class implements ImageProcessor {
colorAt(
image: Image | Promise<Image>,
point: Point | Promise<Point>
): Promise<RGBA> {
return new Promise<RGBA>(async (resolve, reject) => {
const location = await point;
const img = await image;
if (location.x < 0 || location.x >= img.width) {
reject(
`Query location out of bounds. Should be in range 0 <= x < image.width, is ${location.x}`
);
return;
}
if (location.y < 0 || location.y >= img.height) {
reject(
`Query location out of bounds. Should be in range 0 <= y < image.height, is ${location.y}`
);
return;
}
const jimpImage = imageToJimp(img);
const rgba = Jimp.intToRGBA(
jimpImage.getPixelColor(location.x, location.y)
);
resolve(new RGBA(rgba.r, rgba.g, rgba.b, rgba.a));
});
}
}