//
/* ----------------------------------------------------------------------------------------------
    rgb.java		RGB 3色の混合						ver. 2.00 (JDK 1.02)
    														ueyama@infonet.co.jp  1999.08.31
    														update: 2007.10.02
---------------------------------------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;

public class rgb extends Applet
{
	int		RGB[]={255,255,255};										// RGB値 (RGB[2]: 赤  RGB[1]: 緑  RGB[0]: 青)
	int		Nx[]={105,14,60}, Ny[]={ 87,87,13};							// RGB値の表示位置
			// Px, Py: カラーパレット多角形の頂点の座標値
	int		Px[][]={{75,125,150,125},{25,0,25,75},{50,75,100},{50,25,125,100},{75,100,125},{25,50,75},{75,50,100}};
	int		Py[][]={{130,130,87,43},{43,87,130,130},{87,130,87},{0,43,43,0},{43,87,43},{43,87,43},{43,87,87}};
	int		Pg[]=new int[150*130];										// カラーパレットのピクセルデータ用メモリ
	int		Sc[][]={{0xffffff, 0xffcccc, 0xffcc99, 0xffffcc, 0x99ff99, 0xccffcc, 0xffccff},	// サンプルカラー値
					{0xaaaaaa, 0xff0000, 0xff9900, 0xffff00, 0x33ff33, 0x33ccff, 0xcc66cc},
					{0x555555, 0x990000, 0xcc6600, 0x999900, 0x009900, 0x3333ff, 0x993366},
					{0x000000, 0x330000, 0x663300, 0x333300, 0x003300, 0x000066, 0x330033}};
	Image	Img, Buf;
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void init()
	{
		String bg=getParameter("bgcolor");								// 背景色の読み込み
		int bgc=Integer.valueOf(bg,16).intValue();
		setBackground(new Color(bgc));
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));			// gif ファイルの読み込み
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
		Buf=createImage(size().width,size().height);
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public void dsp_g(int x, int y, int m, int n, int c, Graphics g)	// 数字・図形の表示
	{
		int w=0, h=0, ox=0, oy=0;
		switch(c)
		{
			case  0: w=134; h= 42; ox=  0; oy=  0; break;				// カラーバー
			case  1: w=  9; h= 16; ox=134; oy=  0; break;				// 数字
			case  2: w=  5; h= 10; ox=134; oy= 32; break;				// マーク
		}
		Graphics gx=g.create();
		gx.clipRect(x, y, w, h);
		gx.drawImage(Img,x-(ox+m*w),y-(oy+n*h),this);
		gx.dispose();
	}
	
	public void dsp_num(int x, int y, int n, Graphics g)				// RGB 値の表示
	{
		int i,m,s;
		String dec=Integer.toString(RGB[n],10);							// 10進数 文字列
		m=dec.length();
		for(i=0;i>16; RGB[1]=c&0x00ff00; RGB[1]=RGB[1]>>8; RGB[0]=c&0x0000ff; 
	}
	
	public int get_col(int n)											// RGB 値を 色の値に変換する
	{
		return RGB[2]*0x10000*(n&4)/4 + RGB[1]*0x100*(n&2)/2 + RGB[0]*(n&1); // n=1:B 2:G 3:BG 4:R 5:RB 6:RG 7:RGB
	}
	
	public void dsp_bar(Graphics g)										// カラーバーを描く
	{
		dsp_g(8,140,0,0,0,g);											// カラーバーの表示
		for(int i=0;i<3;i++) dsp_g(RGB[2-i]/2+9,i*15+141,0,0,2,g);		// マークの表示
	}

	public void put_pg()												// ピクセルデータの保存
	{
		PixelGrabber pg=new PixelGrabber(Buf,0,0,150,130,Pg,0,150);
		try {pg.grabPixels();}
		catch(InterruptedException ie) {return;}
		if((pg.status() & ImageObserver.ABORT) != 0) return;
	}

	public void paint(Graphics g)
	{
		int i,j;
		Graphics mg=Buf.getGraphics();									// バッファを作成
		mg.clearRect(0,0,size().width,size().height);
		for(i=0;i<7;i++)												// カラーパレット(六角形)を描く
		{
			mg.setColor(new Color(get_col(i+1)));						// 色の設定
			j=(i<2 || i==3)?4:3;										// j: 多角形の角数
			mg.fillPolygon(Px[i],Py[i],j);								// 多角形を描く
		}
		put_pg();														// カラーデータを PixelGrabber に格納
		for(i=0;i<3;i++) dsp_num(Nx[i],Ny[i],i,mg);						// RGB 値の表示
		dsp_bar(mg);
		for(i=0;i<4;i++)												// カラーサンプルの表示
		{
			for(j=0;j<7;j++)
			{
				mg.setColor(new Color(Sc[i][j]));
				mg.fillRect(j*20+6,i*14+196,18,12);
			}
		}
		g.drawImage(Buf,0,0,this);										// バッファを表示
		mg.dispose();
	}

	public boolean mouseDown(Event e, int mx, int my)
	{
		int c=0,i=0,j=0;
		if(my<130)														// カラーパレットをクリック
		{
			c=Pg[my*150+mx] & 0xffffff;									// クリックした点の色の値
			for(i=7;i>=0;i--) if(c==get_col(i)) break;					// 色の値の照合
			if(i>0)
			{
				if((i&1)==1) RGB[0]=255;								// B の値を 255 にする
				if((i&2)==2) RGB[1]=255;								// G の値を 255 にする
				if((i&4)==4) RGB[2]=255;								// R の値を 255 にする
			}
		}
		if(inside(my,138,182))											// カラーバーをクリック
		{
			c=2-(my-138)/15;											// クリックしたカラーバー No.
			if(mx<11) RGB[c]=0;											// バーより左
			else if(mx>139) RGB[c]=255;									// バーより右
			else RGB[c]=(mx-11)*2;										// バー内  色データを mx で決める
		}
		if(my>195)														// カラーサンプルをクリック
		{
			i=(my-196)/14; j=(mx-6)/20;
			get_rgb(Sc[i][j]);											// カラーサンプルの色に変える
		}
		repaint();
		return true;
	}
	
	public boolean mouseDrag(Event e, int mx, int my)					// マウスがドラッグされたときの処理
	{
		if(inside(mx,11,138) && inside(my,138,182))						// カラーバー内をドラッグ
		{
			RGB[2-(my-138)/15]=(mx-11)*2;								// RGB のデータを更新
			repaint();
		}
		return true;
	}
}
//
戻る