import java.applet.*; import java.awt.*; import java.util.Random; import java.lang.String; import ImpulseFunctions; public class Colors3 extends Applet { // double buffering image private Dimension offDim; private Image offImage;// offscreen image private Graphics offGraphics; // associated graphics object private boolean offReady = false; int wavelength[] = {480, 580, 680}; double power[] = {0.5, 0.5, 0.5}; Color spectrum[] = new Color[198]; Color powerScale[] = new Color[100]; int click = -1; private int bcount=0; public void init() { int i; ImpulseFunctions xyz = new ImpulseFunctions(wavelength, power); setBackground(Color.black); setForeground(new Color(0x00AA00)); 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(); repaint(); } 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; ImpulseFunctions xyz = new ImpulseFunctions(wavelength, power); double X = xyz.computeX(); double Y = xyz.computeY(); double Z = xyz.computeZ(); double x = X/(X+Y+Z); double y = Y/(X+Y+Z); Color colFG = getForeground(); Color colBG = getBackground(); 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(rgb(X,Y,Z)); offGraphics.drawRect(45, 3, 204, 107); offGraphics.drawRect(44, 2, 206, 109); for (i=0; i<3; i++) { if (click==i) offGraphics.setColor(Color.white); else offGraphics.setColor(Color.gray); offGraphics.drawLine((wavelength[i]-380)/2+51, 104, (wavelength[i]-380)/2+51, 104-(int)Math.round(power[i]*100)); offGraphics.drawString(Double.toString(power[i]), 160, 145+i*15); offGraphics.drawString(Integer.toString(wavelength[i]), 100, 145+i*15); } 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); } offGraphics.setColor(Color.magenta); offGraphics.drawString("Wavelength", 80, 130); offGraphics.drawString("Power", 160, 130); 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) { int wl; double pwr; if (click>=0) { wl=380+(x-51)*2; pwr=(104-y)/100.0; if (wl<380) wl=380; if (wl>774) wl=774; if (pwr<0.0) pwr=0.0; if (pwr>1.0) pwr=1.0; wavelength[click]=wl; power[click]=pwr; click=-1; repaint(); return true; } return false; } public boolean mouseDrag(Event e, int x, int y) { int wl; double pwr; if (click>=0) { wl=380+(x-51)*2; pwr=(104-y)/100.0; if (wl<380) wl=380; if (wl>774) wl=774; if (pwr<0.0) pwr=0.0; if (pwr>1.0) pwr=1.0; wavelength[click]=wl; power[click]=pwr; repaint(); return true; } return false; } int sqr (int i) { return i*i; } public boolean mouseDown(Event e, int x, int y) { int wl, i, dist, minDist; double pwr; if ((x > 50) && (x < 50+198) && (y>3) && (y<104)) { minDist=Integer.MAX_VALUE; wl=380+(x-51)*2; pwr=(104-y)/100.0; for (i=0; i<3; i++) { dist=sqr(wavelength[i]-wl)+sqr((int)Math.round((power[i]-pwr)*100)); if (dist