将RGB转换为HSV

时间:2015-11-28 18:40:02

标签: java rgb hsv

您好我想从rgb转换为hsv,我一直在关注easyRGB.com的算法。但是没有工作它显示出比正常情况更多的红色。我重写相同的算法几次并修改,但我找不到错误。任何的想法?有算法。

    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;
}

1 个答案:

答案 0 :(得分:2)

1/3(2/3)的值为0,因为您使用两个整数运算,因此结果也是整数。

改为使用1.0 / 3.02.0 / 3.0