mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
getting closer on color picker
This commit is contained in:
@@ -403,7 +403,7 @@ public class Preferences {
|
||||
|
||||
pain.addKeyListener(new KeyAdapter() {
|
||||
public void keyPressed(KeyEvent e) {
|
||||
System.out.println(e);
|
||||
//System.out.println(e);
|
||||
KeyStroke wc = Editor.WINDOW_CLOSE_KEYSTROKE;
|
||||
if ((e.getKeyCode() == KeyEvent.VK_ESCAPE) ||
|
||||
(KeyStroke.getKeyStrokeForEvent(e).equals(wc))) {
|
||||
|
||||
+239
-23
@@ -26,14 +26,17 @@ import processing.app.*;
|
||||
import processing.core.*;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.*;
|
||||
import javax.swing.event.*;
|
||||
import javax.swing.text.*;
|
||||
|
||||
|
||||
public class ColorPicker {
|
||||
public class ColorPicker implements DocumentListener {
|
||||
|
||||
Editor editor;
|
||||
JFrame frame;
|
||||
|
||||
int hue, saturation, brightness; // 360, 100, 100
|
||||
int red, green, blue; // 256, 256, 256
|
||||
@@ -44,16 +47,15 @@ public class ColorPicker {
|
||||
JTextField hueField, saturationField, brightnessField;
|
||||
JTextField redField, greenField, blueField;
|
||||
|
||||
JTextField hexField;
|
||||
|
||||
JPanel colorPanel;
|
||||
|
||||
|
||||
public ColorPicker(Editor editor) {
|
||||
this.editor = editor;
|
||||
}
|
||||
|
||||
|
||||
public void show() {
|
||||
JFrame frame = new JFrame("Color Picker");
|
||||
frame = new JFrame("Color Picker");
|
||||
frame.getContentPane().setLayout(new BorderLayout());
|
||||
|
||||
Box box = Box.createHorizontalBox();
|
||||
@@ -65,6 +67,7 @@ public class ColorPicker {
|
||||
rangePanel.setLayout(new BorderLayout());
|
||||
rangePanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
|
||||
rangePanel.add(range, BorderLayout.CENTER);
|
||||
//range.setSize(256, 256);
|
||||
box.add(rangePanel);
|
||||
box.add(Box.createHorizontalStrut(10));
|
||||
|
||||
@@ -73,19 +76,182 @@ public class ColorPicker {
|
||||
JPanel sliderPanel = new JPanel();
|
||||
sliderPanel.setLayout(new BorderLayout());
|
||||
sliderPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
|
||||
//slider.setSize(256, 20);
|
||||
sliderPanel.add(slider, BorderLayout.CENTER);
|
||||
box.add(sliderPanel);
|
||||
box.add(Box.createHorizontalStrut(10));
|
||||
|
||||
/*
|
||||
JPanel fieldPanel = new JPanel();
|
||||
fieldPanel.setLayout(new BorderLayout());
|
||||
fieldPanel.add(createColorFields(), BorderLayout.CENTER);
|
||||
fieldPanel.doLayout();
|
||||
box.add(fieldPanel);
|
||||
*/
|
||||
box.add(createColorFields());
|
||||
box.add(Box.createHorizontalStrut(10));
|
||||
|
||||
frame.getContentPane().add(box, BorderLayout.CENTER);
|
||||
frame.pack();
|
||||
frame.setResizable(false);
|
||||
//frame.setResizable(false);
|
||||
|
||||
Dimension size = frame.getSize();
|
||||
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
|
||||
frame.setLocation((screen.width - size.width) / 2,
|
||||
(screen.height - size.height) / 2);
|
||||
|
||||
// handle window closing commands for ctrl/cmd-W or hitting ESC.
|
||||
/*
|
||||
frame.addKeyListener(new KeyAdapter() {
|
||||
public void keyPressed(KeyEvent e) {
|
||||
System.out.println(e);
|
||||
KeyStroke wc = Editor.WINDOW_CLOSE_KEYSTROKE;
|
||||
if ((e.getKeyCode() == KeyEvent.VK_ESCAPE) ||
|
||||
(KeyStroke.getKeyStrokeForEvent(e).equals(wc))) {
|
||||
//disposeFrame();
|
||||
//frame.dispose();
|
||||
System.out.println("close me");
|
||||
}
|
||||
}
|
||||
});
|
||||
*/
|
||||
|
||||
hueField.getDocument().addDocumentListener(this);
|
||||
saturationField.getDocument().addDocumentListener(this);
|
||||
brightnessField.getDocument().addDocumentListener(this);
|
||||
redField.getDocument().addDocumentListener(this);
|
||||
greenField.getDocument().addDocumentListener(this);
|
||||
blueField.getDocument().addDocumentListener(this);
|
||||
hexField.getDocument().addDocumentListener(this);
|
||||
}
|
||||
|
||||
|
||||
public void show() {
|
||||
frame.show();
|
||||
}
|
||||
|
||||
|
||||
public void changedUpdate(DocumentEvent e) {
|
||||
//System.out.println("changed");
|
||||
}
|
||||
|
||||
public void removeUpdate(DocumentEvent e) {
|
||||
//System.out.println("remove");
|
||||
}
|
||||
|
||||
|
||||
boolean updating;
|
||||
|
||||
public void insertUpdate(DocumentEvent e) {
|
||||
if (updating) return; // don't update forever recursively
|
||||
updating = true;
|
||||
//System.out.println(e);
|
||||
|
||||
Document doc = e.getDocument();
|
||||
if (doc == hueField.getDocument()) {
|
||||
hue = bounded(hue, hueField, 359);
|
||||
updateRGB();
|
||||
updateHex();
|
||||
|
||||
} else if (doc == saturationField.getDocument()) {
|
||||
saturation = bounded(saturation, saturationField, 99);
|
||||
updateRGB();
|
||||
updateHex();
|
||||
|
||||
} else if (doc == brightnessField.getDocument()) {
|
||||
brightness = bounded(brightness, brightnessField, 99);
|
||||
updateRGB();
|
||||
updateHex();
|
||||
|
||||
} else if (doc == redField.getDocument()) {
|
||||
red = bounded(red, redField, 255);
|
||||
updateHSB();
|
||||
updateHex();
|
||||
|
||||
} else if (doc == greenField.getDocument()) {
|
||||
green = bounded(green, greenField, 255);
|
||||
updateHSB();
|
||||
updateHex();
|
||||
|
||||
} else if (doc == blueField.getDocument()) {
|
||||
blue = bounded(blue, blueField, 255);
|
||||
updateHSB();
|
||||
updateHex();
|
||||
|
||||
} else if (doc == hexField.getDocument()) {
|
||||
|
||||
}
|
||||
range.redraw();
|
||||
slider.redraw();
|
||||
colorPanel.repaint();
|
||||
updating = false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the RGB values based on the current HSB values.
|
||||
*/
|
||||
protected void updateRGB() {
|
||||
int rgb = Color.HSBtoRGB((float)hue / 359f,
|
||||
(float)saturation / 99f,
|
||||
(float)brightness / 99f);
|
||||
red = (rgb >> 16) & 0xff;
|
||||
green = (rgb >> 8) & 0xff;
|
||||
blue = rgb & 0xff;
|
||||
|
||||
redField.setText(String.valueOf(red));
|
||||
greenField.setText(String.valueOf(green));
|
||||
blueField.setText(String.valueOf(blue));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set the HSB values based on the current RGB values;
|
||||
*/
|
||||
protected void updateHSB() {
|
||||
float hsb[] = new float[3];
|
||||
Color.RGBtoHSB(red, green, blue, hsb);
|
||||
|
||||
hue = (int) (hsb[0] * 359.0f);
|
||||
saturation = (int) (hsb[1] * 99.0f);
|
||||
brightness = (int) (hsb[2] * 99.0f);
|
||||
|
||||
hueField.setText(String.valueOf(hue));
|
||||
saturationField.setText(String.valueOf(saturation));
|
||||
brightnessField.setText(String.valueOf(brightness));
|
||||
}
|
||||
|
||||
|
||||
protected void updateHex() {
|
||||
hexField.setText(PApplet.hex(red, 2) +
|
||||
PApplet.hex(green, 2) +
|
||||
PApplet.hex(blue, 2));
|
||||
}
|
||||
|
||||
|
||||
protected int bounded(int current, final JTextField field, final int max) {
|
||||
String text = field.getText();
|
||||
if (text.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
int value = Integer.parseInt(text);
|
||||
if (value > max) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
field.setText(String.valueOf(max));
|
||||
}
|
||||
});
|
||||
return max;
|
||||
}
|
||||
return value;
|
||||
|
||||
} catch (NumberFormatException e) {
|
||||
return current; // should not be reachable
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected Container createColorFields() {
|
||||
//JLabel label = new JLabel();
|
||||
//int labelH = label.getPreferredSize().height;
|
||||
@@ -101,7 +267,7 @@ public class ColorPicker {
|
||||
}
|
||||
};
|
||||
colorPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
|
||||
Dimension dim = new Dimension(60, 30);
|
||||
Dimension dim = new Dimension(60, 60);
|
||||
colorPanel.setMinimumSize(dim);
|
||||
colorPanel.setMaximumSize(dim);
|
||||
colorPanel.setPreferredSize(dim);
|
||||
@@ -112,22 +278,25 @@ public class ColorPicker {
|
||||
|
||||
row = Box.createHorizontalBox();
|
||||
row.add(createFixedLabel("H:"));
|
||||
row.add(hueField = new NumberField(4));
|
||||
row.add(hueField = new NumberField(4, false));
|
||||
row.add(new JLabel(" \u00B0")); // degree symbol
|
||||
row.add(Box.createHorizontalGlue());
|
||||
box.add(row);
|
||||
box.add(Box.createVerticalStrut(5));
|
||||
|
||||
row = Box.createHorizontalBox();
|
||||
row.add(createFixedLabel("S:"));
|
||||
row.add(saturationField = new NumberField(4));
|
||||
row.add(saturationField = new NumberField(4, false));
|
||||
row.add(new JLabel(" %"));
|
||||
row.add(Box.createHorizontalGlue());
|
||||
box.add(row);
|
||||
box.add(Box.createVerticalStrut(5));
|
||||
|
||||
row = Box.createHorizontalBox();
|
||||
row.add(createFixedLabel("B:"));
|
||||
row.add(brightnessField = new NumberField(4));
|
||||
row.add(brightnessField = new NumberField(4, false));
|
||||
row.add(new JLabel(" %"));
|
||||
row.add(Box.createHorizontalGlue());
|
||||
box.add(row);
|
||||
box.add(Box.createVerticalStrut(10));
|
||||
|
||||
@@ -135,24 +304,34 @@ public class ColorPicker {
|
||||
|
||||
row = Box.createHorizontalBox();
|
||||
row.add(createFixedLabel("R:"));
|
||||
row.add(redField = new NumberField(4));
|
||||
row.add(redField = new NumberField(4, false));
|
||||
row.add(Box.createHorizontalGlue());
|
||||
box.add(row);
|
||||
box.add(Box.createVerticalStrut(5));
|
||||
|
||||
row = Box.createHorizontalBox();
|
||||
row.add(createFixedLabel("G:"));
|
||||
row.add(greenField = new NumberField(4));
|
||||
row.add(greenField = new NumberField(4, false));
|
||||
row.add(Box.createHorizontalGlue());
|
||||
box.add(row);
|
||||
box.add(Box.createVerticalStrut(5));
|
||||
|
||||
row = Box.createHorizontalBox();
|
||||
row.add(createFixedLabel("B:"));
|
||||
row.add(blueField = new NumberField(4));
|
||||
row.add(blueField = new NumberField(4, false));
|
||||
row.add(Box.createHorizontalGlue());
|
||||
box.add(row);
|
||||
box.add(Box.createVerticalStrut(10));
|
||||
|
||||
//
|
||||
|
||||
row = Box.createHorizontalBox();
|
||||
row.add(createFixedLabel("#"));
|
||||
row.add(hexField = new NumberField(7, true));
|
||||
row.add(Box.createHorizontalGlue());
|
||||
box.add(row);
|
||||
box.add(Box.createVerticalStrut(10));
|
||||
|
||||
box.add(Box.createVerticalGlue());
|
||||
return box;
|
||||
}
|
||||
@@ -173,12 +352,10 @@ public class ColorPicker {
|
||||
return label;
|
||||
}
|
||||
|
||||
/*
|
||||
public void setFixed(int what) {
|
||||
fixedColor = what;
|
||||
range.redraw();
|
||||
|
||||
public void updateFields(NumberField field) {
|
||||
System.out.println("update based on"); // " + field);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
public class ColorRange extends PApplet {
|
||||
@@ -225,6 +402,10 @@ public class ColorPicker {
|
||||
}
|
||||
}
|
||||
|
||||
public Dimension PreferredSize() {
|
||||
return new Dimension(WIDE, HIGH);
|
||||
}
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return new Dimension(WIDE, HIGH);
|
||||
}
|
||||
@@ -276,6 +457,10 @@ public class ColorPicker {
|
||||
}
|
||||
}
|
||||
|
||||
public Dimension PreferredSize() {
|
||||
return new Dimension(WIDE, HIGH);
|
||||
}
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return new Dimension(WIDE, HIGH);
|
||||
}
|
||||
@@ -291,20 +476,43 @@ public class ColorPicker {
|
||||
*/
|
||||
class NumberField extends JTextField {
|
||||
|
||||
public NumberField(int cols) {
|
||||
public boolean allowHex;
|
||||
|
||||
public NumberField(int cols, boolean allowHex) {
|
||||
super(cols);
|
||||
this.allowHex = allowHex;
|
||||
}
|
||||
|
||||
protected Document createDefaultModel() {
|
||||
return new NumberDocument();
|
||||
return new NumberDocument(this);
|
||||
}
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(35, super.getPreferredSize().height);
|
||||
if (!allowHex) {
|
||||
return new Dimension(35, super.getPreferredSize().height);
|
||||
}
|
||||
return super.getPreferredSize();
|
||||
}
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class NumberDocument extends PlainDocument {
|
||||
|
||||
NumberField parentField;
|
||||
|
||||
public NumberDocument(NumberField parentField) {
|
||||
this.parentField = parentField;
|
||||
//System.out.println("setting parent to " + parentPicker);
|
||||
}
|
||||
|
||||
public void insertString(int offs, String str, AttributeSet a)
|
||||
throws BadLocationException {
|
||||
|
||||
@@ -314,7 +522,12 @@ public class ColorPicker {
|
||||
int charCount = 0;
|
||||
// remove any non-digit chars
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
if (Character.isDigit(chars[i])) {
|
||||
boolean ok = Character.isDigit(chars[i]);
|
||||
if (parentField.allowHex) {
|
||||
if ((chars[i] >= 'A') && (chars[i] <= 'F')) ok = true;
|
||||
if ((chars[i] >= 'a') && (chars[i] <= 'f')) ok = true;
|
||||
}
|
||||
if (ok) {
|
||||
if (charCount != i) { // shift if necessary
|
||||
chars[charCount] = chars[i];
|
||||
}
|
||||
@@ -322,6 +535,9 @@ public class ColorPicker {
|
||||
}
|
||||
}
|
||||
super.insertString(offs, new String(chars, 0, charCount), a);
|
||||
//System.out.println(parentField + " " + parentPicker);
|
||||
//parentPicker.updateFields(parentField);
|
||||
//someMethod();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -279,6 +279,8 @@ public class PGraphics3 extends PGraphics {
|
||||
|
||||
|
||||
protected void allocate() {
|
||||
//System.out.println("allocating for " + width + " " + height);
|
||||
|
||||
pixelCount = width * height;
|
||||
pixels = new int[pixelCount];
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ X fix yet another save bug, context menu paste/cut not setting modified
|
||||
X undoing to the code's original state won't unset it as "modified"
|
||||
X http://dev.processing.org/bugs/show_bug.cgi?id=248
|
||||
|
||||
_ some kind of internal color picker
|
||||
_ could be a separate window that's always around if needed
|
||||
|
||||
_ check on whether linux is running properly
|
||||
_ probably threading issue, 98 doesn't have any trouble
|
||||
_ http://dev.processing.org/bugs/show_bug.cgi?id=282
|
||||
@@ -558,8 +561,6 @@ o getMenu() -> return non-null the tool is a submenu
|
||||
o no, bad idea.. don't want a zillion submenus on things
|
||||
_ need a proper means to handle command keys for tools
|
||||
_ http://dev.processing.org/bugs/show_bug.cgi?id=140
|
||||
_ make some kind of internal color picker
|
||||
_ could be a separate window that's always around if needed
|
||||
_ external editor -> add a command to launch
|
||||
_ http://dev.processing.org/bugs/show_bug.cgi?id=141
|
||||
_ add tool for running in jview
|
||||
|
||||
Reference in New Issue
Block a user