Libgdx change pixel's color

Hey, I’m using Libgdx and I want to take a texture, replace all occurances of a color with another specified color.

This is what I have so far, I’m just not sure on changing the colors (I’m very new to Pixmaps, so I’m sure there are errors everywhere):


// e.g. replace all red pixels with green pixels: replaceColor(Assets.exampleTexture, 0xFF0000, 0x00FF00);
public Texture replaceColor(Texture texture, int c1, int c2) {
		TextureData textureData = texture.getTextureData();
		textureData.prepare();
		Pixmap pixmap = textureData.consumePixmap();
		
		ByteBuffer pixels = pixmap.getPixels();

		while(pixels.hasRemaining()) {
                     if(pixels.getInt() == c1) pixels.putInt(c2);
		}
		
		pixels.rewind();
		
		return new Texture(pixmap);
	}

Thanks. :slight_smile:

You could iterate througth the PixMap using this code:


for (int x=0; x<pixmap.getWidth(); x++) {
    for (int y=0; y<pixmap.getHeight(); y++) {
         int color = pixmap.getPixel(x, y);
    }
}

The int color now holds the RGBA8888 Value of the Pixels color.
To change it simply use:

drawPixel(x, y, newColor)