Utilities
rgbaToInt()
Convert red, green, blue and alpha values to an RGBA integer.
From @jimp/utils
rgbaToInt packs four channel values into the single 0xRRGGBBAA integer that Jimp uses for colors, as taken by setPixelColor, new Jimp({ color }) and many plugin options.
intToRGBA does the reverse.
Signature
import { rgbaToInt } from "jimp";
rgbaToInt(r: number, g: number, b: number, a: number): numberParameters
| Parameter | Type | Default | Description |
|---|---|---|---|
r |
number |
required | Red, from 0 to 255. |
g |
number |
required | Green, from 0 to 255. |
b |
number |
required | Blue, from 0 to 255. |
a |
number |
required | Alpha, from 0 (transparent) to 255 (opaque). |
All four are required: there’s no default alpha.
Example
import { Jimp, rgbaToInt } from "jimp";
rgbaToInt(255, 136, 0, 255); // 0xff8800ff, which is 4287103231
const image = new Jimp({ width: 100, height: 100, color: rgbaToInt(0, 128, 255, 255) });Notes
- The result is always unsigned, from
0to0xffffffff, which is whatsetPixelColorneeds. Building the number yourself with<<and|gives a negative number when red is128or more, unless you finish with>>> 0. - Range checks. A value below
0or above255throws, for exampler must be between 0 and 255. Clamp computed values first withlimit255. - Missing or non-number arguments throw
r, g, b and a must be numbers. - Fractions are truncated, not rounded:
rgbaToInt(1.7, 0, 0, 255)is the same asrgbaToInt(1, 0, 0, 255). UseMath.roundif you need rounding. - Changes from v0.
rgbaToIntused to be a static method,Jimp.rgbaToInt. It’s now a named export ofjimp(and of@jimp/utils).