Would be very helpful if someone knows of such a program.
You can just open any image editing program, use its color slider and divide the rgb values by 255 and get the float values, if that’s what you’re looking for.
Thank you! Didn’t know it was that simple!
Or DIY:
import javax.swing.*;
import javax.swing.event.*;
public class NeedSomeColors {
public static void main(String[] a) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JColorChooser jcc = new JColorChooser();
frame.add(jcc);
jcc.getSelectionModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
float[] rgba = jcc.getSelectionModel().getSelectedColor().getRGBComponents(null);
frame.setTitle(rgba[0] + " " + rgba[1] + " " + rgba[2]);
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Very nice and useful example code!
I know it was just a short example, but just a quick edit in order to:
- make it compile under pre-Java 1.8 (using
final
for locals closed over anonymous classes; remove@Override
for interface implementations) - add JTextField, which has focus all the time, to be able to Ctrl+C copy/paste the RGB values
- format the RGB values with ‘f’ suffix and commas in-between values, to be inserted as arguments in common methods
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class NeedSomeColors {
public static void main(String[] a) {
final JFrame frame = new JFrame("Press Ctrl+C to copy RGB into clipboard");
frame.setLayout(new BorderLayout());
final JTextField tf = new JTextField();
tf.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if ((e.getModifiers() & KeyEvent.CTRL_MASK) != 0 && e.getKeyCode() == KeyEvent.VK_C) {
String rgb = tf.getText();
StringSelection stringSelection = new StringSelection(rgb);
Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard();
clpbrd.setContents(stringSelection, null);
frame.setTitle("Copied '" + rgb + "' into clipboard");
e.consume();
}
}
});
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
final JColorChooser jcc = new JColorChooser();
frame.add(jcc, BorderLayout.CENTER);
frame.add(tf, BorderLayout.NORTH);
jcc.getSelectionModel().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
float[] rgba = jcc.getSelectionModel().getSelectedColor().getRGBComponents(null);
tf.setText(rgba[0] + "f, " + rgba[1] + "f, " + rgba[2] + "f");
}
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}