public class pixel {
	public int red;
	public int green;
	public int blue;
	
	public pixel(int r, int g, int b) {
		red = r;
		green = g;
		blue = b;
	}

	public pixel(int v) {
		red = v;
		green = v;
		blue = v;
	}
	
	public pixel invert() {
		return invert(255);
	}
	
	public pixel invert(int max) {
		return new pixel(max-red, max-green, max-blue);
	}
	
	public pixel grayscale() {
		int v = (red + green + blue) / 3;
		return new pixel(v);
	}
	
	public pixel emboss(pixel p) {
		int v = 128 + absMax(
			red - p.red,
			green - p.green,
			blue - p.blue
		);
		if (v > 255) v = 255;
		else if (v < 0) v = 0;
		return new pixel(v);
	}
	
	public String toString() {
		return red + " " + green + " " + blue + " ";
	}

	public static pixel average(pixel...pixels) {
		int r = 0,
			g = 0,
			b = 0;
		for (pixel p : pixels) {
			r += p.red;
			g += p.green;
			b += p.blue;
		}
		r = r / pixels.length;
		g = g / pixels.length;
		b = b / pixels.length;
		return new pixel(r, g, b);
	}
	
	public static int absMax(int...vals) {
		int max = 0;
		for (int v : vals) {
			if (Math.abs(v) > Math.abs(max)) {
				max = v;
			}
		}
		return max;
	}
	
}
