tags:

views:

38

answers:

2

According to the document, android.graphics.Color has a method called RGBToHSV which can convert RGB values to HSV, this is what the document tells me:

public static void RGBToHSV (int red, int green, int blue, float[] hsv)

Convert RGB components to HSV.

  • hsv[0] is Hue [0 .. 360)
  • hsv[1] is Saturation [0...1]
  • hsv[2] is Value [0...1]

Parameters

  • red: red component value [0..255]
  • green: green component value [0..255]
  • blue: blue component value [0..255]
  • hsv: 3 element array which holds the resulting HSV components.

But when I write a program to test it, it doesn't work any way.

float[] hsv = new float[3];

RGBToHSV(255, 255, 0, hsv);

Log.i("HSV_H", "" + hsv[0]);   // always output 0.0

Is it a bug ?

A: 

There is no indication that RGBtoHSV is assigning any values to the hsv array; try something like this:

RGBtoHSV(255, 255, 0, hsv);

And then check the value of hsv[0].

Chris Hutchinson
public static **void** RGBToHSV() I think this method will return nothing
wong2
Check my answer again; I failed to read the method definition when I posted my answer.
Chris Hutchinson
+3  A: 

What are your expected values? To me it seems to be working.

The code I used:

float[] hsv = new float[3];
android.graphics.Color.RGBToHSV(255, 255, 0, hsv);
Log.i("HSV_H", "Hue=" + hsv[0]);
Log.i("HSV_H", "Saturation=" + hsv[1]);
Log.i("HSV_H", "Value=" + hsv[2]);

The results:

Hue=60.0
Saturation=1.0
Value=1.0

This was run using a project targeting Android SDK 1.6 (API level 4) on a 1.6 emulator.

Marc Bernstein
Thanks, it works well
wong2