Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ColorPicker.java
1 package ij.plugin;
2 import ij.*;
3 //import ij.plugin.*;
4 import ij.plugin.frame.*;
5 import java.awt.*;
6 import java.awt.event.*;
7 import java.util.Vector;
8 import ij.process.*;
9 import ij.gui.*;
10 import javax.swing.*;
11 
13 public class ColorPicker extends ImagePlus implements PlugIn {
14  static int id;
15  private static JFrame instance;
16 
17  public void run(String arg) {
18  int colorWidth = 22;
19  int colorHeight = 16;
20  int columns = 5;
21  int rows = 20;
22  int width = columns*colorWidth;
23  int height = rows*colorHeight;
24  ColorGenerator cg = new ColorGenerator(width, height, new int[width*height], this);
25  cg.drawColors(colorWidth, colorHeight, columns, rows);
26  setProcessor("CP", cg);
27  id = getID();
28  show();
29  IJ.register(ColorPicker.class);
30  }
31 
33  public void show() {
34  if (img==null && ip!=null)
35  img = ip.createImage();
36  draw();
37  IJ.showStatus("");
38  }
39 }
40 
41 class ColorGenerator extends ColorProcessor {
42  int w, h;
43  ImagePlus imp;
44  int[] colors = {0xff0000, 0x00ff00, 0x0000ff, 0xffffff, 0x00ffff, 0xff00ff, 0xffff00, 0x000000};
45 
46  public ColorGenerator(int width, int height, int[] pixels, ImagePlus imp) {
47  super(width, height, pixels);
48  this.imp = imp;
49  }
50  void drawColors(int colorWidth, int colorHeight, int columns, int rows) {
51  w = colorWidth;
52  h = colorHeight;
53  setColor(0xffffff);
54  setRoi(0, 0, 110, 320);
55  fill();
56  drawRamp();
57  resetBW();
58  flipper();
59  drawLine(0, 256, 110, 256);
60 
61  int x = 1;
62  int y = 0;
63  refreshBackground();
64  refreshForeground();
65 
66  Color c;
67  float hue, saturation=1f, brightness=1f;
68  double w=colorWidth, h=colorHeight;
69  for ( x=2; x<10; x++) {
70  for ( y=0; y<32; y++) {
71  hue = (float)(y/(2*h)-.15);
72  if (x<6) {
73  saturation = 1f;
74  brightness = (float)(x*4/w);
75  } else {
76  saturation = 1f - ((float)((5-x)*-4/w));
77  brightness = 1f;
78  }
79  c = Color.getHSBColor(hue, saturation, brightness);
80  setRoi(x*(int)(w/2), y*(int)(h/2), (int)w/2, (int)h/2);
81  setColor(c);
82  fill();
83  }
84  }
85  drawSpectrum(h);
86  resetRoi();
87  }
88 
89  void drawColor(int x, int y, Color c)
90  {
91  setRoi(x*w, y*h, w, h);
92  setColor(c);
93  fill();
94  }
95 
96  public void refreshBackground() {
97  //Boundary for Background Selection
98  setColor(0x444444);
99  drawRect((w*2)-12, 276, (w*2)+4, (h*2)+4);
100  setColor(0x999999);
101  drawRect((w*2)-11, 277, (w*2)+2, (h*2)+2);
102  setRoi((w*2)-10, 278, w*2, h*2);//Paints the Background Color
103  setColor(Toolbar.getBackgroundColor());
104  fill();
105  imp.updateAndDraw();
106  }
107 
108  public void refreshForeground() {
109  //Boundary for Foreground Selection
110  setColor(0x444444);
111  drawRect(8, 266, (w*2)+4, (h*2)+4);
112  setColor(0x999999);
113  drawRect(9, 267, (w*2)+2, (h*2)+2);
114  setRoi(10, 268, w*2, h*2); //Paints the Foreground Color
115  setColor(Toolbar.getForegroundColor());
116  fill();
117  imp.updateAndDraw();
118  }
119 
120  void drawSpectrum(double h) {
121  Color c;
122  for ( int x=5; x<7; x++) {
123  for ( int y=0; y<32; y++) {
124  float hue = (float)(y/(2*h)-.15);
125  c = Color.getHSBColor(hue, 1f, 1f);
126  setRoi(x*(int)(w/2), y*(int)(h/2), (int)w/2, (int)h/2);
127  setColor(c);
128  fill();
129  }
130  }
131  setRoi(55, 32, 22, 16); //Solid red
132  setColor(0xff0000);
133  fill();
134  setRoi(55, 120, 22, 16); //Solid green
135  setColor(0x00ff00);
136  fill();
137  setRoi(55, 208, 22, 16); //Solid blue
138  setColor(0x0000ff);
139  fill();
140  setRoi(55, 80, 22, 8); //Solid yellow
141  setColor(0xffff00);
142  fill();
143  setRoi(55, 168, 22, 8); //Solid cyan
144  setColor(0x00ffff);
145  fill();
146  setRoi(55, 248, 22, 8); //Solid magenta
147  setColor(0xff00ff);
148  fill();
149  }
150 
151  void drawRamp() {
152  int r,g,b;
153  for (int x=0; x<w; x++) {
154  for (double y=0; y<(h*16); y++) {
155  r = g = b = (byte)y;
156  pixels[(int)y*width+x] = 0xff000000 | ((r<<16)&0xff0000) | ((g<<8)&0xff00) | (b&0xff);
157  }
158  }
159  }
160 
161  void resetBW() { //Paints the Color Reset Button
162  setColor(0x000000);
163  drawRect(92, 300, 9, 7);
164  setColor(0x000000);
165  setRoi(88, 297, 9, 7);
166  fill();
167  }
168 
169  void flipper() { //Paints the Flipper Button
170  int xa = 90;
171  int ya = 272;
172  setColor(0x000000);
173  drawLine(xa, ya, xa+9, ya+9);//Main Body
174  drawLine(xa+1, ya, xa+9, ya+8);
175  drawLine(xa, ya+1, xa+8, ya+9);
176  drawLine(xa, ya, xa, ya+5);//Upper Arrow
177  drawLine(xa+1, ya+1, xa+1, ya+6);
178  drawLine(xa, ya, xa+5, ya);
179  drawLine(xa+1, ya+1, xa+6, ya+1);
180  drawLine(xa+9, ya+9, xa+9, ya+4);//Lower Arrow
181  drawLine(xa+8, ya+8, xa+8, ya+3);
182  drawLine(xa+9, ya+9, xa+4, ya+9);
183  drawLine(xa+8, ya+8, xa+3, ya+8);
184  }
185 }
186 
187 class ColorCanvas extends ImageCanvas {
188 
189  Vector colors;
190  boolean background = false;
191  long mouseDownTime;
192 
193  public ColorCanvas(ImagePlus imp) {
194  super(imp);
195  }
196 
197  public void mousePressed(MouseEvent e) {
198  //super.mousePressed(e);
199  ImageProcessor ip = imp.getProcessor();
200  ip.setLineWidth(1);
201 
202  Rectangle flipperRect = new Rectangle(86, 268, 18, 18);
203  Rectangle resetRect = new Rectangle(86, 294, 18, 18);
204  Rectangle foreground1Rect = new Rectangle(9, 266, 45, 10);
205  Rectangle foreground2Rect = new Rectangle(9, 276, 23, 25);
206  Rectangle background1Rect = new Rectangle(33, 302, 45, 10);
207  Rectangle background2Rect = new Rectangle(56, 277, 23, 25);
208  int x = offScreenX(e.getX());
209  int y = offScreenY(e.getY());
210  long difference = System.currentTimeMillis()-mouseDownTime;
211  boolean doubleClick = (difference<=250);
212  mouseDownTime = System.currentTimeMillis();
213  if (flipperRect.contains(x, y)) {
214  Color c = Toolbar.getBackgroundColor();
215  Toolbar.setBackgroundColor(Toolbar.getForegroundColor());
216  Toolbar.setForegroundColor(c);
217  } else if(resetRect.contains(x,y)) {
218  Toolbar.setForegroundColor(new Color(0x000000));
219  Toolbar.setBackgroundColor(new Color(0xffffff));
220  } else if ((background1Rect.contains(x,y)) || (background2Rect.contains(x,y))) {
221  background = true;
222  if (doubleClick) editColor();
223  ((ColorGenerator)ip).refreshForeground();
224  ((ColorGenerator)ip).refreshBackground();
225  } else if ((foreground1Rect.contains(x,y)) || (foreground2Rect.contains(x,y))) {
226  background = false;
227  if (doubleClick) editColor();
228  ((ColorGenerator)ip).refreshBackground();
229  ((ColorGenerator)ip).refreshForeground();
230  } else {
231  //IJ.log(" " + difference + " " + doubleClick);
232  if (doubleClick)
233  editColor();
234  else {
235  setDrawingColor(offScreenX(e.getX()), offScreenY(e.getY()), background);
236  }
237  }
238  if (ip instanceof ColorGenerator) {
239  if (background){
240  ((ColorGenerator)ip).refreshForeground();
241  ((ColorGenerator)ip).refreshBackground();
242  } else {
243  ((ColorGenerator)ip).refreshBackground();
244  ((ColorGenerator)ip).refreshForeground();
245  }
246  }
247  }
248 
249  void editColor() {
250  Color c = background?Toolbar.getBackgroundColor():Toolbar.getForegroundColor();
251  ColorChooser cc = new ColorChooser((background?"Background":"Foreground")+" Color", c, false);
252  c = cc.getColor();
253  if (background)
254  Toolbar.setBackgroundColor(c);
255  else
256  Toolbar.setForegroundColor(c);
257  }
258 
259  public void refreshColors() {
260  ImageProcessor ip = imp.getProcessor();
261  ((ColorGenerator)ip).refreshBackground();
262  ((ColorGenerator)ip).refreshForeground();
263  }
264 }
265