import java.awt.*; import java.awt.event.*; import java.applet.*; public class hsb extends Applet implements AdjustmentListener{ Label lb_r=new Label("赤(Red)"); Label lb_g=new Label("緑(Green)"); Label lb_b=new Label("青(Blue)"); Label lb_h=new Label("色相(Hue)"); Label lb_s=new Label("彩度(Saturation)"); Label lb_c=new Label("明度(Brightness)"); Scrollbar scr_r,scr_g,scr_b,scr_h,scr_s,scr_c; Panel pn,ps; TextField tr = new TextField(4); TextField tg = new TextField(4); TextField tb = new TextField(4); TextField trx = new TextField(4); TextField tgx = new TextField(4); TextField tbx = new TextField(4); TextField th = new TextField(4); TextField ts = new TextField(4); TextField tc = new TextField(4); int r=255; int g=0; int b=0; float h=0; float s=100; float c=100; public void init(){ scr_r = new Scrollbar(Scrollbar.HORIZONTAL,0,16,0,271); scr_g = new Scrollbar(Scrollbar.HORIZONTAL,0,16,0,271); scr_b = new Scrollbar(Scrollbar.HORIZONTAL,0,16,0,271); scr_h = new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,110); scr_s = new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,110); scr_c = new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,110); pn = new Panel(); pn.setLayout(new GridLayout(3,3)); pn.add(lb_r); pn.add(lb_g); pn.add(lb_b); pn.add(tr); pn.add(tg); pn.add(tb); pn.add(scr_r); pn.add(scr_g); pn.add(scr_b); ps = new Panel(); ps.setLayout(new GridLayout(3,3)); ps.add(lb_h); ps.add(lb_s); ps.add(lb_c); ps.add(th); ps.add(ts); ps.add(tc); ps.add(scr_h); ps.add(scr_s); ps.add(scr_c); setLayout(new BorderLayout()); add("North",pn); add("South",ps); scr_r.addAdjustmentListener(this); scr_g.addAdjustmentListener(this); scr_b.addAdjustmentListener(this); scr_h.addAdjustmentListener(this); scr_s.addAdjustmentListener(this); scr_c.addAdjustmentListener(this); scr_r.setValue(r); scr_g.setValue(g); scr_b.setValue(b); scr_h.setValue((int) h); scr_s.setValue((int) s); scr_c.setValue((int) c); repaint(); } public void adjustmentValueChanged(AdjustmentEvent e){ if((e.getSource()==scr_r)|(e.getSource()==scr_g)|(e.getSource()==scr_b)){ r=scr_r.getValue();repaint();g=scr_g.getValue();b=scr_b.getValue(); float hsb[] = Color.RGBtoHSB(r,g,b,null); h = (hsb[0]*100); s = (hsb[1]*100); c = (hsb[2]*100); scr_h.setValue((int) h); scr_s.setValue((int) s); scr_c.setValue((int) c); } if((e.getSource()==scr_h)|(e.getSource()==scr_s)|(e.getSource()==scr_c)){ h=scr_h.getValue();s=scr_s.getValue();c=scr_c.getValue(); Color co = Color.getHSBColor(h/100,s/100,c/100); r=co.getRed();g=co.getGreen();b=co.getBlue(); /* int rgb = Color.HSBtoRGB(h/100,s/100,c/100); r=(rgb & 0x00ff0000) >> 16; g=(rgb & 0x0000ff00) >> 8; b=rgb & 0x000000ff; */ scr_r.setValue(r); scr_g.setValue(g); scr_b.setValue(b); } repaint(); } public void paint(Graphics z){ z.setColor(new Color(r,g,b)); Dimension d = getSize(); z.fillRect(0,0,d.width,d.height); tr.setText(""+r+"["+hex(r)+"]"); tg.setText(""+g+"["+hex(g)+"]"); tb.setText(""+b+"["+hex(b)+"]"); th.setText(""+h/100); ts.setText(""+s/100); tc.setText(""+c/100); z.setColor(new Color(r,g,b)); Font font = new Font("MS Pゴシック",Font.BOLD,32); z.setFont(font); z.drawString("色見本",d.width/2,d.height/2); } public String hex(int n) { String t[]={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}; String x = ""; for (int i=1;n>=16;i++) { x=t[n%16]+x; n=(n-n%16)/16; } x=t[n%16]+x; return x; } }