Jump to content

  • Log In with Google      Sign In   
  • Create Account

Android Bitmap change Hue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Canvas   Members   -  Reputation: 243

Like
1Likes
Like

Posted 13 June 2013 - 05:13 PM

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



Sponsor:

#2 _greyfox()   Members   -  Reputation: 1134

Like
0Likes
Like

Posted 14 June 2013 - 04:50 AM

Hi, I couldn't find it anywhere. And I was kind of bored, so I thought why couldn't I help you? smile.png 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:

360px-HueScale.svg.png

 

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, 14 June 2013 - 04:54 AM.

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”― Nigel Marsh




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS