Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
Colors.java
1 package ij.plugin;
2 import ij.*;
3 import ij.gui.*;
4 import ij.process.*;
5 import ij.io.*;
6 import ij.plugin.filter.*;
7 import java.awt.*;
8 import java.awt.event.*;
9 import java.util.Vector;
10 
12 public class Colors implements PlugIn, ItemListener {
13  private String[] colors = {"red","green","blue","magenta ","cyan","yellow","orange","black","white"};
14  private Choice fchoice, bchoice, schoice;
15  private Color fc2, bc2, sc2;
16 
17  public void run(String arg) {
18  showDialog();
19  }
20 
21  void showDialog() {
22  Color fc =Toolbar.getForegroundColor();
23  String fname = getColorName(fc, "black");
24  Color bc =Toolbar.getBackgroundColor();
25  String bname = getColorName(bc, "white");
26  Color sc =Roi.getColor();
27  String sname = getColorName(sc, "yellow");
28  GenericDialog gd = new GenericDialog("Colors");
29  gd.addChoice("Foreground:", colors, fname);
30  gd.addChoice("Background:", colors, bname);
31  gd.addChoice("Selection:", colors, sname);
32  Vector choices = gd.getChoices();
33  fchoice = (Choice)choices.elementAt(0);
34  bchoice = (Choice)choices.elementAt(1);
35  schoice = (Choice)choices.elementAt(2);
36  fchoice.addItemListener(this);
37  bchoice.addItemListener(this);
38  schoice.addItemListener(this);
39 
40  gd.showDialog();
41  if (gd.wasCanceled()) {
42  if (fc2!=fc) Toolbar.setForegroundColor(fc);
43  if (bc2!=bc) Toolbar.setBackgroundColor(bc);
44  if (sc2!=sc) {
45  Roi.setColor(sc);
46  ImagePlus imp = IJ.getInstance().getImagePlus();
47  if (imp!=null && imp.getRoi()!=null) imp.draw();
48  }
49  return;
50  }
51  fname = gd.getNextChoice();
52  bname = gd.getNextChoice();
53  sname = gd.getNextChoice();
54  fc2 = getColor(fname, Color.black);
55  bc2 = getColor(bname, Color.white);
56  sc2 = getColor(sname, Color.yellow);
57  if (fc2!=fc) Toolbar.setForegroundColor(fc2);
58  if (bc2!=bc) Toolbar.setBackgroundColor(bc2);
59  if (sc2!=sc) {
60  Roi.setColor(sc2);
61  ImagePlus imp = IJ.getInstance().getImagePlus();
62  if (imp!=null) imp.draw();
63  }
64  }
65 
66  String getColorName(Color c, String defaultName) {
67  String name = defaultName;
68  if (c.equals(Color.red)) name = colors[0];
69  else if (c.equals(Color.green)) name = colors[1];
70  else if (c.equals(Color.blue)) name = colors[2];
71  else if (c.equals(Color.magenta)) name = colors[3];
72  else if (c.equals(Color.cyan)) name = colors[4];
73  else if (c.equals(Color.yellow)) name = colors[5];
74  else if (c.equals(Color.orange)) name = colors[6];
75  else if (c.equals(Color.black)) name = colors[7];
76  else if (c.equals(Color.white)) name = colors[8];
77  return name;
78  }
79 
80  Color getColor(String name, Color defaultColor) {
81  Color c = defaultColor;
82  if (name.equals(colors[0])) c = Color.red;
83  else if (name.equals(colors[1])) c = Color.green;
84  else if (name.equals(colors[2])) c = Color.blue;
85  else if (name.equals(colors[3])) c = Color.magenta;
86  else if (name.equals(colors[4])) c = Color.cyan;
87  else if (name.equals(colors[5])) c = Color.yellow;
88  else if (name.equals(colors[6])) c = Color.orange;
89  else if (name.equals(colors[7])) c = Color.black;
90  else if (name.equals(colors[8])) c = Color.white;
91  return c;
92  }
93 
94  public void itemStateChanged(ItemEvent e) {
95  Choice choice = (Choice)e.getSource();
96  String item = choice.getSelectedItem();
97  Color color = getColor(item, Color.black);
98  if (choice==fchoice)
99  Toolbar.setForegroundColor(color);
100  else if (choice==bchoice)
101  Toolbar.setBackgroundColor(color);
102  else if (choice==schoice) {
103  Roi.setColor(color);
104  ImagePlus imp = IJ.getInstance().getImagePlus();
105  if (imp!=null && imp.getRoi()!=null) imp.draw();
106  }
107  }
108 
109 }