diff --git a/packages/jimp/src/__snapshots__/callbacks.test.ts.snap b/packages/jimp/src/__snapshots__/callbacks.test.ts.snap index 5d5df786..4d49dc97 100644 --- a/packages/jimp/src/__snapshots__/callbacks.test.ts.snap +++ b/packages/jimp/src/__snapshots__/callbacks.test.ts.snap @@ -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`] = ` diff --git a/packages/jimp/src/callbacks.test.ts b/packages/jimp/src/callbacks.test.ts index 0f4f5c8b..3fb18887 100644 --- a/packages/jimp/src/callbacks.test.ts +++ b/packages/jimp/src/callbacks.test.ts @@ -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: [] }, diff --git a/plugins/plugin-color/src/__image_snapshots__/birghtness-node-test-ts-src-birghtness-node-test-ts-brightness-3-x-3-box-blur-matrix-using-convolute-1-snap.png b/plugins/plugin-color/src/__image_snapshots__/birghtness-node-test-ts-src-birghtness-node-test-ts-brightness-3-x-3-box-blur-matrix-using-convolute-1-snap.png new file mode 100644 index 00000000..01defca2 Binary files /dev/null and b/plugins/plugin-color/src/__image_snapshots__/birghtness-node-test-ts-src-birghtness-node-test-ts-brightness-3-x-3-box-blur-matrix-using-convolute-1-snap.png differ diff --git a/plugins/plugin-color/src/__snapshots__/index.test.ts.snap b/plugins/plugin-color/src/__snapshots__/index.test.ts.snap index 95a632af..8daf853a 100644 --- a/plugins/plugin-color/src/__snapshots__/index.test.ts.snap +++ b/plugins/plugin-color/src/__snapshots__/index.test.ts.snap @@ -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: diff --git a/plugins/plugin-color/src/birghtness.node.test.ts b/plugins/plugin-color/src/birghtness.node.test.ts new file mode 100644 index 00000000..08c0d7a3 --- /dev/null +++ b/plugins/plugin-color/src/birghtness.node.test.ts @@ -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(); + }); +}); diff --git a/plugins/plugin-color/src/index.test.ts b/plugins/plugin-color/src/index.test.ts index 70ea200c..2552beae 100644 --- a/plugins/plugin-color/src/index.test.ts +++ b/plugins/plugin-color/src/index.test.ts @@ -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(); + }); +}); diff --git a/plugins/plugin-color/src/index.ts b/plugins/plugin-color/src/index.ts index 6ba8ef45..eba72ace 100644 --- a/plugins/plugin-color/src/index.ts +++ b/plugins/plugin-color/src/index.ts @@ -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"; @@ -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; diff --git a/turbo.json b/turbo.json index 40137713..7f78cdfe 100644 --- a/turbo.json +++ b/turbo.json @@ -13,10 +13,10 @@ "dependsOn": ["^lint"] }, "test": { - "dependsOn": ["^test"] + "dependsOn": ["^build", "^test"] }, "test:browser": { - "dependsOn": ["^test:browser"] + "dependsOn": ["^build", "^test:browser"] }, "dev": { "cache": false,