package dataBase.subPicture;

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

public class Pixel implements Serializable {
	
	private static final long serialVersionUID = 3L;
	public short r,g,b;
	
	/**
	 * Constructor: full
	 * @param rgb - RGB-array. Alpha ignored.
	 */
	public Pixel(int[] rgb){
		r = (short) rgb[0];
		g = (short) rgb[1];
		b = (short) rgb[2];
	}
	
	/**
	 * Constructor: empty
	 */
	public Pixel(){
		//empty
	}
	
	/**
	 * Testing, assertion
	 * @return true if assertion (pixel values between 0 and 255)
	 */
	public boolean test(){
		return r<256 && g<256 && b<=256 &&
				r>0 && g>0 && b>0;
	}
	
	/**
	 * @return luminance within 255
	 */
	public int getLumi(){
		return (int)r+(int)g+(int)b;
	}
	
//	/**
//	 * @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(r, g, b, hsv);
		return hsv;
	}
	
	@Override
	public String toString(){
		return ""+r+"/"+g+"/"+b;
	}
	
	protected void writeObjectNew(java.io.ObjectOutputStream out) throws IOException {
		out.writeShort(r);
		out.writeShort(g);
		out.writeShort(b);
	}
	protected Pixel readObjectNew(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
		r=in.readShort();
		g=in.readShort();
		b=in.readShort();
		return this;
	}
}
