-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorimeterFrame.java
181 lines (138 loc) · 5.35 KB
/
ColorimeterFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**************************************************************
* Colorimeter was inspired by the built-in Mac application
* DigitalColor Meter.
*
* Ian Burns - 8/31/2013
**************************************************************/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class ColorimeterFrame extends JFrame {
private ColorimeterPanel colorimeterPanel;
public ColorimeterFrame() {
super("Default");
}
public ColorimeterFrame(String s) {
super(s);
setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(340, 185);
setResizable(false);
colorimeterPanel = new ColorimeterPanel();
setContentPane(colorimeterPanel);
setLocationRelativeTo(null);
colorimeterPanel.repaint();
setVisible(true);
}
public void refresh() {
colorimeterPanel.repaint();
}
public void setImage(BufferedImage image) {
colorimeterPanel.setImage(image);
}
public void setColor(Color color) {
colorimeterPanel.setColor(color);
}
public void setColorValues(int value) {
colorimeterPanel.setColorValues(value);
}
public Color getColor() {
return colorimeterPanel.getColor();
}
public boolean useFloats() {
return colorimeterPanel.useFloats();
}
private class CopyAction extends AbstractAction {
public CopyAction() {
super();
}
public void actionPerformed(ActionEvent e) {
Colorimeter.copyColorToClipboard();
}
}
private class ColorimeterPanel extends JPanel {
private Color color;
private BufferedImage image;
private int colorValues;
private CopyAction copyAction;
private JRadioButton floatButton;
public ColorimeterPanel() {
// add key bindings for ctl-c and cmd-c
copyAction = new CopyAction();
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.CTRL_DOWN_MASK), "copy");
getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, KeyEvent.META_DOWN_MASK), "copy");
getActionMap().put("copy", copyAction);
// setup the rest of the panel
setLayout(null);
setBackground(Color.LIGHT_GRAY);
JLabel scaleLabel = new JLabel("Scale for RGB values:");
floatButton = new JRadioButton("0.0 - 1.0");
JRadioButton intButton = new JRadioButton("0 - 255");
ButtonGroup group = new ButtonGroup();
group.add(floatButton);
group.add(intButton);
intButton.setBounds(200, 25, 100, 25);
floatButton.setBounds(200, 45, 100, 25);
scaleLabel.setBounds(180, 5, 200, 25);
intButton.setSelected(true);
add(floatButton);
add(intButton);
add(scaleLabel);
setSize(340, 185);
setFocusable(true);
requestFocusInWindow();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// make sure the panel has focus so that the keybindings work
grabFocus();
if (image != null) {
g.drawImage(image, 10, 6, 150, 150, null);
}
g.setColor(Color.WHITE);
g.draw3DRect(185, 80, 75, 75, true);
if (color != null) {
g.setColor(color);
} else {
g.setColor(Color.WHITE);
}
g.fill3DRect(185, 80, 75, 75, true);
g.setColor(Color.BLACK);
g.drawRect(81, 81, 6, 6);
if (color != null) {
switch (colorValues) {
case 0:
g.drawString("R: " + color.getRed(), 275, 100);
g.drawString("G: " + color.getGreen(), 274, 125);
g.drawString("B: " + color.getBlue(), 275, 150);
break;
case 1:
g.drawString(String.format("R: %.4f", (color.getRed() / 255.0)), 275, 100);
g.drawString(String.format("G: %.4f", (color.getGreen() / 255.0)), 274, 125);
g.drawString(String.format("B: %.4f", (color.getBlue() / 255.0)), 275, 150);
break;
}
} else {
g.drawString("R: ", 275, 100);
g.drawString("G: ", 274, 125);
g.drawString("B: ", 275, 150);
}
}
public boolean useFloats() {
return this.floatButton.isSelected();
}
public void setColor(Color color) {
this.color = color;
}
public Color getColor() {
return this.color;
}
public void setImage(BufferedImage image) {
this.image = image;
}
public void setColorValues(int value) {
this.colorValues = value;
}
}
}