package dataBase.subPicture;

import java.awt.Color;
import java.io.IOException;
import java.io.Serializable;

public class PixelByte implements Serializable {
	
	private static final long serialVersionUID = 5L;
	public byte r1,g1,b1;
	static int count = 0;
	
	/**
	 * Constructor: full
	 * @param rgb - RGB-array. Alpha ignored.
	 */
	public PixelByte(int[] rgb){
		
		assert rgb[0]>0 && rgb[1]>0 && rgb[2]>0 && rgb[0]<256 && rgb[1]<256 && rgb[2]<256;
		
		r1 = (byte) rgb[0];
		g1 = (byte) rgb[1];
		b1 = (byte) rgb[2];
		
		//if(count++ % 1000 == 0)
		//	Out.pl(" >> Pixel = "+toString());
	}
	
	/**
	 * Constructor: empty
	 */
	public PixelByte(){
		//empty
	}
	
//	/**
//	 * Testing, assertion
//	 * @return true if assertion (pixel values between 0 and 255)
//	 */
//	private boolean test(){
//		return r<256 && g<256 && b<=256 &&
//				r>0 && g>0 && b>0;
//	}
	
	/**
	 * @return luminance within 255
	 */
	public int getLumiFull(){
		return r1&0xff + g1&0xff + b1&0xff;
	}
	
//	/**
//	 * @return luminance within 255
//	 */
//	public short getLumi255(){
//		return (short) ((r+g+b+0.5f)/3.0f);
//	}
	
	/**
	 * Also called HSB (Hue, Saturation, Value/Brightness)
	 * @return {Hue,Saturation,Value}
	 */
	public float[] getHSV(){
		float[] hsv = new float[3];
		Color.RGBtoHSB(r1&0xff, g1&0xff, b1&0xff, hsv);
		return hsv;
	}
	
	@Override
	public String toString(){
		return ""+ (r1&0xff) +"/"+ (g1&0xff) +"/"+ (b1&0xff);
	}
	
	protected void writeObjectNew(java.io.ObjectOutputStream out) throws IOException {
		out.writeByte(r1);
		out.writeByte(g1);
		out.writeByte(b1);
	}
	protected PixelByte readObjectNew(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
		r1=in.readByte();
		g1=in.readByte();
		b1=in.readByte();
		
		//if(count++ % 1000 == 0)
		//	Out.pl(" >> Pixel = "+toString());
		
		return this;
	}
}
