public void paint(Graphics g) { super.paint(g); Graphics2D g2 = (Graphics2D) g; int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); g2.translate(getWidth() / 2 - size / 2, getHeight() / 2 - size / 2); g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Shape shape; if (mode == ColorPicker.SAT || mode == ColorPicker.BRI) { shape = new Ellipse2D.Float(0, 0, size, size); } else { Rectangle r = new Rectangle(0, 0, size, size); shape = r; } if (hasFocus()) { PaintUtils.paintFocus(g2, shape, 5); } if (!(shape instanceof Rectangle)) { // paint a circular shadow g2.translate(2, 2); g2.setColor(new Color(0, 0, 0, 20)); g2.fill(new Ellipse2D.Float(-2, -2, size + 4, size + 4)); g2.setColor(new Color(0, 0, 0, 40)); g2.fill(new Ellipse2D.Float(-1, -1, size + 2, size + 2)); g2.setColor(new Color(0, 0, 0, 80)); g2.fill(new Ellipse2D.Float(0, 0, size, size)); g2.translate(-2, -2); } g2.drawImage(image, 0, 0, size, size, 0, 0, size, size, null); if (shape instanceof Rectangle) { Rectangle r = (Rectangle) shape; PaintUtils.drawBevel(g2, r); } else { g2.setColor(new Color(0, 0, 0, 120)); g2.draw(shape); } g2.setColor(Color.white); g2.setStroke(new BasicStroke(1)); g2.draw(new Ellipse2D.Float(point.x - 3, point.y - 3, 6, 6)); g2.setColor(Color.black); g2.draw(new Ellipse2D.Float(point.x - 4, point.y - 4, 8, 8)); g.translate(-imagePadding.left, -imagePadding.top); }
public void mousePressed(MouseEvent e) { requestFocus(); Point p = e.getPoint(); int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); p.translate(-(getWidth() / 2 - size / 2), -(getHeight() / 2 - size / 2)); if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) { // the two circular views: double radius = ((double) size) / 2.0; double x = p.getX() - size / 2.0; double y = p.getY() - size / 2.0; double r = Math.sqrt(x * x + y * y) / radius; double theta = Math.atan2(y, x) / (Math.PI * 2.0); if (r > 1) r = 1; if (mode == ColorPicker.BRI) { setHSB((float) (theta + .25f), (float) (r), bri); } else { setHSB((float) (theta + .25f), sat, (float) (r)); } } else if (mode == ColorPicker.HUE) { float s = ((float) p.x) / ((float) size); float b = ((float) p.y) / ((float) size); if (s < 0) s = 0; if (s > 1) s = 1; if (b < 0) b = 0; if (b > 1) b = 1; setHSB(hue, s, b); } else { int x2 = p.x * 255 / size; int y2 = p.y * 255 / size; if (x2 < 0) x2 = 0; if (x2 > 255) x2 = 255; if (y2 < 0) y2 = 0; if (y2 > 255) y2 = 255; if (mode == ColorPicker.RED) { setRGB(red, x2, y2); } else if (mode == ColorPicker.GREEN) { setRGB(x2, green, y2); } else { setRGB(x2, y2, blue); } } }
public void keyPressed(KeyEvent e) { int dx = 0; int dy = 0; if (e.getKeyCode() == KeyEvent.VK_LEFT) { dx = -1; } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) { dx = 1; } else if (e.getKeyCode() == KeyEvent.VK_UP) { dy = -1; } else if (e.getKeyCode() == KeyEvent.VK_DOWN) { dy = 1; } int multiplier = 1; if (e.isShiftDown() && e.isAltDown()) { multiplier = 10; } else if (e.isShiftDown() || e.isAltDown()) { multiplier = 5; } if (dx != 0 || dy != 0) { int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); int offsetX = getWidth() / 2 - size / 2; int offsetY = getHeight() / 2 - size / 2; mouseListener.mousePressed( new MouseEvent( ColorPickerPanel.this, MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), 0, point.x + multiplier * dx + offsetX, point.y + multiplier * dy + offsetY, 1, false)); } }
/** Recalculates the (x,y) point used to indicate the selected color. */ private void regeneratePoint() { int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); if (mode == ColorPicker.HUE || mode == ColorPicker.SAT || mode == ColorPicker.BRI) { if (mode == ColorPicker.HUE) { point = new Point((int) (sat * size), (int) (bri * size)); } else if (mode == ColorPicker.SAT) { double theta = hue * 2 * Math.PI - Math.PI / 2; if (theta < 0) theta += 2 * Math.PI; double r = bri * size / 2; point = new Point( (int) (r * Math.cos(theta) + .5 + size / 2.0), (int) (r * Math.sin(theta) + .5 + size / 2.0)); } else if (mode == ColorPicker.BRI) { double theta = hue * 2 * Math.PI - Math.PI / 2; if (theta < 0) theta += 2 * Math.PI; double r = sat * size / 2; point = new Point( (int) (r * Math.cos(theta) + .5 + size / 2.0), (int) (r * Math.sin(theta) + .5 + size / 2.0)); } } else if (mode == ColorPicker.RED) { point = new Point((int) (green * size / 255f + .49f), (int) (blue * size / 255f + .49f)); } else if (mode == ColorPicker.GREEN) { point = new Point((int) (red * size / 255f + .49f), (int) (blue * size / 255f + .49f)); } else if (mode == ColorPicker.BLUE) { point = new Point((int) (red * size / 255f + .49f), (int) (green * size / 255f + .49f)); } }
/** Regenerates the image. */ private synchronized void regenerateImage() { int size = Math.min( MAX_SIZE, Math.min( getWidth() - imagePadding.left - imagePadding.right, getHeight() - imagePadding.top - imagePadding.bottom)); if (mode == ColorPicker.BRI || mode == ColorPicker.SAT) { float bri2 = this.bri; float sat2 = this.sat; float radius = ((float) size) / 2f; float hue2; float k = 1.2f; // the number of pixels to antialias for (int y = 0; y < size; y++) { float y2 = (y - size / 2f); for (int x = 0; x < size; x++) { float x2 = (x - size / 2f); double theta = Math.atan2(y2, x2) - 3 * Math.PI / 2.0; if (theta < 0) theta += 2 * Math.PI; double r = Math.sqrt(x2 * x2 + y2 * y2); if (r <= radius) { if (mode == ColorPicker.BRI) { hue2 = (float) (theta / (2 * Math.PI)); sat2 = (float) (r / radius); } else { // SAT hue2 = (float) (theta / (2 * Math.PI)); bri2 = (float) (r / radius); } row[x] = Color.HSBtoRGB(hue2, sat2, bri2); if (r > radius - k) { int alpha = (int) (255 - 255 * (r - radius + k) / k); if (alpha < 0) alpha = 0; if (alpha > 255) alpha = 255; row[x] = row[x] & 0xffffff + (alpha << 24); } } else { row[x] = 0x00000000; } } image.getRaster().setDataElements(0, y, size, 1, row); } } else if (mode == ColorPicker.HUE) { float hue2 = this.hue; for (int y = 0; y < size; y++) { float y2 = ((float) y) / ((float) size); for (int x = 0; x < size; x++) { float x2 = ((float) x) / ((float) size); row[x] = Color.HSBtoRGB(hue2, x2, y2); } image.getRaster().setDataElements(0, y, image.getWidth(), 1, row); } } else { // mode is RED, GREEN, or BLUE int red2 = red; int green2 = green; int blue2 = blue; for (int y = 0; y < size; y++) { float y2 = ((float) y) / ((float) size); for (int x = 0; x < size; x++) { float x2 = ((float) x) / ((float) size); if (mode == ColorPicker.RED) { green2 = (int) (x2 * 255 + .49); blue2 = (int) (y2 * 255 + .49); } else if (mode == ColorPicker.GREEN) { red2 = (int) (x2 * 255 + .49); blue2 = (int) (y2 * 255 + .49); } else { red2 = (int) (x2 * 255 + .49); green2 = (int) (y2 * 255 + .49); } row[x] = 0xFF000000 + (red2 << 16) + (green2 << 8) + blue2; } image.getRaster().setDataElements(0, y, size, 1, row); } } repaint(); }