Skip to content

Commit

Permalink
Make brightness function behave like other implementations (#1312)
Browse files Browse the repository at this point in the history
* Make brightness function behave like other implementations

* fix test
  • Loading branch information
hipstersmoothie authored Sep 2, 2024
1 parent 5c836c4 commit a425b4c
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 25 deletions.
12 changes: 6 additions & 6 deletions packages/jimp/src/__snapshots__/callbacks.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ AA-94-3Fᶠ¹ 7E-7E-7Eᶠ¹ 54-69-BFᶠ¹
exports[`Callbacks > with brightness 1`] = `
Visualization:
▫▾4
2 A
D▦◆
▴▸▾
◆▪▰
▵▹▿
Data:
FF-7F-7Fᶠᶠ 7F-FF-7Fᶠᶠ 7F-7F-FFᶠᶠ
FF-FF-7Fᶠᶠ 7F-FF-FFᶠᶠ FF-7F-FFᶠᶠ
FF-7F-7F⁷ᶠ 7F-FF-7F⁷ᶠ 7F-7F-FF⁷ᶠ
FF-00-00ᶠᶠ 00-FF-00ᶠᶠ 00-00-FFᶠᶠ
FF-FF-00ᶠᶠ 00-FF-FFᶠᶠ FF-00-FFᶠᶠ
FF-00-00⁷ᶠ 00-FF-00⁷ᶠ 00-00-FF⁷ᶠ
`;

exports[`Callbacks > with composite 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion packages/jimp/src/callbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("Callbacks", () => {
opacity: { args: [0.5] },
resize: { args: [{ w: 2, h: 2 } as ResizeOptions] },
scale: { args: [0.5] },
brightness: { args: [0.5] },
brightness: { args: [1.5] },
contrast: { args: [0.75] },
posterize: { args: [5] },
dither: { args: [] },
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions plugins/plugin-color/src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`brightness > should be able to brighten 1`] = `
Visualization:
6666666666
6666666666
6666666666
6666666666
6666666666
6666666666
6666666666
6666666666
6666666666
6666666666
Data:
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
`;

exports[`brightness > should be able to darken 1`] = `
Visualization:
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
CCCCCCCCCC
Data:
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
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⁸⁸
`;

exports[`canvas color transformation > brighten 1`] = `
Visualization:
Expand Down
18 changes: 18 additions & 0 deletions plugins/plugin-color/src/birghtness.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { expect, test, describe } from "vitest";
import png from "@jimp/js-png";
import jpeg from "@jimp/js-jpeg";
import { createJimp } from "@jimp/core";
import "@jimp/test-utils/image-snapshot";

import { methods } from "./index.js";

const jimp = createJimp({ formats: [png, jpeg], plugins: [methods] });

describe("Brightness", function () {
test("3x3 box blur matrix using convolute", async () => {
const image = await jimp.read(__dirname + "/images/tiles.jpg");
const output = await image.brightness(2).getBuffer("image/png");

expect(output).toMatchImageSnapshot();
});
});
24 changes: 24 additions & 0 deletions plugins/plugin-color/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,27 @@ describe("canvas color transformation", () => {
expect(image.color([{ apply: "hue", params: [150] }])).toMatchSnapshot();
});
});

describe("brightness", () => {
test("brightness of 1 should be the same as no change", () => {
const jgd = makeDonutTestImage(0xff000088, 0xff000088, 0xff000088);
const image = jimp.fromBitmap(jgd);
const image2 = jimp.fromBitmap(jgd);

expect(image.brightness(1).bitmap).toStrictEqual(image2.bitmap);
});

test("should be able to brighten", () => {
const jgd = makeDonutTestImage(0xaa000088, 0xaa000088, 0xaa000088);
const image = jimp.fromBitmap(jgd);

expect(image.brightness(2).bitmap).toMatchSnapshot();
});

test("should be able to darken", () => {
const jgd = makeDonutTestImage(0xaa000088, 0xaa000088, 0xaa000088);
const image = jimp.fromBitmap(jgd);

expect(image.brightness(0.2).bitmap).toMatchSnapshot();
});
});
20 changes: 4 additions & 16 deletions plugins/plugin-color/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export const methods = {

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

if (val < -1 || val > +1) {
throw new Error("val must be a number between -1 and +1");
}

image.scan((_, __, idx) => {
if (val < 0.0) {
image.bitmap.data[idx]! *= 1 + val;
image.bitmap.data[idx + 1]! *= 1 + val;
image.bitmap.data[idx + 2]! *= 1 + val;
} else {
image.bitmap.data[idx]! += (255 - image.bitmap.data[idx]!) * val;
image.bitmap.data[idx + 1]! +=
(255 - image.bitmap.data[idx + 1]!) * val;
image.bitmap.data[idx + 2]! +=
(255 - image.bitmap.data[idx + 2]!) * val;
}
image.bitmap.data[idx]! = limit255(image.bitmap.data[idx]! * val);
image.bitmap.data[idx + 1]! = limit255(image.bitmap.data[idx + 1]! * val);
image.bitmap.data[idx + 2]! = limit255(image.bitmap.data[idx + 2]! * val);
});

return image;
Expand Down
4 changes: 2 additions & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"dependsOn": ["^lint"]
},
"test": {
"dependsOn": ["^test"]
"dependsOn": ["^build", "^test"]
},
"test:browser": {
"dependsOn": ["^test:browser"]
"dependsOn": ["^build", "^test:browser"]
},
"dev": {
"cache": false,
Expand Down

0 comments on commit a425b4c

Please sign in to comment.