Change bitmap color

I am trying to create a function that gets the color of a bitmap and fate and returns a color bitmap (without using paint). I found several ways to do this, but nothing works as I want it.

The closest solution I could find is:

public static Bitmap changeImageColor(Bitmap srcBmp, int dstColor) { int width = srcBmp.getWidth(); int height = srcBmp.getHeight(); float srcHSV[] = new float[3]; float dstHSV[] = new float[3]; Bitmap dstBitmap = Bitmap.createBitmap(width, height, Config.RGB_565); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { Color.colorToHSV(srcBmp.getPixel(col, row), srcHSV); Color.colorToHSV(dstColor, dstHSV); // If it area to be painted set only value of original image dstHSV[2] = srcHSV[2]; // value int color2=Color.HSVToColor(dstHSV);; dstBitmap.setPixel(col, row, Color.HSVToColor(dstHSV)); } } return dstBitmap; } 

but it doesn't work very well on transparent images, as you can see here (before and after): enter image description here

Anyone have other solutions (again without using paint at all)?

+6
source share
3 answers

You just need to extract the alpha and reapply it after the conversion. And use ARGB_8888;

Edited your code to include alpha:

 public Bitmap colorize(Bitmap srcBmp, int dstColor) { int width = srcBmp.getWidth(); int height = srcBmp.getHeight(); float srcHSV[] = new float[3]; float dstHSV[] = new float[3]; Bitmap dstBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int pixel = srcBmp.getPixel(col, row); int alpha = Color.alpha(pixel); Color.colorToHSV(pixel, srcHSV); Color.colorToHSV(dstColor, dstHSV); // If it area to be painted set only value of original image dstHSV[2] = srcHSV[2]; // value dstBitmap.setPixel(col, row, Color.HSVToColor(alpha, dstHSV)); } } return dstBitmap; } 
+4
source

Here is a sample code to change the color for a bitmap:

 private BitmapDrawable getColoredBitmap(int color, Context context, int drawableId) { Bitmap source = BitmapFactory.decodeResource(context.getResources(), drawableId); final Bitmap bitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); for (int i = 0; i < source.getWidth(); i++) { for (int j = 0; j < source.getHeight(); j++) { int pixel = source.getPixel(i, j); // if (pixel == Color.TRANSPARENT) { // // } else if (pixel == Color.WHITE) { pixel = Color.argb(Color.alpha(pixel), Color.red(Color.WHITE), Color.green(Color.WHITE), Color.blue(Color.WHITE)); } else { pixel = Color.argb(Color.alpha(pixel), Color.red(color), Color.green(color), Color.blue(color)); } bitmap.setPixel(i, j, pixel); } } return new BitmapDrawable(context.getResources(), bitmap); } 
+1
source

You do it:

 int alpha=srcBmp.getPixel(col, row); dstBitmap.setPixel(col, row, Color.HSVToColor(dstHSV)); 

in which you compute alpha (possibly incorrectly from the look of this code) and then don't use it. You may have to create a color using HSVToColor, then set the alpha of that color and then use it in setPixel. And you probably have to get alpha the same way, because I find it hard to believe that the getPixel function returns alpha: p

0
source

Source: https://habr.com/ru/post/981211/


All Articles