mirror of
https://github.com/processing/processing4.git
synced 2026-02-04 14:19:19 +01:00
replace tweak color selector with awt version (fixes #3209)
This commit is contained in:
@@ -24,18 +24,18 @@ import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.Cursor;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import javax.swing.Box;
|
||||
import javax.swing.JComponent;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import processing.core.PApplet;
|
||||
import processing.core.PGraphics;
|
||||
import processing.core.PImage;
|
||||
|
||||
|
||||
// TODO replace this with {@link processing.app.ColorChooser}
|
||||
// https://github.com/processing/processing/issues/3209
|
||||
// A guideline: https://github.com/processing/processing/pull/2975/files
|
||||
public class ColorSelector {
|
||||
int hue, saturation, brightness;
|
||||
|
||||
@@ -46,14 +46,13 @@ public class ColorSelector {
|
||||
SelectorTopBar topBar;
|
||||
|
||||
|
||||
public ColorSelector(ColorControlBox colorBox)
|
||||
{
|
||||
public ColorSelector(ColorControlBox colorBox) {
|
||||
this.colorBox = colorBox;
|
||||
createFrame();
|
||||
}
|
||||
|
||||
public void createFrame()
|
||||
{
|
||||
|
||||
public void createFrame() {
|
||||
frame = new JFrame();
|
||||
frame.setBackground(Color.BLACK);
|
||||
|
||||
@@ -64,163 +63,149 @@ public class ColorSelector {
|
||||
|
||||
if (!colorBox.isBW) {
|
||||
selectorBox = new ColorSelectorBox();
|
||||
box.add(selectorBox.getComponent());
|
||||
box.add(selectorBox);
|
||||
}
|
||||
|
||||
box.add(Box.createHorizontalGlue());
|
||||
box.add(selectorSlider.getComponent(), BorderLayout.CENTER);
|
||||
box.add(selectorSlider, BorderLayout.CENTER);
|
||||
box.add(Box.createHorizontalGlue());
|
||||
|
||||
frame.getContentPane().add(box, BorderLayout.CENTER);
|
||||
frame.pack();
|
||||
frame.setResizable(false);
|
||||
frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
|
||||
|
||||
selectorBox.init();
|
||||
selectorSlider.init();
|
||||
}
|
||||
|
||||
public void show(int x, int y)
|
||||
{
|
||||
|
||||
public void show(int x, int y) {
|
||||
frame.setLocation(x, y);
|
||||
frame.setVisible(true);
|
||||
frame.repaint();
|
||||
}
|
||||
|
||||
public void hide()
|
||||
{
|
||||
|
||||
public void hide() {
|
||||
this.colorBox = null;
|
||||
frame.setVisible(false);
|
||||
}
|
||||
|
||||
public void refreshColor()
|
||||
{
|
||||
if (colorBox.ilegalColor) {
|
||||
return;
|
||||
}
|
||||
|
||||
setColor(colorBox.color);
|
||||
public void refreshColor() {
|
||||
if (!colorBox.ilegalColor) {
|
||||
setColor(colorBox.color);
|
||||
}
|
||||
}
|
||||
|
||||
public void setColor(Color c)
|
||||
{
|
||||
|
||||
public void setColor(Color c) {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.setToColor(c);
|
||||
}
|
||||
selectorSlider.setToColor(c);
|
||||
|
||||
repaintSelector();
|
||||
}
|
||||
|
||||
public void satBrightChanged()
|
||||
{
|
||||
|
||||
public void satBrightChanged() {
|
||||
repaintSelector();
|
||||
}
|
||||
|
||||
public void hueChanged()
|
||||
{
|
||||
|
||||
public void hueChanged() {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.renderBack();
|
||||
}
|
||||
repaintSelector();
|
||||
}
|
||||
|
||||
public void repaintSelector()
|
||||
{
|
||||
|
||||
public void repaintSelector() {
|
||||
if (selectorBox != null) {
|
||||
selectorBox.redraw();
|
||||
selectorBox.repaint();
|
||||
}
|
||||
selectorSlider.redraw();
|
||||
selectorSlider.repaint();
|
||||
}
|
||||
|
||||
/*
|
||||
* PApplets for the interactive color fields
|
||||
*/
|
||||
|
||||
public class ColorSelectorBox extends PApplet {
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
class ColorSelectorBox extends JComponent {
|
||||
int lastX, lastY;
|
||||
PImage backImg;
|
||||
|
||||
public int sketchWidth() { return 255; }
|
||||
|
||||
public int sketchHeight() { return 255; }
|
||||
|
||||
public void setup() {
|
||||
noLoop();
|
||||
colorMode(HSB, 255, 255, 255);
|
||||
noFill();
|
||||
BufferedImage backImg;
|
||||
|
||||
ColorSelectorBox() {
|
||||
if (!colorBox.ilegalColor) {
|
||||
setToColor(colorBox.color);
|
||||
}
|
||||
|
||||
renderBack();
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
image(backImg, 0, 0);
|
||||
|
||||
stroke((lastY<128) ? 0 : 255);
|
||||
public void paintComponent(Graphics g) {
|
||||
g.drawImage(backImg, 0, 0, this);
|
||||
|
||||
pushMatrix();
|
||||
translate(lastX, lastY);
|
||||
ellipse(0, 0, 5, 5);
|
||||
line(-8, 0, -6, 0);
|
||||
line(6, 0, 8, 0);
|
||||
line(0, -8, 0, -6);
|
||||
line(0, 6, 0, 8);
|
||||
popMatrix();
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
g.setColor(lastY < 128 ? Color.BLACK : Color.WHITE);
|
||||
AffineTransform tx = g2.getTransform();
|
||||
g2.translate(lastX, lastY);
|
||||
g2.drawOval(0, 0, 5, 5);
|
||||
g2.drawLine(-8, 0, -6, 0);
|
||||
g2.drawLine(6, 0, 8, 0);
|
||||
g2.drawLine(0, -8, 0, -6);
|
||||
g2.drawLine(0, 6, 0, 8);
|
||||
g2.setTransform(tx);
|
||||
}
|
||||
|
||||
|
||||
public void renderBack() {
|
||||
PGraphics buf = createGraphics(255, 255);
|
||||
buf.colorMode(HSB, 255, 255, 255);
|
||||
buf.beginDraw();
|
||||
buf.loadPixels();
|
||||
int index=0;
|
||||
for (int j=0; j<255; j++) {
|
||||
for (int i=0; i<255; i++) {
|
||||
buf.pixels[index++] = color(hue, i, 255-j);
|
||||
int[] pixels = new int[256 * 256];
|
||||
int index = 0;
|
||||
for (int j = 0; j < 256; j++) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
pixels[index++] = // color(hue, i, 255-j);
|
||||
Color.HSBtoRGB(hue / 255f, (i / 255f), (255-j));
|
||||
}
|
||||
}
|
||||
buf.updatePixels();
|
||||
buf.endDraw();
|
||||
|
||||
backImg = buf.get();
|
||||
backImg = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
|
||||
backImg.getRaster().setDataElements(0, 0, 256, 256, pixels);
|
||||
}
|
||||
|
||||
|
||||
public void setToColor(Color c) {
|
||||
// set selector color
|
||||
float hsb[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
|
||||
saturation = (int)(hsb[1]*255);
|
||||
brightness = (int)(hsb[2]*255);
|
||||
saturation = (int) (hsb[1] * 255);
|
||||
brightness = (int) (hsb[2] * 255);
|
||||
lastX = saturation;
|
||||
lastY = 255 - brightness;
|
||||
}
|
||||
|
||||
public void mousePressed() {
|
||||
if (mouseX < 0 || mouseX > 255 ||
|
||||
mouseY < 0 || mouseY > 255) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastX = mouseX;
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
void updateMouse(MouseEvent event) {
|
||||
int mouseX = event.getX();
|
||||
int mouseY = event.getY();
|
||||
|
||||
if (mouseX >= 0 && mouseX < 256 &&
|
||||
mouseY >= 0 && mouseY < 256) {
|
||||
lastX = mouseX;
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseDragged() {
|
||||
if (mouseX < 0 || mouseX > 255 ||
|
||||
mouseY < 0 || mouseY > 255) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastX = mouseX;
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
}
|
||||
|
||||
public void updateColor() {
|
||||
void updateColor() {
|
||||
saturation = lastX;
|
||||
brightness = 255 - lastY;
|
||||
|
||||
@@ -228,182 +213,195 @@ public class ColorSelector {
|
||||
colorBox.selectorChanged(hue, saturation, brightness);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(255, 255);
|
||||
return new Dimension(256, 256);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return new Dimension(255, 255);
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return new Dimension(255, 255);
|
||||
return getPreferredSize();
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
public class ColorSelectorSlider extends PApplet {
|
||||
PImage backImg;
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
class ColorSelectorSlider extends JComponent {
|
||||
final int WIDE = 30;
|
||||
BufferedImage backImg;
|
||||
int lastY;
|
||||
|
||||
public void setup() {
|
||||
size(30, 255);
|
||||
noLoop();
|
||||
colorMode(HSB, 255, 255, 255);
|
||||
strokeWeight(1);
|
||||
noFill();
|
||||
loadPixels();
|
||||
ColorSelectorSlider() {
|
||||
// size(30, 255);
|
||||
// noLoop();
|
||||
// colorMode(HSB, 255, 255, 255);
|
||||
// strokeWeight(1);
|
||||
// noFill();
|
||||
// loadPixels();
|
||||
if (!colorBox.ilegalColor) {
|
||||
setToColor(colorBox.color);
|
||||
}
|
||||
|
||||
// draw the slider background
|
||||
renderBack();
|
||||
|
||||
addMouseListener(new MouseAdapter() {
|
||||
public void mousePressed(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
updateMouse(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void draw() {
|
||||
image(backImg, 0, 0);
|
||||
if (colorBox.isBW) {
|
||||
stroke(lastY<128 ? 0 : 255);
|
||||
}
|
||||
else {
|
||||
stroke(0);
|
||||
public void paintComponent(Graphics g) {
|
||||
g.drawImage(backImg, 0, 0, this);
|
||||
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
// if (colorBox.isBW) {
|
||||
// stroke(lastY<128 ? 0 : 255);
|
||||
// }
|
||||
// else {
|
||||
// stroke(0);
|
||||
// }
|
||||
if (colorBox.isBW && lastY >= 128) {
|
||||
g2.setColor(Color.WHITE);
|
||||
} else {
|
||||
g2.setColor(Color.BLACK);
|
||||
}
|
||||
|
||||
pushMatrix();
|
||||
translate(0, lastY);
|
||||
AffineTransform tx = g2.getTransform();
|
||||
g2.translate(0, lastY);
|
||||
// draw left bracket
|
||||
beginShape();
|
||||
vertex(5, -2);
|
||||
vertex(1, -2);
|
||||
vertex(1, 2);
|
||||
vertex(5, 2);
|
||||
endShape();
|
||||
// beginShape();
|
||||
// vertex(5, -2);
|
||||
// vertex(1, -2);
|
||||
// vertex(1, 2);
|
||||
// vertex(5, 2);
|
||||
// endShape();
|
||||
g.drawRect(1, -2, 6, 4);
|
||||
|
||||
// draw middle lines
|
||||
line(13, 0, 17, 0);
|
||||
line(15, -2, 15, 2);
|
||||
g.drawLine(13, 0, 17, 0);
|
||||
g.drawLine(15, -2, 15, 2);
|
||||
|
||||
// draw right bracket
|
||||
beginShape();
|
||||
vertex(24, -2);
|
||||
vertex(28, -2);
|
||||
vertex(28, 2);
|
||||
vertex(24, 2);
|
||||
endShape();
|
||||
popMatrix();
|
||||
// beginShape();
|
||||
// vertex(24, -2);
|
||||
// vertex(28, -2);
|
||||
// vertex(28, 2);
|
||||
// vertex(24, 2);
|
||||
// endShape();
|
||||
g.drawRect(24, -2, 4, 4);
|
||||
g2.setTransform(tx);
|
||||
|
||||
if (colorBox.isBW) {
|
||||
stroke(255);
|
||||
rect(0, 0, 29, 254);
|
||||
}
|
||||
else {
|
||||
stroke(0);
|
||||
line(0, 0, 0, 255);
|
||||
line(29, 0, 29, 255);
|
||||
// stroke(255);
|
||||
// rect(0, 0, 29, 254);
|
||||
g.setColor(Color.WHITE);
|
||||
g.drawRect(0, 0, WIDE, 255);
|
||||
} else {
|
||||
// stroke(0);
|
||||
// line(0, 0, 0, 255);
|
||||
// line(29, 0, 29, 255);
|
||||
g.setColor(Color.BLACK);
|
||||
g.drawLine(0, 0, 0, 255);
|
||||
g.drawLine(29, 0, 29, 255);
|
||||
}
|
||||
}
|
||||
|
||||
public void renderBack() {
|
||||
PGraphics buf = createGraphics(30, 255);
|
||||
buf.beginDraw();
|
||||
buf.loadPixels();
|
||||
int index=0;
|
||||
for (int j=0; j<255; j++) {
|
||||
for (int i=0; i<30; i++) {
|
||||
if (colorBox.isBW) {
|
||||
buf.pixels[index++] = color(255-j);
|
||||
}
|
||||
else {
|
||||
buf.pixels[index++] = color(255-j, 255, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
buf.updatePixels();
|
||||
buf.endDraw();
|
||||
|
||||
backImg = buf.get();
|
||||
void renderBack() {
|
||||
int[] pixels = new int[WIDE * 256];
|
||||
int index = 0;
|
||||
|
||||
int argb = 0;
|
||||
for (int j = 0; j < 256; j++) {
|
||||
if (colorBox.isBW) {
|
||||
int gray = 255 - j;
|
||||
argb = 0xff000000 | (gray << 16) | (gray << 8) | gray;
|
||||
} else {
|
||||
// color(255-j, 255, 255);
|
||||
argb = Color.HSBtoRGB((255 - j) / 255f, 1, 1);
|
||||
}
|
||||
for (int i = 0; i < WIDE; i++) {
|
||||
pixels[index++] = argb;
|
||||
}
|
||||
}
|
||||
backImg = new BufferedImage(WIDE, 256, BufferedImage.TYPE_INT_RGB);
|
||||
backImg.getRaster().setDataElements(0, 0, WIDE, 256, pixels);
|
||||
}
|
||||
|
||||
public void setToColor(Color c)
|
||||
{
|
||||
|
||||
void setToColor(Color c) {
|
||||
// set slider position
|
||||
if (colorBox.isBW) {
|
||||
hue = c.getRed();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
float hsb[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
|
||||
hue = (int)(hsb[0]*255);
|
||||
}
|
||||
lastY = 255 - hue;
|
||||
}
|
||||
|
||||
public void mousePressed()
|
||||
{
|
||||
if (mouseX < 0 || mouseX > 30 ||
|
||||
mouseY < 0 || mouseY > 255) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
}
|
||||
void updateMouse(MouseEvent event) {
|
||||
int mouseY = event.getY();
|
||||
if (mouseY >= 0 && mouseY < 256) {
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
}
|
||||
}
|
||||
|
||||
public void mouseDragged()
|
||||
{
|
||||
if (mouseX < 0 || mouseX > 30 ||
|
||||
mouseY < 0 || mouseY > 255) {
|
||||
return;
|
||||
}
|
||||
|
||||
lastY = mouseY;
|
||||
updateColor();
|
||||
}
|
||||
|
||||
public void updateColor()
|
||||
{
|
||||
public void updateColor() {
|
||||
hue = 255 - lastY;
|
||||
|
||||
hueChanged();
|
||||
colorBox.selectorChanged(hue, saturation, brightness);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
return new Dimension(30, 255);
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return new Dimension(30, 255);
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return new Dimension(30, 255);
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
|
||||
public class SelectorTopBar extends PApplet
|
||||
{
|
||||
|
||||
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
|
||||
|
||||
|
||||
public class SelectorTopBar extends JComponent {
|
||||
int barWidth;
|
||||
int barHeight;
|
||||
int barHeight = 16;
|
||||
|
||||
public SelectorTopBar(int w)
|
||||
{
|
||||
super();
|
||||
public SelectorTopBar(int w) {
|
||||
barWidth = w;
|
||||
barHeight = 16;
|
||||
}
|
||||
|
||||
public void setup()
|
||||
{
|
||||
size(barWidth, barHeight);
|
||||
noLoop();
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
background(128);
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
g.setColor(Color.GRAY);
|
||||
Dimension size = getSize();
|
||||
g.fillRect(0, 0, size.width, size.height);
|
||||
}
|
||||
|
||||
public Dimension getPreferredSize() {
|
||||
@@ -411,12 +409,11 @@ public class ColorSelector {
|
||||
}
|
||||
|
||||
public Dimension getMinimumSize() {
|
||||
return new Dimension(barWidth, barHeight);
|
||||
return getPreferredSize();
|
||||
}
|
||||
|
||||
public Dimension getMaximumSize() {
|
||||
return new Dimension(barWidth, barHeight);
|
||||
return getPreferredSize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user