Farhad
Farhad

Reputation: 752

Fill not-transparent part of image by color

I have one task in Libgdx: Change color of image for example triangle,star, heart and others shapes. All shapes are given in png with transparent background.

I'm doing this with Pixmap, checking every pixel if it is not-transparent fill pixel with needed color. Here is the code:

      for (int y = 0; y < pixmap.getHeight(); y++) {
            for (int x = 0; x < pixmap.getWidth(); x++) {
                Color color = new Color();

                Color.rgba8888ToColor(color, pixmap.getPixel(x, y));

                if(color.r != 1 || color.b != 1 && color.g != 1){
                    pixmap.setColor(setColor);
                    pixmap.fillRectangle(x, y, 1, 1);
                }
            }
        }

Is there any other way to do this? Because method below works too long.

Upvotes: 1

Views: 942

Answers (3)

Tenfour04
Tenfour04

Reputation: 93882

You can certainly speed up the way you're doing it, because right now for every pixel in the image you are instantiating a new Color object and converting the pixel components into separate floats. And then the GC will have to take time to clear up all those Color objects you are generating. Those extra intermediate steps are unnecessary.

Also, you only need to call pixmap.setColor one time (although that is fairly trivial). And you can use drawPixel instead of fillRectangle to more efficiently draw a single pixel.

static final int R = 0xFF000000;
static final int G = 0x00FF0000;
static final int B = 0x0000FF00;

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

            if((pixel & R) != R || (pixel & B) != B && (pixel & G) != G){
                pixmap.drawPixel(x, y);
            }
        }
    }

(By the way, did you mean to check red or blue and green? Seems like odd criteria unless you only want to change the color if the original color is pure yellow, cyan, or white.)

If you are merely drawing the images as Textures, then there is no need to be operating on the Pixmaps like this. You could make your source image white and tint the image when drawing it with SpriteBatch, for example, and this would have no impact on performance.

Upvotes: 1

Reza.Abedini
Reza.Abedini

Reputation: 2257

If you just need to show these pictures with a specific color in your application you can simply do it with setColorFilter

ImageView ivEx = (ImageView) findViewById(R.id.ivEx);
int color = Color.parseColor("your color's code");          
ivEx.setColorFilter(color);

Upvotes: 0

pdegand59
pdegand59

Reputation: 13039

Support library provides utilities to tint drawable.

// create a drawable from the bitmap
BitmapDrawable tintedDrawable = DrawableCompat.wrap(new BitmapDrawable(getResources(), pixmap));

// Apply a Tint, it will color all non-transparent pixel
DrawableCompat.setTint(setColor);

// Draw it back on a bitmap
Bitmap b = Bitmap.createBitmap(pixmap.getWidth(), pixmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
tintedDrawable.setBounds(0, 0, pixmap.getWidth(), pixmap.getHeight());
tintedDrawable.draw(c);

Upvotes: 0

Related Questions