Jota_sk
Jota_sk

Reputation: 169

Converting RGB to HSV

Hi I want convert from rgb to hsv and I have been following the algorithm from easyRGB.com. But doesn't work it show more red than normal. I rewrite the same algorithm a few time and revised, but I can't find the error. Any idea? There is the algorithm.

    public static double[] RGB2HSV(double[] tmp){

    double R = tmp[0] / 255.0;
    double G = tmp[1] / 255.0;
    double B = tmp[2] / 255.0;

    double min = Math.min(Math.min(R, G), B);
    double max = Math.max(Math.max(R, G), B);
    double delta = max - min;

    double H = max;
    double S = max;
    double V = max;

    if(delta == 0){
        H = 0;
        S = 0;
    }else{

        S = delta / max;

        double delR = ( ( ( max - R ) / 6 ) + ( delta / 2 ) ) / delta;
        double delG = ( ( ( max - G ) / 6 ) + ( delta / 2 ) ) / delta;
        double delB = ( ( ( max - B ) / 6 ) + ( delta / 2 ) ) / delta;

        if(R == max){
            H = delB - delG;
        }else if(G == max){
            H = (1/3) + delR - delB;
        }else if(B == max){
            H = (2/3) + delG - delR;
        }

        if(H < 0) H += 1;
        if(H > 1) H -= 1;
    }

    double[] hsv = new double[3];
    hsv[0] = H;
    hsv[1] = S;
    hsv[2] = V;
    return hsv;
}

Upvotes: 1

Views: 1125

Answers (1)

medvedNick
medvedNick

Reputation: 4510

The values of 1/3 and (2/3) are 0, because you are operating with two integers, so the result is the integer too.

Use 1.0 / 3.0 and 2.0 / 3.0 instead.

Upvotes: 2

Related Questions