Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional parameter to sepia effect to control its intensity #1256

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/jimp/test/callbacks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("Callbacks", () => {
},
},
sepia: {
args: [],
args: [1],
result: {
width: 3,
height: 3,
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-color/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-color/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface Color {
greyscale(cb?: ImageCallback<this>): this;
grayscale(cb?: ImageCallback<this>): this;
opacity(f: number, cb?: ImageCallback<this>): this;
sepia(cb?: ImageCallback<this>): this;
sepia(f?: number, cb?: ImageCallback<this>): this;
fade(f: number, cb?: ImageCallback<this>): this;
convolution(kernel: number[][], cb?: ImageCallback<this>): this;
convolution<T>(
Expand Down
13 changes: 9 additions & 4 deletions packages/plugin-color/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 1, 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,
Expand All @@ -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;
Expand Down