2014-10-08 55 views
2

我想写一个从颜色rgb转换为hsv的代码,但编译器似乎没有看到里面有什么如果合并 这里的代码 package Problem2;在HSV中输入一个颜色代码并输出等效RGB代码的Java程序,反之亦然

public class HSVRGB { 

    double d; 
    double e; 
    double g; 

//Java program that takes an input a color code in HSV and 

输出等效的RGB代码,反之亦然。

public static void convert(double d,double e,double g){ /// my hsv inputs that am gonna check 

    double t; 
    double p ; 
    double q; 
    double f; 
    double Hi; 
    //my other variable am gonna use to produce the output 

    Hi=(d/60); 
    f=(d/60)-Hi;// here's the formula that will proceed the code 




    if(Hi==0) { 
     t=g*(1-(1-f)*e); 
     p=g*(1-e); 
     System.out.print("R="+ g + "G=" + t + "b=" + p); 
     // the given input should pass into the above if condition since its true 



    } 
    else 
     if(Hi==1){ 
      q=g*(1-f*e); 
      p=1-e; 
      System.out.print("R="+q+"G="+g+"b="+p); 



     } 
     else 
      if(Hi==2){ 
       p=g*(1-e); 
       t=g*(1-(1-f)*e); 
       System.out.print("R="+p + "G="+ g + "B="+t); 
      } 
      else 
       if(Hi==3){ 
        p=g*(1-e); 
        q=g*(1-(f*e)); 
        System.out.print("R="+p + "G="+q + "B=" + g); 


       } 
       else 
        if(Hi==4){ 
         t=g*(1-(1-f)*e); 
         p=g*(1-e); 
         System.out.print("R"+t+"G"+p+"B"+g); 

        } 
        else 
         if(Hi==5){ 
          p=g*(1-e); 
          q=g*(1-(f*e)); 
          System.out.print("R="+g+ " G"+p+"B="+q); 


         } 
         else 
          System.out.print("Invalid Inpjdsnabfvkdbfjsv,hdbut"); //it only print this message 


} 


public static void main (String[]args){ 
    convert(100.34,0.74,0.78); 

    } 
} 
+1

为什么不使用'Color.getHSBColor范围()'与RGB组件? – trashgod 2014-10-08 01:37:41

回答

0

这里

convert(100.34,0.74,0.78); 

可变d的值将是可变的Hi

Hi=(d/60); 

值之后100.34将是1.6723333333333334

在你的声明,如果您有例如

else 
     if(Hi==1){ 

你觉得变量Hi这是1.6723333333333334去的价值等于1

建议:我真的不知道为什么你尝试比较双为int,但你可以做的铸造或可尝试比较像这样与之间的