Followers 0

# Android Bitmap change Hue

## 1 post in this topic

I have got a Android bitmap and i'm trying to change the HUE of the image, as the image is a red block, I want to change that block to green by just changing the HUE, but I can't seem to find any code anywhere.

Anyone know how I can do this?

Canvas

1

##### Share on other sites

Hi, I couldn't find it anywhere. And I was kind of bored, so I thought why couldn't I help you? So I wrote simple function which does all the job for you.

Please note that this function will not work at real time, well MAYBE it would work if the bitmap would be very small, and your phone super fast. If you would like to do this in real time, then I think you should look into OpenGL. But don't take my word for it, because I didn't use Canvas for android in a LONG time, maybe you can change it in real time.

Here it is:

    /**
* @param bitmap which bitmap to convert
* @param hue any value from 0 to 360.
* @return bitmap with new hue values
*/
public Bitmap setHue(Bitmap bitmap, float hue){
Bitmap newBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
int width = newBitmap.getWidth();
int height = newBitmap.getHeight();
float [] hvs = new float[3];

for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
int pixel = newBitmap.getPixel(x,y);
Color.colorToHSV(pixel,hvs);
hvs[0] = hue;
newBitmap.setPixel(x,y,Color.HSVToColor(Color.alpha(pixel),hvs));
}
}
return newBitmap;
}


Here are the hue values:

Just pass your bitmap, and any value from 0 to 360 in this function, and it will return new, processed bitmap.

Edited by Edvinas Kilbauskas
0

## Create an account

Register a new account