Color Spaces
How Jimp handles Display P3, sRGB and ICC color profiles.
A pixel’s red, green and blue values only describe a color once you know the color space they’re in. Most images are sRGB, which is also what viewers assume when an image doesn’t say. iPhones and many other cameras save photos in Display P3, which can show more saturated reds and greens, and embed a Display P3 ICC profile so viewers know.
Jimp keeps every image in the color space it was read in, and converts colors only where they enter or leave the image. Images without a profile, which is most of them, are read and written exactly as before.
Reading and writing
When Jimp.read or Jimp.fromBuffer loads a JPEG, PNG or WebP with an ICC profile, Jimp keeps the profile, and the pixels stay as they are.
JPEG, PNG and WebP output gets the same profile, byte for byte, so the image looks the same as the original.
import { Jimp } from "jimp";
const image = await Jimp.read("iphone-photo.jpg");
image.colorSpace; // "display-p3"
image.resize({ w: 2000 });
await image.write("small.jpg"); // still Display P3await image.write("small.png"); // also Display P3image.colorSpace is:
"srgb"for images without a profile, or with an sRGB one"display-p3"for images with a Display P3 profile, from any vendor"custom"for any other profile, such as Adobe RGB
clone keeps the color space.
JPEG 2000 files from @jimp/wasm-jpeg2000 have their profile read too.
Formats without a profile
GIF, BMP, TIFF, ICO, AVIF and JPEG 2000 output can’t carry a profile in Jimp, so for those Jimp writes a copy of the image converted to sRGB. The image itself isn’t changed.
To write sRGB without a profile in a JPEG, PNG or WebP, pass icc: false to write or getBuffer.
Jimp converts the colors first, so the output still looks right in every viewer.
Display P3 colors more saturated than sRGB can show are clipped.
await image.write("small.jpg", { icc: false });// orconst buffer = await image.getBuffer("image/png", { icc: false });Converting
image.toColorSpace() converts the pixels and changes the profile to match:
const image = await Jimp.read("iphone-photo.jpg");
image.toColorSpace("srgb"); // for example, for image recognition that expects sRGB
image.colorSpace; // "srgb"image.iccProfile; // undefinedYou can convert to "srgb" or "display-p3", from either of them or from any "custom" RGB profile made of primaries and tone curves, which covers Adobe RGB, ProPhoto RGB and Rec. 2020.
Jimp can’t convert from profiles built on lookup tables, or from CMYK, and throws if you try.
Converting to a smaller space clips the colors it can’t show.
Creating Display P3 images
Pass colorSpace when creating an image with new Jimp():
const image = new Jimp({ width: 256, height: 256, color: "color(display-p3 1 0 0)", // the most saturated red Display P3 can show colorSpace: "display-p3",});
await image.write("red.png"); // with a Display P3 profileJimp.fromBitmap takes colorSpace too, for pixel data that’s already in Display P3.
Colors
CSS color strings mean the same as in a browser:
#ff0000,rgb(),hsl()and named colors are sRGB. On a Display P3 image Jimp converts them, so#ff0000becomesrgb(234, 51, 35)in Display P3, the same red you’d see in a browser.color(display-p3 r g b)is a Display P3 color, with channels from 0 to 1 (or percentages) and an optional/ alpha. On an sRGB image it’s converted, and clipped if sRGB can’t show it.color(srgb r g b)is an sRGB color, like#rrggbb.
This applies to the color option of new Jimp(), to border() and boxShadow() colors, and to CSS colors in color()’s mix and xor actions.
cssColorToHex does the same conversion when you pass it a color space.
Numbers such as 0xff0000ff are pixel values, so they’re used as they are, in the image’s own color space.
The same goes for setPixelColor, getPixelColor, the background color and the { r, g, b } colors of color()’s mix and xor.
On a Display P3 image 0xff0000ff is Display P3’s most saturated red.
Drawing one image onto another
composite() and blit() convert the source image to the color space of the image you draw onto, so an sRGB logo keeps its colors on a Display P3 photo.
The source image itself isn’t changed.
const photo = await Jimp.read("iphone-photo.jpg"); // Display P3const logo = await Jimp.read("logo.png"); // sRGB
photo.composite(logo, 10, 10);Pixel operations
Operations such as resize(), blur(), brightness(), greyscale() and the color() adjustments work on the pixel values in the image’s own color space, the same way they work on sRGB values.
White, black and grays are the same in sRGB and Display P3, so greyscale(), tint() and shade() give the same colors either way.
Hue and saturation changes are measured in the image’s own space, so they’re a little different from the same change on the sRGB version of the image.
Removing the profile without converting
image.iccProfile holds the profile bytes.
Setting it to undefined makes Jimp treat the pixel values as sRGB without converting them, which is what Jimp did with every image before 2.0:
image.iccProfile = undefined;
image.colorSpace; // "srgb", with the same pixel valuesSetting it to another profile’s bytes re-tags the image the same way.
See also
toColorSpace: convert the pixels to sRGB or Display P3getBufferoptions: theiccoption of JPEG, PNG and WebP- Formats: which formats read and write profiles
- Colors and pixels: pixel values and color helpers