/*
 * ColorPicker.java  v2.1 11/24/95 
 *
 * Author: Ken Rawlings (krawling@bluemarble.net)
 *
 * Copyright (c) 1995 Grey Associates
 *
 * Permission to use, copy, modify, and distribute this software
 * for non-commercial purposes and without fee is hereby granted.
 *
 * This copyright statement must acompany any redistribution of this code. 
 * This software is provided as-is, no warranties. For information 
 * about Grey Associates contact grey@bluemarble.net or visit our web site 
 * at http://www.greyassoc.com/java/
 *
 */

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.Scrollbar;
import java.awt.TextField;

public class ColorPicker extends Applet {
	ColorPanel out;
	ColorChooser select;

	public void init() {
		setLayout(new BorderLayout());

		out = new ColorPanel();
		select = new ColorChooser(out, 255, 204, 102);
		add("West", select);
		add("Center", out);
	}

}

class ColorPanel extends Panel {
	Panel disp;

	ColorPanel() {
		setLayout(new BorderLayout());
		disp = new Panel();
		add("Center", disp);
	}

	public Insets insets() {
		return new Insets(1, 1, 1, 1);
	}

	public Dimension minimumSize() {
		return new Dimension(100, 100);
	}

	public void paint(Graphics g) {
		Rectangle rect = bounds();

		super.paint(g);
		g.drawRect(0, 0, rect.width - 1, rect.height - 1);
	}

	void change(Color new_c) {
		disp.setBackground(new_c);
		disp.repaint();
	}
}

class ColorChooser extends Panel {
	ColorPanel controller;
	Label html;
	RGBChooser red, green, blue;

	public Insets insets() {
		return new Insets(1, 1, 1, 0);
	}

	public void paint(Graphics g) {
		Rectangle rect = bounds();

		super.paint(g);
		g.draw3DRect(0, 0, rect.width - 1, rect.height - 1, true);
	}

	public Dimension preferredSize() {
		return new Dimension(75, 200);
	}

	ColorChooser(ColorPanel myController, int r, int g, int b) {
		super();

		controller = myController;
		setLayout(new BorderLayout());

		Panel controls = new Panel();
		controls.setLayout(new GridLayout(3, 1));
		red = new RGBChooser(this, r, "Red");
		controls.add(red);
		green = new RGBChooser(this, g, "Green");
		controls.add(green);
		blue = new RGBChooser(this, b, "Blue");
		controls.add(blue);
		add("Center", controls);
		html = new Label("#000000");
		html.setBackground(Color.gray);
		add("South", html);

		colorChange();
	}

	void colorChange() {
		Color new_c = getColor();
		int col[] = new int[3];
		StringBuffer text;

		text = new StringBuffer("");
		col[0] = new_c.getRed();
		col[1] = new_c.getGreen();
		col[2] = new_c.getBlue();

		for (int i = 0; i < 3; i++) {
			if (col[i] < 16)
				text.append('0');
			text.append(Integer.toString(col[i], 16));
		}

		controller.change(new_c);
		html.setText("#" + text.toString());

	}

	Color getColor() {
		// System.out.println("Color Change");
		return new Color(red.value(), green.value(), blue.value());
	}
}

class InputPanel extends Panel {
	public Insets insets() {
		return new Insets(0, 3, 0, 3);
	}
}

class RGBChooser extends Panel {
	ColorChooser controller;
	Scrollbar colorScroll;
	TextField colorField;
	int c_value;

	public Insets insets() {
		return new Insets(1, 1, 2, 0);
	}

	public void paint(Graphics g) {
		Rectangle rect = bounds();
		// g.drawLine(rect.x, rect.y, rect.height, rect.width);
		// super.paint(g);
	}

	public boolean handleEvent(Event evt) {
		Integer in;

		if (evt.target == colorScroll) {
			in = (Integer) evt.arg;
			set_value(255 - in.intValue());
			controller.colorChange();

			return true;
		} else if (evt.target == colorField) {
			String tmp;
			tmp = (String) evt.arg;
			in = Integer.valueOf(tmp);
			set_value(in.intValue());
			colorScroll.setValue(-1 * (in.intValue() - 255));
			controller.colorChange();
			return true;
		} else {
			// System.out.println("!Handled: " + evt);
			return super.handleEvent(evt);
		}
	}

	RGBChooser(ColorChooser myController, int initial, String caption) {
		super();
		controller = myController;

		setLayout(new BorderLayout());

		colorField = new TextField();
		colorScroll = new Scrollbar(Scrollbar.VERTICAL, initial, 0, 0, 255);
		set_value(255 - initial);
		add("East", colorScroll);

		InputPanel temp = new InputPanel();
		temp.setLayout(new GridLayout(3, 1));
		Label label1 = new Label(caption); // ,Label.LEFT);
		temp.add(label1);
		temp.add(colorField);
		add("Center", temp);
	}

	int value() {
		return c_value;
	}

	void set_value(int initial) {
		colorField.setText(String.valueOf(initial));
		c_value = initial;
	}
}


