使用公式将RGB转换为HSV

时间:2014-11-10 21:23:00

标签: java

我被要求编写一个基于java的程序将颜色从RGB转换为HSV,但我不允许使用java.awt.Color,到目前为止我的努力让我达到这个水平,我不知道如何要使控制台读取其他方法,它只是读取主要方法..

public class colorconversion {


public static void main(String[] args) {

 System.out.println("nope");
}

void RGBtoHSV( float r, float g, float b, float h, float s, float v )
{
r=Input.readFloat();
float min, max, delta;
min = MIN( r, g, b );
max = MAX( r, g, b );
v = max;                // v
delta = max - min;
if( max != 0 )
    s = delta / max;        // s
else {
    // r = g = b = 0        // s = 0, v is undefined
    s = 0;
    h = -1;
    return;
}
if( r == max )
    h = ( g - b ) / delta;      // between yellow & magenta
else if( g == max )
    h = 2 + ( b - r ) / delta;  // between cyan & yellow
else
    h = 4 + ( r - g ) / delta;  // between magenta & cyan
h = 60;             // degrees
if( h < 0 )
    h += 360;
}
void HSVtoRGB( float r, float g, float b, float h, float s, float v )
{
int i;
float f, p, q, t;
if( s == 0 ) {
    // achromatic (grey)
    r = g = b = v;
    return;
}
h /= 60;            // sector 0 to 5
i = floor( h );
f = h - i;          // factorial part of h
p = v * ( 1 - s );
q = v * ( 1 - s * f );
t = v * ( 1 - s * ( 1 - f ) );
switch( i ) {
    case 0:
        r = v;
        g = t;
        b = p;
        break;
    case 1:
        r = q;
        g = v;
        b = p;
        break;
    case 2:
        r = p;
        g = v;
        b = t;
        break;
    case 3:
        r = p;
        g = q;
        b = v;
        break;
    case 4:
        r = t;
        g = p;
        b = v;
        break;
    default:        // case 5:
        r = v;
        g = p;
        b = q;
        break;
  }
  }


    private static float MAX(float r, float g, float b) {
    // TODO Auto-generated method stub
   return 0;
  }

   private static float MIN(float r, float g, float b) {
  // TODO Auto-generated method stub
 return 0;
  }

  private static int floor(float h) {
 // TODO Auto-generated method stub
 return 0;
  } }

1 个答案:

答案 0 :(得分:2)

您在main

中声明了方法
class ClassName {
    public static void main(String[] args) {
        void methodInsideMain() {
        }
    }
}

方法不能在Java中的其他方法中。移出这些方法:

class ClassName {
    public static void main(String[] args) {
    }
    static void methodOutsideMain() {
    }
}

在您的代码中,正如您所展示的那样,RGBtoHSVHSVtoRGB是在main内声明的方法。没有其他编译错误。