Utilities
intToRGBA()
Convert an RGBA integer such as 0xff0000ff to { r, g, b, a }.
From @jimp/utils
intToRGBA splits a color integer, such as the one getPixelColor returns, into its red, green, blue and alpha channels.
rgbaToInt does the reverse.
Signature
import { intToRGBA } from "jimp";
intToRGBA(i: number): RGBAColor // { r: number; g: number; b: number; a: number }It returns a new RGBAColor object with each channel from 0 to 255.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
i |
number |
required | A color in 0xRRGGBBAA form, from 0 to 0xffffffff. |
Example
import { Jimp, intToRGBA } from "jimp";
intToRGBA(0xff8800ff); // { r: 255, g: 136, b: 0, a: 255 }
const image = await Jimp.read("photo.png");const { r, g, b, a } = intToRGBA(image.getPixelColor(0, 0));Edit a channel and write it back
import { intToRGBA, rgbaToInt } from "jimp";
const { r, g, b } = intToRGBA(image.getPixelColor(5, 5));
image.setPixelColor(rgbaToInt(r, g, b, 128), 5, 5); // same color, half transparentNotes
- The byte order is red, green, blue, alpha, from the highest byte to the lowest.
0x0000ffffis opaque blue, not transparent black. - Non-numbers throw
i must be a number. - Pass unsigned values. A negative number gives a negative
r, for exampleintToRGBA(-1)returns{ r: -1, g: 255, b: 255, a: 255 }. Colors built with JavaScript’s bitwise operators can be negative; convert them with>>> 0first. - Inside
scanyou can read the channels straight fromimage.bitmap.data[idx]to[idx + 3], which avoids creating an object for every pixel. - Changes from v0.
intToRGBAused to be a static method,Jimp.intToRGBA. It’s now a named export ofjimp(and of@jimp/utils).