1 import java.awt.*; 2 import java.awt.event.*; 3 import java.awt.image.*; 4 5 public class ColorPicker extends Canvas implements MouseListener 6 { 7 private float 8 width = 204, 9 height = 204, 10 width2 = 10; 11 public Color selected = new Color(0, 0, 0); 12 13 private Color colors[][]; 14 15 private Image image; 16 private Graphics graphics; 17 18 private TextField output; 19 20 public ColorPicker() 21 { 22 addMouseListener(this); 23 24 colors = new Color[(int)(width+width2)][(int)height]; 25 } 26 27 public void paint(Graphics screen) 28 { 29 float r, g, b; 30 31 if (super.getSize().width != width+width2 || super.getSize().height != height) 32 { 33 image = null; 34 width = super.getSize().width-width2; 35 height = super.getSize().height; 36 37 colors = new Color[Math.round(width+width2)][Math.round(height)]; 38 } 39 40 if (image == null || graphics == null) 41 { 42 image = createImage(Math.round(width+width2), Math.round(height)); 43 graphics = image.getGraphics(); 44 45 for (float x = 0; x < 1f; x += 1f/width) 46 { 47 if (x <= 1f/3f) 49 { 50 r = (1f/3f-x)*3; 51 g = x*3; 52 b = 0f; 53 } 54 else if (x >= 1f/3f && x <= 2f/3f) 55 { 56 r = 0f; 57 g = (1f/3f-(x-1f/3f))*3; 58 b = (x-1f/3f)*3; 59 } 60 else 61 { 62 b = (1f/3f-(x-2f/3f))*3; 63 r = (x-2f/3f)*3; 64 g = 0f; 65 } 66 67 for (float y = 0f; y < 1f; y += 1f/height) 68 { 69 71 float 72 r2 = (r-0.5f)*(1f-y)+0.5f, 73 g2 = (g-0.5f)*(1f-y)+0.5f, 74 b2 = (b-0.5f)*(1f-y)+0.5f; 75 76 int 78 iX = Math.round(x*width), 79 iY = Math.round(y*height); 80 81 if (iX >= width+width2 || iY >= height)break; 82 83 colors[iX][iY] = new Color(r2, g2, b2); 84 graphics.setColor(colors[iX][iY]); 85 graphics.drawLine(iX, iY, iX, iY); 86 } 87 } 88 } 89 90 for (float y = 0f; y < 2f; y += 2f/height) 92 { 93 if (y <= 1f) 94 { 95 r = (float)selected.getRed()/255f *y; 96 g = (float)selected.getGreen()/255f*y; 97 b = (float)selected.getBlue()/255f *y; 98 } 99 else { 101 r = (float)selected.getRed()/255f +y-1f; 102 g = (float)selected.getGreen()/255f+y-1f; 103 b = (float)selected.getBlue()/255f +y-1f; 104 } 105 106 if (r > 1f)r = 1f; 107 if (g > 1f)g = 1f; 108 if (b > 1f)b = 1f; 109 110 int 111 iY = Math.round(y/2*height), 112 iX = Math.round(width), 113 iX2 = Math.round(width+width2); 114 115 if (iX >= width+width2 || iY >= height)break; 116 117 Color c = new Color(r, g, b); 118 for (int x = 0; x < width2; x ++) 119 colors[Math.round(width+x)][iY] = c; 120 121 122 graphics.setColor(c); 123 graphics.drawLine(iX, iY, iX2, iY); 124 } 125 126 screen.drawImage(image, 0, 0, this); 127 } 128 129 public void setOutput(TextField out) 130 { 131 output = out; 132 } 133 134 public void mouseClicked(MouseEvent e) 135 { 136 try 137 { 138 selected = colors[e.getX()][e.getY()]; 139 } 140 catch (ArrayIndexOutOfBoundsException asd){} 141 repaint(); 142 143 if (output != null) 144 output.setText(Integer.toString(selected.getRGB() & 0xFFFFFF, 16)); 145 } 146 147 public void mousePressed(MouseEvent e){} 148 public void mouseReleased(MouseEvent e){} 149 public void mouseEntered(MouseEvent e){} 150 public void mouseExited(MouseEvent e){} 151 152 156 157 public Dimension prefferedSize() 158 { 159 return new Dimension(0xfffff, 0xffff); } 161 162 public int getWidth(){return (int)(width+width2);}; 163 public int getHeight(){return (int)height;}; 164 165 public static void main(String args[]) 166 { 167 Frame frame = new Frame(); 168 frame.setLayout(new GridLayout()); 169 frame.add(new ColorPicker()); 170 frame.show(); 171 frame.pack(); 172 } 173 } 174
| Popular Tags
|