Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
ColorChooser.java
1 package ij.gui;
2 import ij.*;
3 import ij.process.*;
4 import ij.util.*;
5 import java.awt.*;
6 import java.util.Vector;
7 import java.awt.event.*;
8 
9 
11 public class ColorChooser implements TextListener, AdjustmentListener {
12  Vector colors, sliders;
13  ColorPanel panel;
14  Color initialColor;
15  int red, green, blue;
16  boolean useHSB;
17  String title;
18  boolean mono;
19 
21  public ColorChooser(String title, Color initialColor, boolean useHSB) {
22  this.title = title;
23  this.initialColor = initialColor;
24  red = initialColor.getRed();
25  green = initialColor.getGreen();
26  blue = initialColor.getBlue();
27  mono = red==green && green==blue;
28  this.useHSB = useHSB;
29  }
30 
32  public Color getColor() {
33  GenericDialog gd = new GenericDialog(title);
34  gd.addSlider("Red:", 0, 255, red);
35  gd.addSlider("Green:", 0, 255, green);
36  gd.addSlider("Blue:", 0, 255, blue);
37  panel = new ColorPanel(initialColor);
38  gd.addPanel(panel, GridBagConstraints.CENTER, new Insets(10, 0, 0, 0));
39  colors = gd.getNumericFields();
40  for (int i=0; i<colors.size(); i++)
41  ((TextField)colors.elementAt(i)).addTextListener(this);
42  sliders = gd.getSliders();
43  for (int i=0; i<sliders.size(); i++)
44  ((Scrollbar)sliders.elementAt(i)).addAdjustmentListener(this);
45  gd.showDialog();
46  if (gd.wasCanceled()) return null;
47  int red = (int)gd.getNextNumber();
48  int green = (int)gd.getNextNumber();
49  int blue = (int)gd.getNextNumber();
50  return new Color(red, green, blue);
51  }
52 
53  public void textValueChanged(TextEvent e) {
54  int red = (int)Tools.parseDouble(((TextField)colors.elementAt(0)).getText());
55  int green = (int)Tools.parseDouble(((TextField)colors.elementAt(1)).getText());
56  int blue = (int)Tools.parseDouble(((TextField)colors.elementAt(2)).getText());
57  panel.setColor(new Color(red, green, blue));
58  panel.repaint();
59  }
60 
61  public synchronized void adjustmentValueChanged(AdjustmentEvent e) {
62  Object source = e.getSource();
63  for (int i=0; i<sliders.size(); i++) {
64  if (source==sliders.elementAt(i)) {
65  Scrollbar sb = (Scrollbar)source;
66  TextField tf = (TextField)colors.elementAt(i);
67  if (i==0 && mono) { // red
68  String red = tf.getText();
69  TextField tf1 = (TextField)colors.elementAt(1);
70  TextField tf2 = (TextField)colors.elementAt(2);
71  tf1.setText(red);
72  tf2.setText(red);
73  }
74  if (i!=0) mono = false;
75  }
76  }
77  }
78 
79 }
80 
81 class ColorPanel extends Panel {
82  static final int WIDTH=100, HEIGHT=50;
83  Color c;
84 
85  ColorPanel(Color c) {
86  this.c = c;
87  }
88 
89  public Dimension getPreferredSize() {
90  return new Dimension(WIDTH, HEIGHT);
91  }
92 
93  void setColor(Color c) {
94  this.c = c;
95  }
96 
97  public Dimension getMinimumSize() {
98  return new Dimension(WIDTH, HEIGHT);
99  }
100 
101  public void paint(Graphics g) {
102  g.setColor(c);
103  g.fillRect(0, 0, WIDTH, HEIGHT);
104  g.setColor(Color.black);
105  g.drawRect(0, 0, WIDTH-1, HEIGHT-1);
106  }
107 
108 }