Achilles
Achilles

Reputation: 1065

Changing specific colors in a bitmap , Android

Suppose i have 3 BITMAPS with 2 colors on them.

  1. Red and white
  2. Blue and white
  3. Green and white

Now I want to change only the red , blue and green colours in the 3 images respectively with any colour. ex :Black.

What approach do I take ?

I read about replacing colours and i was successful in replacing particular colours. Ex: I was able to replace red , blue and green individually by specifying the colour that I want to change.

But how do I make it generic ? Can you suggest any approach towards it ?

Upvotes: 0

Views: 447

Answers (1)

TeePaps
TeePaps

Reputation: 471

You can use the approach here: Replace black color in bitmap with red and just add more tests inside the if statement.

int [] allpixels = new int [ myBitmap.getHeight()*myBitmap.getWidth()];

myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0,myBitmap.getWidth(),myBitmap.getHeight());

for(int i =0; i<myBitmap.getHeight()*myBitmap.getWidth();i++){

    if( allpixels[i] == Color.RED || allpixels[i] == Color.BLUE || allpixels[i] == Color.GREEN)
         allpixels[i] = Color.BLACK;
}

myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

Then you can make this into a function which takes an array of colors as an argument.

Upvotes: 3

Related Questions