color()
Apply color actions such as hue, saturate, tint, mix and multiply.
From @jimp/plugin-color
color applies a list of color adjustments, called actions, to every pixel: rotate the hue, saturate, lighten, tint, mix in a color, multiply the channels and more.
Use it when you need a color change that the single-purpose methods such as brightness or sepia don’t cover, or to apply several adjustments in one pass.
Signature
image.color(actions: ColorAction[]): this
type ColorAction = { apply: string; params?: [...] }; // see the table belowcolor changes the image in place and returns it, so you can chain more calls after it.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
actions |
ColorAction[] |
required | The actions to apply, in order. Each one is { apply, params }. An empty array changes nothing. |
Actions
apply names the action and params is a tuple of its arguments.
Where the default column shows a value, params can be left out.
apply |
params |
Range | Default | Effect |
|---|---|---|---|---|
hue |
[degrees] |
-360 to 360 |
required | Rotate the hue around the color wheel. 120 turns red into green. |
spin |
[degrees] |
-360 to 360 |
required | Same as hue. |
saturate |
[amount] |
0 to 100 |
10 |
Multiply the HSL saturation by 1 + amount / 100, up to full saturation. Grays stay gray. |
desaturate |
[amount] |
0 to 100 |
10 |
Multiply the HSL saturation by 1 - amount / 100. 100 removes all color. |
greyscale |
none ([]) |
Remove all color. Gives the same result as greyscale(). |
||
lighten |
[amount] |
0 to 100 |
10 |
Blend amount percent toward white. |
darken |
[amount] |
0 to 100 |
10 |
Blend amount percent toward black. |
brighten |
[amount] |
0 to 100 |
10 |
Add amount percent of 255 to each channel, so 20 adds 51. |
tint |
[amount] |
0 to 100 |
50 |
Blend amount percent toward white. |
shade |
[amount] |
0 to 100 |
50 |
Blend amount percent toward black. |
mix |
[color, amount?] |
a color, 0 to 100 |
amount 50 |
Blend amount percent toward color. |
multiply |
[r, g, b] or [color] |
any numbers, or a color | required | Multiply each channel by its factor, or by the matching channel of color divided by 255. |
xor |
[color] |
a color | required | Bitwise XOR each channel with color. XOR with white inverts the image. |
red |
[amount] |
-255 to 255 |
required | Add amount to the red channel. |
green |
[amount] |
-255 to 255 |
required | Add amount to the green channel. |
blue |
[amount] |
-255 to 255 |
required | Add amount to the blue channel. |
color in mix, multiply and xor is either an { r, g, b } object with each channel from 0 to 255, such as { r: 0, g: 102, b: 221 }, or a CSS color string, such as "#06D" or "tomato".
Results are clamped to 0–255.
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
image.color([ { apply: "hue", params: [-90] }, { apply: "lighten", params: [20] }, { apply: "xor", params: ["#06D"] },]);
await image.write("photo-color.jpg");Combine several actions
Actions run in the order you list them, one after the other on each pixel.
Putting several in one call gives the same result as calling color once per action (give or take rounding), but the image is only scanned once.
// A faded, warm "vintage" lookimage.color([ { apply: "desaturate", params: [40] }, { apply: "mix", params: [{ r: 255, g: 170, b: 90 }, 20] }, { apply: "lighten", params: [10] },]);Order matters when actions interact. Mixing in a color and then desaturating loses some of the color you mixed in; desaturating first keeps it.
// Duotone: remove the color, then tint everything toward one hueimage.color([ { apply: "greyscale" }, { apply: "mix", params: ["#2858c8", 45] },]);Adjust the channels with multiply
multiply takes either three factors for red, green and blue, or one color.
Factors can brighten as well as darken: this warms an image by raising red and lowering blue.
image.color([{ apply: "multiply", params: [1.2, 1.05, 0.9] }]);A color works like the multiply blend mode: each channel is multiplied by the color’s channel divided by 255, so white leaves the image unchanged and every other color darkens it.
image.color([{ apply: "multiply", params: ["#ffe0c0"] }]);
// the same color as an objectimage.color([{ apply: "multiply", params: [{ r: 255, g: 224, b: 192 }] }]);Recreate common filter steps
Photo filter recipes are often built from a few standard steps. A color filter (c - (c - color) × amount) is mix with amount × 100, a per-channel adjustment is multiply with factors, and a partial sepia is sepia(amount):
image .color([ { apply: "multiply", params: [1.1, 1, 0.9] }, { apply: "mix", params: [{ r: 255, g: 153, b: 0 }, 20] }, ]) .sepia(0.3);Use constants for the action names
ColorActionName holds every action name, for example ColorActionName.MULTIPLY is "multiply".
Plain strings work just as well.
import { Jimp, ColorActionName } from "jimp";
const image = await Jimp.read("photo.jpg");
image.color([ { apply: ColorActionName.SATURATE, params: [20] }, { apply: ColorActionName.MULTIPLY, params: [1, 1, 0.9] },]);Build actions in TypeScript
The ColorAction type is exported from jimp, so you can build an action list ahead of time and get type checking on apply and params:
import { Jimp, type ColorAction } from "jimp";
const warm: ColorAction[] = [ { apply: "saturate", params: [20] }, { apply: "red", params: [15] }, { apply: "blue", params: [-15] },];
const image = await Jimp.read("photo.jpg");image.color(warm);color doesn’t change the array you pass, so the same list can be reused for many images.
Try it
Pick an action and move its slider. The code panel shows the action you built.
image.color([{ apply: "hue", params: [90] }])Apply one color action. mix and xor take a CSS color; multiply scales red, green and blue by three factors.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
This one builds a single call with several actions. Set a slider to 0 to leave its action out.
image.color([{ apply: "hue", params: [40] }, { apply: "saturate", params: [40] }, { apply: "mix", params: ["#ff8f1f", 20] }])Docs →Apply several color actions in one color() call. Actions run in order; a slider at 0 leaves its action out.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Click a method above to add it here await image.write("output.png");
Notes
- Alpha is unchanged. Every action works on red, green and blue only. To change transparency use
opacity,fadeoropaque. - Validation comes first. Every action is checked before any pixel changes, so an invalid action leaves the image untouched. Out-of-range values, a missing required
paramsor an unknownapplythrow aZodError. A CSS string that isn’t a color throwsInvalid color "notacolor". Passing something other than an array throwsactions must be an array. - CSS colors and color spaces. A CSS color string is sRGB, as in CSS, and is converted to the image’s color space before it’s used, so
"#ff0000"is a slightly different value in a Display P3 image. An{ r, g, b }object is used as raw pixel values. The alpha of a CSS color is ignored. See Colors and pixels. multiplyfactors. Any number is accepted. Results are rounded and clamped, so a negative factor gives0.greyscaleversusdesaturatewith100. Thegreyscaleaction uses Rec. 709 luminance, likegreyscale().desaturatewith100sets each pixel to its HSL lightness (the average of the largest and smallest channel) instead: for an orange(200, 100, 50)the action gives118anddesaturategives125.lightenandtintblend the same way. Both move each channel toward white (anddarkenandshadetoward black).lightenanddarkenround the result;tintandshadedrop the fraction. The defaults differ too:10forlighten/darken,50fortint/shade.lightenversusbrighten.lightenmoves each channel a percentage of the way to white, so light areas change less than dark ones.brightenadds the same amount to every channel, which clips highlights sooner.saturatescales.saturateanddesaturatemultiply the saturation rather than adding to it, so muted colors stay muted and grays are never tinted. For a boost that favors muted colors, usevibrance.- Added in 2.0: the
multiplyaction andColorActionName.MULTIPLY. - Changed in 2.0:
mixandxoraccept CSS color strings; before, they only took{ r, g, b }objects. - Changed in 2.0: the
greyscaleaction gives the same result asgreyscale(); before, it used HSL lightness. - Changed in 2.0:
saturateanddesaturatescale the saturation, andlightenanddarkenblend toward white and black; before, all four added or subtracted a fixed amount in HSL, which tinted grays and turned near-white pixels vivid when darkened. - Changed in 2.0:
ColorActionNameis exported fromjimp; before, it was only exported by@jimp/plugin-color.