floodFill works like the paint bucket in an image editor: it starts at a pixel and fills every pixel connected to it that has the same color, or a similar one.
Use it to recolor a background, fill a shape you drew with outlines, or color in a region of a diagram.
Signature
image.floodFill(options: { x: number; y: number; color: Paint; tolerance?: number;}): thisfloodFill draws on the image in place and returns it, so you can chain more calls after it.
Paint is a color or a gradient; see Colors and gradients. The options type is exported from jimp as FloodFillOptions.
Options
| Option | Type | Default | Description |
|---|---|---|---|
x |
number |
required | The x of the pixel to start from. Fractions are rounded down. |
y |
number |
required | The y of the pixel to start from. Fractions are rounded down. |
color |
Paint |
required | What to fill with: a color or a gradient. |
tolerance |
number |
0 |
How different, from 0 to 1, a pixel can be from the starting pixel and still be filled. 0 fills only the exact color; 1 fills everything connected. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("logo.png");
// Replace the background, starting from the top-left pixelimage.floodFill({ x: 0, y: 0, color: "#ffcc00", tolerance: 0.1 });
await image.write("logo-yellow.png");Color in a shape
Draw an outline, then fill its inside by starting anywhere within it:
const image = new Jimp({ width: 120, height: 120, color: "white" });
image .drawEllipse({ cx: 60, cy: 60, rx: 50, ry: 50, stroke: "black", strokeWidth: 3 }) .floodFill({ x: 60, y: 60, color: "#5cc8ff", tolerance: 0.5 });The tolerance takes in the antialiased pixels just inside the outline, so the fill meets the line without a light fringe.
A gradient fill
color can be a gradient. Its coordinates are in image pixels:
image.floodFill({ x: 60, y: 60, tolerance: 0.5, color: { type: "radial", cx: 60, cy: 60, r: 50, stops: [ { offset: 0, color: "white" }, { offset: 1, color: "#3a7bff" }, ], },});Try it
Pick the starting point on the image and raise tolerance to take in more of the edges. The default point is on the transparent background.
image.floodFill({ x: 11, y: 8, color: "#ffd83a", tolerance: 0.2 })Fill the area connected to a point, like a paint bucket. Raise tolerance to take in similar colors.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- Four directions. A pixel is connected to the pixels above, below, left and right of it, not diagonally, so a fill doesn’t leak through a 1-pixel diagonal line.
- How tolerance is measured. Two colors match when every channel (red, green, blue and alpha) differs by at most
tolerance × 255. Red, green and blue are multiplied by alpha first, so every fully transparent pixel matches every other, whatever color is hidden in it. - The fill is drawn over the pixels. Each matched pixel is blended with the fill color like any other drawing, so a semi-transparent color tints what’s there instead of replacing it. Pixels that don’t match aren’t touched.
- Edges aren’t antialiased. A pixel is either filled or not. Raise
toleranceto take in the soft edge of an antialiased outline. - A start outside the image fills nothing and doesn’t throw.
- Fast on large images. It’s a scanline fill with an explicit stack instead of recursion, so it can fill a whole 1920 × 1080 image without running out of stack, in tens of milliseconds.
- Validation.
xandymust be finite numbers andtolerancemust be from0to1; anything else throws aZodErrornaming the option. An invalid CSS color throwsInvalid color "notacolor". - Added in 2.0.