/*
The code is available only to persons
currently affiliated with any of the following,
      o Universities and schools
      o Non-commercial research organizations
      o Registered not-for-profit organizations
      o Registered charities
For these people, the license is of the form of
the GNU Public License version 2.  The restrictions
must remain attached to this and any subsequently derived code.
All other people have no implicit right to use, read, or copy
this software unless granted by Mr. Deck
THIS SOFTWARE IS PROVIDED "AS IS" AND MR. DECK
MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
IMPLIED.  BY WAY OF EXAMPLE, BUT NOT LIMITATION, HE
MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANT-
ABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE
OF THE LICENSED SOFTWARE AND DOCUMENTATION WILL NOT
INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR
*/

import java.awt.*;

public class colorSelector extends Frame{
int rgb[]    = { 255,255,255 };
int index    = -1;
Color red    = new Color(140,0,0);
Color green  = new Color(0,80,0);
Color blue   = new Color(0,0,140);
chiclet chic = null;
int        y = 0;

   public colorSelector(chiclet chic){
	super(env.cpick);
	setBackground(Color.white);
	rgb[0] = (int)(chic.col.getRed());
	rgb[1] = (int)(chic.col.getGreen());
	rgb[2] = (int)(chic.col.getBlue());
	this.chic = chic;
        pack();
        reshape( getToolkit().getScreenSize().width/2-160,
		 getToolkit().getScreenSize().height/2-70,
	        315,150);
  }

   public void paint(Graphics g){
		y = size().height/2-20;
		g.clearRect(rgb[0]+10,y+0,255-rgb[0],18);
		g.clearRect(rgb[1]+10,y+21,255-rgb[1],18);
		g.clearRect(rgb[2]+10,y+42,255-rgb[2],18);
		g.setColor(Color.lightGray);
		g.drawRect(rgb[0]+10,y+1,255-rgb[0],16);
		g.drawRect(rgb[1]+10,y+21,255-rgb[1],16);
		g.drawRect(rgb[2]+10,y+42,255-rgb[2],16);
		g.setColor(red);
		g.fill3DRect(10,y,rgb[0],18,true);
		g.setColor(green);
		g.fill3DRect(10,y+21,rgb[1],18,true);
		g.setColor(blue);
		g.fill3DRect(10,y+42,rgb[2],18,true);
		g.setColor(chic.col = new 
		Color((float)rgb[0]/255,
                      (float)rgb[1]/255,
                      (float)rgb[2]/255));
		g.fill3DRect(270,y,30,60,true);
	}

	public boolean handleEvent(Event e){
		e.x-=10;
		if(e.x>255) e.x=255;
		else if(e.x<0) e.x = 0;
		switch(e.id){
		    case Event.MOUSE_DOWN:
		             if(e.y<=(21+y)) index=0;
			else if(e.y<=(41+y)) index=1;
			else if(e.y<=(61+y)) index=2;
		    case Event.MOUSE_DRAG:
                        if(index==-1){
		             if(e.y<=(21+y)) index=0;
			else if(e.y<=(41+y)) index=1;
			else if(e.y<=(61+y)) index=2;
			}else{
		             if(e.y<=21+y) rgb[index]=e.x;
			else if(e.y<=41+y) rgb[index]=e.x;
			else if(e.y<=61+y) rgb[index]=e.x;
			if(rgb[index]>255) rgb[index]=255;
			if(rgb[index]<0)   rgb[index]=0;
			repaint();
			}
			break;
		    case Event.MOUSE_UP:
                      index=-1;
		      if(chic.it.MSIE) chic.it.IEBUG=true;
                      String s = 
			chic.it.lst.getItem(chic.it.curItem).substring(0,4);
                      chic.it.lst.replaceItem(s+
                        chic.it.getHex(
                           chic.col.getRed(),
                           chic.col.getGreen(),
                           chic.col.getBlue()),
                        chic.it.curItem);
                      chic.disp=false; //blame microsoft
		      chic.it.lst.select(chic.it.curItem);
                      chic.it.ctab.put(new 
                              Character(s.charAt(0)),chic.col);
		       int x=0,y=0;
		      for(int r=0;r<50;r++){
                         for(int c=0;c<50;c++){
                            chic.it.da.img_g.setColor((Color) 
				chic.it.ctab.get(
                                    chic.it.ta.text[c][r]));
                            //chic.it.da.img_g.drawLine(r,c,r,c);
                            chic.it.da.img_g.fillRect(x,y,5,5);
			    x+=5;
                         }
			y+=5;
			x=0;
                      }
		      chic.repaint();
                      chic.it.da.repaint();
                      chic.disp=true; //blame microsoft
       		      break; 
		    case Event.WINDOW_DESTROY:
			this.dispose();
			break;		
                    case 1005:
			index=-1;
			break;
		}
		return true;	
	}
}
