Skip to content

Commit

Permalink
Make brightness function behave like other implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Sep 2, 2024
1 parent 9828a2f commit bd58915
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 16 deletions.
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.only("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

0 comments on commit bd58915

Please sign in to comment.