Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert RGB to HSV in android

I want to get the ARGB values from a pixel and convert to HSV and then set the pixel with the new values.

I don't fully understand how to do that. Can anyone help me?

like image 446
Madalin Avatar asked Mar 06 '15 06:03

Madalin


People also ask

Can RGB be converted to HSV?

You can convert all 16,777,216 possible RGB colors to HSV and back again to RGB. Unfortunately, using this algorithm you will find that some colors will not roundtrip well.

How do you convert RGB to HSV manually?

Converting RGB to HSVH = 360 - cos-1[ (R - ½G - ½B)/√R² + G² + B² - RG - RB - GB ] if B > G. Inverse cosine is calculated in degrees.

Why RGB is converted to HSV?

R, G, B in RGB are all co-related to the color luminance( what we loosely call intensity),i.e., We cannot separate color information from luminance. HSV or Hue Saturation Value is used to separate image luminance from color information. This makes it easier when we are working on or need luminance of the image/frame.


1 Answers

Let's say you have a Bitmap object and x and y co-ordinates. You can get the color from the bitmap as a 32-bit value like this:

int color = bitmap.getPixel(x, y);

You can separate out the argb components like this:

int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);

Then you can convert to HSV like this:

float[] hsv = new float[3];
Color.RGBToHSV(r, g, b, hsv);

Now you can manipulate the HSV values however you want. When you are done you can convert back to rgb:

color = Color.HSVToRGB(hsv);

or like this is you want to use the alpha value:

color = Color.HSVToRGB(a, hsv);

Then you can write the color back to the bitmap (it has to be a mutable Bitmap):

bitmap.setPixel(x, y, color);
like image 114
samgak Avatar answered Sep 30 '22 11:09

samgak