A related message has some good ideas, but the mathematical matrix used for ColorFilter can be (a) complex overcrowding and (b) introduce tangible shifts in the resulting colors.
Changing the solution given by janin here - fooobar.com/questions/22585 / ... - I based this version on the Color Blend Mode in Photoshop . It doesn't seem to spoil the image caused by PorterDuff.Mode.Multiply, and works great for tinting shades of unsaturated / artificial black-and-white images without loss of contrast.
public void changeHue (Bitmap bitmap, int hue, int width, int height) {
if (bitmap == null) { return; }
if ((hue < 0) || (hue > 360)) { return; }
int size = width * height;
int[] all_pixels = new int [size];
int top = 0;
int left = 0;
int offset = 0;
int stride = width;
bitmap.getPixels (all_pixels, offset, stride, top, left, width, height);
int pixel = 0;
int alpha = 0;
float[] hsv = new float[3];
for (int i=0; i < size; i++) {
pixel = all_pixels [i];
alpha = Color.alpha (pixel);
Color.colorToHSV (pixel, hsv);
hsv [0] = hue;
hsv [1] = 1.0f;
all_pixels [i] = Color.HSVToColor (alpha, hsv);
}
bitmap.setPixels (all_pixels, offset, stride, top, left, width, height);
}
source
share