Convert from RGB TO HSB in java

user2080806 picture user2080806 · Feb 17, 2013 · Viewed 8.2k times · Source

I am trying to find the average colour in a picture, and convert the colours to HSB.

I am using a bufferedImage and the list function, and I have found the colours red, green and blue in each pixel.

Can anyone give me some tips on how to convert to HSB in java? :)

Answer

StatsTrade picture StatsTrade · Feb 17, 2013

Use java.awt.Color.RGBtoHSB(int r, int g, int b, float[] hsbvals). See javadoc here.

P.S. If you use p = bufferedImage.getRGB(x,y), recall that the lowest 8 bits (myPixel&0xff) correspond to the blue component, so you may use something like: Color.RGBtoHSB((p>>16)&0xff, (p>>8)&0xff, p&0xff, hsbvals).