package Targa;

import java.awt.Color;
/**
* RGBPoint is what every pixel is converted to before using. This class contains
* procedures to change to the different colour spaces used in this research. It can
* be constructed using an integer, or 3 r,g,b components...
*/
public class RGBPoint {
	public int r,g,b,a;
	public double L,alpha,beta;
	public double Lx,ax,bx, Cx, hue;
	public float H,S,Br;


	public double[] getSphericalCoords() {
    double []temp = new double[3];
    calcSpherical();

    temp[0] = L;
    temp[1] = L/java.lang.Math.atan(alpha);
    temp[2] = temp[0]*java.lang.Math.atan(beta);
    return temp;
  }

  public RGBPoint() {
  	this.r = 0;
		this.g = 0;
		this.b = 0;
		this.a = 0;
	}


	public RGBPoint(int rgba) {
		this.r = (rgba & 0xFF0000) >>> 16;
		this.g = (rgba & 0xFF00) >>> 8 ;
		this.b = (rgba & 0xFF) ;
		this.a = (rgba & 0xFF000000) >>> 24;
	}



	public RGBPoint(int r,int g,int b) {
		this.r = r;
		this.g = g;
		this.b = b;
		this.a = 255;
	}



	public RGBPoint(float h, float s, float br) {
		this(Color.HSBtoRGB(h, s, br)) ;
System.out.println("RGB " + this);
		float[] hsb = new float[3] ;
		hsb = Color.RGBtoHSB(r,g,b,hsb) ;
System.out.println("HSB " + hsb[0] + " " + hsb[1] + " " + hsb[2]);
	}



	public void calcLAB() {
		double X,Y,Z,X255,Y255,Z255;

		X = 2.7690*r + 1.7518*g + 1.1300*b ;
		Y = r+ 4.5907*g + 0.0601*b ;
		Z = 0.0565*g + 5.5943*b ;

		X255 = 2.7690*255 + 1.7518*255+1.1300*255;
		Y255 = 255+ 4.5907*255+ 0.0691*255 ;
		Z255 = 0.0565*255+ 5.5943*255;
		Lx = 116.0*java.lang.Math.pow(Y/Y255,(1.0/3.0)) - 16 ;
		ax = 500.0*(java.lang.Math.pow(X/X255,(1.0/3.0)) - java.lang.Math.pow(Y/Y255,(1.0/3.0))) ;
		bx = 200.0*(java.lang.Math.pow(Y/Y255,(1.0/3.0)) - java.lang.Math.pow(Z/Z255,(1.0/3.0))) ;
    Cx = java.lang.Math.sqrt(ax*ax+bx*bx) ;
    hue = java.lang.Math.atan(bx/ax);

	}

  public void calcSpherical() {

    L = java.lang.Math.sqrt((r*r)+(g*g)+(b*b));
    if (L == 0) {
      alpha = java.lang.Math.PI;
      beta = java.lang.Math.PI;
    }
    else {
      alpha = java.lang.Math.acos(b/L) ;
      beta = java.lang.Math.acos(r/(L*java.lang.Math.sin(alpha)));
    }
    if (new Double(beta).isNaN() == true) {
      beta = 0.0;
    }
    if (new Double(alpha).isNaN() == true) {
      alpha = 0.0;
    }
  }


	public void calcHSB() {
		float[] hsb = new float[3] ;
		hsb = Color.RGBtoHSB(r,g,b,hsb) ;
		H = hsb[0] ;
		S = hsb[1] ;
		Br = hsb[2] ;
	}

	public int toInt() {
		return ((this.a << 24) | (this.r << 16) | (this.g << 8) | this.b);
	}
	public int toLuminance() {
		return (int)((0.30*r)+(0.59*g) + (0.11*b)) ;
	}
	public String toString() {
		return "Red:"+r+" Green:"+g+" Blue:"+b + " Alpha:"+a;
	}



  public static void main(String[] args) {
    RGBPoint pt = new RGBPoint(100,100,100) ;
    pt.calcLAB();

    System.out.println(pt.Lx + " "  + pt.ax + " " + pt.bx + " " + pt.Cx + " " + pt.hue) ;
  }
}
