import java.applet.*; import java.awt.*; import java.util.Random; import java.lang.String; import ImpulseFunctions; public class Colors4 extends Applet { private Dimension offDim; private Image offImage; private Graphics offGraphics; private boolean offReady = false; private int phase; private Random rnd=new Random(); private ImpulseFunctions xyz = new ImpulseFunctions(380, 0.0, 380, 0.0, 380, 0.0); double power[] = new double [198]; Color spectrum[] = new Color[198]; Color powerScale[] = new Color[100]; int click = -1; int lastX; private int bcount=0; public void init() { int i; for (i=0; i<198; i++) spectrum[i]=rgb(xyz.getx(i*2+380)*2.0,xyz.gety(i*2+380)*2.0,xyz.getz(i*2+380)*2.0); for (i=0; i<100; i++) powerScale[i]=rgb(xyz.getx(520)*i/100.0,xyz.gety(520)*i/100.0,xyz.getz(520)*i/100.0); offDim = new Dimension(300, 300); offImage = createImage(offDim.width, offDim.height); offGraphics = offImage.getGraphics(); } public void start() { requestFocus(); repaint(); } public void paint(Graphics g) { if (offReady) g.drawImage(offImage, 0, 0, null); else update(g); } Color rgb (double X, double Y, double Z) { float r, g, b; r=(float)(3.2410*X-1.5374*Y-0.4986*Z)/3.0f; g=(float)(-0.9692*X+1.8760*Y+0.0416*Z)/3.0f; b=(float)(0.0556*X-0.2040*Y+1.0570*Z)/3.0f; if (r<0.0) r=0.0f; if (g<0.0) g=0.0f; if (b<0.0) b=0.0f; if (r<=0.00304) r*=12.92; else r=1.055f*(float)Math.exp(Math.log(r)/2.4)-0.055f; if (g<=0.00304) g*=12.92; else g=1.055f*(float)Math.exp(Math.log(g)/2.4)-0.055f; if (b<=0.00304) b*=12.92; else b=1.055f*(float)Math.exp(Math.log(b)/2.4)-0.055f; if (r>1.0) r=1.0f; if (g>1.0) g=1.0f; if (b>1.0) b=1.0f; return new Color(r,g,b); } public void update(Graphics g) { int i; double X=0, Y=0, Z=0, x, y; offGraphics.setColor(Color.black); offGraphics.fillRect(0, 0, offDim.width, offDim.height); offGraphics.setColor(Color.white); offGraphics.drawRect(0, 0, offDim.width-1, offDim.height-1); offGraphics.setColor(Color.gray); offGraphics.drawRect(45, 3, 204, 107); offGraphics.drawRect(44, 2, 206, 109); for (i=0; i<198; i++) { offGraphics.setColor(Color.white); offGraphics.drawLine(51+i, 104, 51+i, 104-(int)Math.round(power[i]*100)); } for (i=0; i<198; i++) { offGraphics.setColor(spectrum[i]); offGraphics.drawLine(i+51, 105, i+51, 108); } for (i=0; i<100; i++) { offGraphics.setColor(powerScale[i]); offGraphics.drawLine(47, 103-i, 50, 103-i); } if (click<0) { for (i=0; i<197; i++) { X+=power[i]*xyz.getx(i*2+380)*2; Y+=power[i]*xyz.gety(i*2+380)*2; Z+=power[i]*xyz.getz(i*2+380)*2; } x = X/(X+Y+Z); y = Y/(X+Y+Z); offGraphics.setColor(rgb(X/40.0,Y/40.0,Z/40.0)); offGraphics.drawRect(45, 3, 204, 107); offGraphics.drawRect(44, 2, 206, 109); offGraphics.setColor(Color.blue); offGraphics.drawString("X", 80, 200); offGraphics.setColor(Color.white); offGraphics.drawString(Double.toString(X), 100, 200); offGraphics.setColor(Color.blue); offGraphics.drawString("Y", 80, 215); offGraphics.setColor(Color.white); offGraphics.drawString(Double.toString(Y), 100, 215); offGraphics.setColor(Color.blue); offGraphics.drawString("Z", 80, 230); offGraphics.setColor(Color.white); offGraphics.drawString(Double.toString(Z), 100, 230); offGraphics.setColor(Color.cyan); offGraphics.drawString("Y", 80, 245); offGraphics.setColor(Color.white); offGraphics.drawString(Double.toString(Y), 100, 245); offGraphics.setColor(Color.cyan); offGraphics.drawString("x", 80, 260); offGraphics.setColor(Color.white); offGraphics.drawString(Double.toString(x), 100, 260); offGraphics.setColor(Color.cyan); offGraphics.drawString("y", 80, 275); offGraphics.setColor(Color.white); offGraphics.drawString(Double.toString(y), 100, 275); } offReady=true; paint(g); } public boolean mouseUp(Event e, int x, int y) { double pwr; int i; if (click>=0) { pwr=(104-y)/100.0; if (pwr<0.0) pwr=0.0; if (pwr>1.0) pwr=1.0; if (x 50) && (i < 51+198)) power[i-51]=pwr; } else { for (i=lastX+1; i<=x; i++) if ((i > 50) && (i < 51+198)) power[i-51]=pwr; } if ((x > 50) && (x < 50+198)) power[x-51]=pwr; click=-1; repaint(); return true; } return false; } public boolean mouseDrag(Event e, int x, int y) { double pwr; int i; if (click>=0) { pwr=(104-y)/100.0; if (pwr<0.0) pwr=0.0; if (pwr>1.0) pwr=1.0; if (x 50) && (i < 51+198)) power[i-51]=pwr; } else { for (i=lastX; i<=x; i++) if ((i > 50) && (i < 51+198)) power[i-51]=pwr; } if ((x > 50) && (x < 50+198)) power[x-51]=pwr; lastX=x; repaint(); return true; } return false; } int sqr (int i) { return i*i; } public boolean mouseDown(Event e, int x, int y) { double pwr; if ((x > 50) && (x < 51+198) && (y>3) && (y<104)) { pwr=(104-y)/100.0; power[x-51]=pwr; click=0; lastX=x; repaint(); return true; } else click=-1; return false; } }