Replace colors in an image without having to iterate through all pixels

Solved it with this one:

private PImage swapPixelColor(PImage img, int old, int now) {
        old &= ~0x00000000;
        now &= ~0x00000000;

        img.loadPixels();
        int p[] = img.pixels, i = p.length;

        while (i-- != 0) if ((p[i]) == old) p[i] = now;

        img.updatePixels();
        return img;
    }

It works like a charm and it takes almost no time:

Swapped colors in 140ms // That's replacing it three times(different colors ofc)
3 Likes