From d36626b4d5aac54707df72e9acb081c408f5f7d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhan=20So=CC=88nmez?= Date: Tue, 22 Aug 2023 15:39:07 +0200 Subject: [PATCH 1/2] Allow intensity of `sepia` effect to be controlled --- packages/jimp/test/callbacks.test.js | 2 +- packages/plugin-color/README.md | 4 ++-- packages/plugin-color/index.d.ts | 2 +- packages/plugin-color/src/index.js | 13 +++++++++---- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/jimp/test/callbacks.test.js b/packages/jimp/test/callbacks.test.js index f84333a13..a0cf96253 100644 --- a/packages/jimp/test/callbacks.test.js +++ b/packages/jimp/test/callbacks.test.js @@ -77,7 +77,7 @@ describe("Callbacks", () => { }, }, sepia: { - args: [], + args: [1], result: { width: 3, height: 3, diff --git a/packages/plugin-color/README.md b/packages/plugin-color/README.md index 1b4c800fc..72aecc438 100644 --- a/packages/plugin-color/README.md +++ b/packages/plugin-color/README.md @@ -134,7 +134,7 @@ main(); ## sepia -Applies a sepia tone to the image +Applies a sepia tone to the image with given intensity, a factor between 0 and 1 - @param {function(Error, Jimp)} cb (optional) a callback for when complete @@ -144,7 +144,7 @@ import jimp from "jimp"; async function main() { const image = await jimp.read("test/image.png"); - image.sepia(); + image.sepia(1); } main(); diff --git a/packages/plugin-color/index.d.ts b/packages/plugin-color/index.d.ts index bdf6b3f75..4f5bbcf4b 100644 --- a/packages/plugin-color/index.d.ts +++ b/packages/plugin-color/index.d.ts @@ -30,7 +30,7 @@ interface Color { greyscale(cb?: ImageCallback): this; grayscale(cb?: ImageCallback): this; opacity(f: number, cb?: ImageCallback): this; - sepia(cb?: ImageCallback): this; + sepia(f: number, cb?: ImageCallback): this; fade(f: number, cb?: ImageCallback): this; convolution(kernel: number[][], cb?: ImageCallback): this; convolution( diff --git a/packages/plugin-color/src/index.js b/packages/plugin-color/src/index.js index 451bf2ce8..0337c84da 100644 --- a/packages/plugin-color/src/index.js +++ b/packages/plugin-color/src/index.js @@ -321,7 +321,12 @@ export default () => ({ * @param {function(Error, Jimp)} cb (optional) a callback for when complete * @returns {Jimp }this for chaining of methods */ - sepia(cb) { + sepia(f, cb) { + if (typeof f !== "number") + return throwError.call(this, "f must be a number", cb); + if (f < 0 || f > 1) + return throwError.call(this, "f must be a number from 0 to 1", cb); + this.scanQuiet( 0, 0, @@ -332,9 +337,9 @@ export default () => ({ let green = this.bitmap.data[idx + 1]; let blue = this.bitmap.data[idx + 2]; - red = red * 0.393 + green * 0.769 + blue * 0.189; - green = red * 0.349 + green * 0.686 + blue * 0.168; - blue = red * 0.272 + green * 0.534 + blue * 0.131; + red = red * (1 - 0.607 * f) + green * 0.769 * f + blue * 0.189 * f; + green = red * 0.349 * f + green * (1 - 0.314 * f) + blue * 0.168 * f; + blue = red * 0.272 * f + green * 0.534 * f + blue * (1 - 0.869 * f); this.bitmap.data[idx] = red < 255 ? red : 255; this.bitmap.data[idx + 1] = green < 255 ? green : 255; From 9a7df04bc3263b0beed3920426ade03be0b5561d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhan=20So=CC=88nmez?= Date: Tue, 22 Aug 2023 15:48:52 +0200 Subject: [PATCH 2/2] Make `f` value for `sepia` function optional to guarantee backwards-compatibility --- packages/plugin-color/index.d.ts | 2 +- packages/plugin-color/src/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/plugin-color/index.d.ts b/packages/plugin-color/index.d.ts index 4f5bbcf4b..6d5027126 100644 --- a/packages/plugin-color/index.d.ts +++ b/packages/plugin-color/index.d.ts @@ -30,7 +30,7 @@ interface Color { greyscale(cb?: ImageCallback): this; grayscale(cb?: ImageCallback): this; opacity(f: number, cb?: ImageCallback): this; - sepia(f: number, cb?: ImageCallback): this; + sepia(f?: number, cb?: ImageCallback): this; fade(f: number, cb?: ImageCallback): this; convolution(kernel: number[][], cb?: ImageCallback): this; convolution( diff --git a/packages/plugin-color/src/index.js b/packages/plugin-color/src/index.js index 0337c84da..141a291b2 100644 --- a/packages/plugin-color/src/index.js +++ b/packages/plugin-color/src/index.js @@ -321,7 +321,7 @@ export default () => ({ * @param {function(Error, Jimp)} cb (optional) a callback for when complete * @returns {Jimp }this for chaining of methods */ - sepia(f, cb) { + sepia(f = 1, cb) { if (typeof f !== "number") return throwError.call(this, "f must be a number", cb); if (f < 0 || f > 1)