Skip to content

Commit dcfd6ab

Browse files
Make brightness function behave like other implementations
1 parent 9828a2f commit dcfd6ab

5 files changed

+102
-16
lines changed

plugins/plugin-color/src/__snapshots__/index.test.ts.snap

+56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3+
exports[`brightness > should be able to brighten 1`] = `
4+
Visualization:
5+
6+
6666666666
7+
6666666666
8+
6666666666
9+
6666666666
10+
6666666666
11+
6666666666
12+
6666666666
13+
6666666666
14+
6666666666
15+
6666666666
16+
17+
Data:
18+
19+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
20+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
21+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
22+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
23+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
24+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
25+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
26+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
27+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
28+
FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸ FF-00-00⁸⁸
29+
`;
30+
31+
exports[`brightness > should be able to darken 1`] = `
32+
Visualization:
33+
34+
CCCCCCCCCC
35+
CCCCCCCCCC
36+
CCCCCCCCCC
37+
CCCCCCCCCC
38+
CCCCCCCCCC
39+
CCCCCCCCCC
40+
CCCCCCCCCC
41+
CCCCCCCCCC
42+
CCCCCCCCCC
43+
CCCCCCCCCC
44+
45+
Data:
46+
47+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
48+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
49+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
50+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
51+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
52+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
53+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
54+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
55+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
56+
22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸ 22-00-00⁸⁸
57+
`;
58+
359
exports[`canvas color transformation > brighten 1`] = `
460
Visualization:
561
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { expect, test, describe } from "vitest";
2+
import png from "@jimp/js-png";
3+
import jpeg from "@jimp/js-jpeg";
4+
import { createJimp } from "@jimp/core";
5+
import "@jimp/test-utils/image-snapshot";
6+
7+
import { methods } from "./index.js";
8+
9+
const jimp = createJimp({ formats: [png, jpeg], plugins: [methods] });
10+
11+
describe("Brightness", function () {
12+
test("3x3 box blur matrix using convolute", async () => {
13+
const image = await jimp.read(__dirname + "/images/tiles.jpg");
14+
const output = await image.brightness(2).getBuffer("image/png");
15+
16+
expect(output).toMatchImageSnapshot();
17+
});
18+
});

plugins/plugin-color/src/index.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,27 @@ describe("canvas color transformation", () => {
3737
expect(image.color([{ apply: "hue", params: [150] }])).toMatchSnapshot();
3838
});
3939
});
40+
41+
describe("brightness", () => {
42+
test("brightness of 1 should be the same as no change", () => {
43+
const jgd = makeDonutTestImage(0xff000088, 0xff000088, 0xff000088);
44+
const image = jimp.fromBitmap(jgd);
45+
const image2 = jimp.fromBitmap(jgd);
46+
47+
expect(image.brightness(1).bitmap).toStrictEqual(image2.bitmap);
48+
});
49+
50+
test("should be able to brighten", () => {
51+
const jgd = makeDonutTestImage(0xaa000088, 0xaa000088, 0xaa000088);
52+
const image = jimp.fromBitmap(jgd);
53+
54+
expect(image.brightness(2).bitmap).toMatchSnapshot();
55+
});
56+
57+
test("should be able to darken", () => {
58+
const jgd = makeDonutTestImage(0xaa000088, 0xaa000088, 0xaa000088);
59+
const image = jimp.fromBitmap(jgd);
60+
61+
expect(image.brightness(0.2).bitmap).toMatchSnapshot();
62+
});
63+
});

plugins/plugin-color/src/index.ts

+4-16
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ export const methods = {
336336

337337
/**
338338
* Adjusts the brightness of the image
339-
* @param val the amount to adjust the brightness, a number between -1 and +1
339+
* @param val the amount to adjust the brightness.
340340
* @example
341341
* ```ts
342342
* import { Jimp } from "jimp";
@@ -351,22 +351,10 @@ export const methods = {
351351
throw new Error("val must be numbers");
352352
}
353353

354-
if (val < -1 || val > +1) {
355-
throw new Error("val must be a number between -1 and +1");
356-
}
357-
358354
image.scan((_, __, idx) => {
359-
if (val < 0.0) {
360-
image.bitmap.data[idx]! *= 1 + val;
361-
image.bitmap.data[idx + 1]! *= 1 + val;
362-
image.bitmap.data[idx + 2]! *= 1 + val;
363-
} else {
364-
image.bitmap.data[idx]! += (255 - image.bitmap.data[idx]!) * val;
365-
image.bitmap.data[idx + 1]! +=
366-
(255 - image.bitmap.data[idx + 1]!) * val;
367-
image.bitmap.data[idx + 2]! +=
368-
(255 - image.bitmap.data[idx + 2]!) * val;
369-
}
355+
image.bitmap.data[idx]! = limit255(image.bitmap.data[idx]! * val);
356+
image.bitmap.data[idx + 1]! = limit255(image.bitmap.data[idx + 1]! * val);
357+
image.bitmap.data[idx + 2]! = limit255(image.bitmap.data[idx + 2]! * val);
370358
});
371359

372360
return image;

0 commit comments

Comments
 (0)