colorpicker-1.0.0/ 0000755 0001750 0001750 00000000000 11452722626 013355 5 ustar andrew andrew colorpicker-1.0.0/License.html 0000644 0001750 0001750 00000004056 11437472304 015630 0 ustar andrew andrew Source code, binaries and/or any other resources in the package labeled "com.bric" are copyright (c) 2010 by Jeremy Wood. They are available under the Modified BSD license (see below).
Any resources not in the "com.bric" package may be subject to additional license terms and restrictions.
If you have any questions about this jar, the relevant licenses, the source code, etc., please contact mickleness+java@gmail.com.
This jar is part of the "javagraphics" project, discussed here
Copyright (c) 2010, Jeremy Wood.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* The name of the contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
colorpicker-1.0.0/META-INF/ 0000755 0001750 0001750 00000000000 11452722626 014515 5 ustar andrew andrew colorpicker-1.0.0/META-INF/MANIFEST.MF 0000644 0001750 0001750 00000000117 11437472304 016144 0 ustar andrew andrew Manifest-Version: 1.0
Sealed: false
Main-Class: com/bric/swing/ColorPickerDemo
colorpicker-1.0.0/com/ 0000755 0001750 0001750 00000000000 11452722626 014133 5 ustar andrew andrew colorpicker-1.0.0/com/bric/ 0000755 0001750 0001750 00000000000 11452722626 015052 5 ustar andrew andrew colorpicker-1.0.0/com/bric/swing/ 0000755 0001750 0001750 00000000000 11452722626 016201 5 ustar andrew andrew colorpicker-1.0.0/com/bric/swing/ImageTransferable.java 0000644 0001750 0001750 00000002534 11437472304 022421 0 ustar andrew andrew /*
* @(#)ImageTransferable.java
*
* $Date: 2009-02-20 01:34:41 -0600 (Fri, 20 Feb 2009) $
*
* Copyright (c) 2009 by Jeremy Wood.
* All rights reserved.
*
* The copyright of this software is owned by Jeremy Wood.
* You may not use, copy or modify this software, except in
* accordance with the license agreement you entered into with
* Jeremy Wood. For details see accompanying license terms.
*
* This software is probably, but not necessarily, discussed here:
* http://javagraphics.blogspot.com/
*
* And the latest version should be available here:
* https://javagraphics.dev.java.net/
*/
package com.bric.swing;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
class ImageTransferable implements Transferable {
Image img;
public ImageTransferable(Image i) {
img = i;
}
public Object getTransferData(DataFlavor f)
throws UnsupportedFlavorException, IOException {
if(f.equals(DataFlavor.imageFlavor)==false)
throw new UnsupportedFlavorException(f);
return img;
}
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {DataFlavor.imageFlavor};
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return(flavor.equals(DataFlavor.imageFlavor));
}
}
colorpicker-1.0.0/com/bric/swing/resources/ 0000755 0001750 0001750 00000000000 11452722626 020213 5 ustar andrew andrew colorpicker-1.0.0/com/bric/swing/resources/DialogFooter_sp.properties 0000644 0001750 0001750 00000000055 11437472304 025407 0 ustar andrew andrew dialogCancelButton=Cancelar
dialogOKButton=OK colorpicker-1.0.0/com/bric/swing/resources/DialogFooter_de.properties 0000644 0001750 0001750 00000000056 11437472304 025356 0 ustar andrew andrew dialogCancelButton=Abbrechen
dialogOKButton=OK colorpicker-1.0.0/com/bric/swing/resources/DialogFooter_nl.properties 0000644 0001750 0001750 00000000057 11437472304 025400 0 ustar andrew andrew dialogCancelButton=Annuleeren
dialogOKButton=OK colorpicker-1.0.0/com/bric/swing/resources/ColorPicker.properties 0000644 0001750 0001750 00000001030 11437472304 024535 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=Choose a Color
Copy=Copy
blueLabel=Blue:
greenLabel=Green:
redLabel=Red:
brightnessLabel=Bri:
hueLabel=Hue:
saturationLabel=Sat:
hexLabel=Hex:
opacityLabel=Opacity:
alphaLabel=Alpha: colorpicker-1.0.0/com/bric/swing/resources/ColorPicker_ru.properties 0000644 0001750 0001750 00000002032 11437472304 025246 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=\u0412\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0446\u0432\u0435\u0442
Copy=\u041a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c
Cancel=\u041e\u0442\u043c\u0435\u043d\u0430
OK=OK
blueLabel=\u0421\u0438\u043d\u0438\u0439:
greenLabel=\u0417\u0435\u043b\u0451\u043d\u044b\u0439:
redLabel=\u041a\u0440\u0430\u0441\u043d\u044b\u0439:
brightnessLabel=\u042f\u0440\u043a\u043e\u0441\u0442\u044c:
hueLabel=\u0422\u043e\u043d:
saturationLabel=\u041d\u0430\u0441\u044b\u0449\u0435\u043d\u043d\u043e\u0441\u0442\u044c:
hexLabel=Hex:
opacityLabel=\u041d\u0435\u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e\u0441\u0442\u044c:
alphaLabel=\u0410\u043b\u044c\u0444\u0430: colorpicker-1.0.0/com/bric/swing/resources/DialogFooter_es.properties 0000644 0001750 0001750 00000000122 11437472304 025367 0 ustar andrew andrew dialogCancelButton=Cancelar
dialogOKButton=OK
dialogYesButton=Sí
dialogNoButton=No colorpicker-1.0.0/com/bric/swing/resources/ColorPicker_sp.properties 0000644 0001750 0001750 00000001245 11437472304 025247 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=Elija un color
Copy=Copiar
blueLabel=Azul:
greenLabel=Verde:
redLabel=Rojo:
brightnessLabel=Luminosidad:
# not sure, could be 'brillo'
hueLabel=Hue:
# dont know : matiz ? 'Tinte' is more the tint
saturationLabel=Saturación:
hexLabel=Hex:
opacityLabel=Opacidad:
alphaLabel=Alpha:
# not sure but I believe alpha should work colorpicker-1.0.0/com/bric/swing/resources/ColorPicker_es.properties 0000644 0001750 0001750 00000001021 11437472304 025224 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=Elegir Color
Copy=Copiar
blueLabel=Azul:
greenLabel=Verde:
redLabel=Rojo:
brightnessLabel=Brillo:
saturationLabel=Sat:
hueLabel=Tinte:
opacityLabel=Opacidad:
alphaLabel=Alpha: colorpicker-1.0.0/com/bric/swing/resources/ColorPicker_en_GB.properties 0000644 0001750 0001750 00000000561 11437472304 025577 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=Choose a Colour colorpicker-1.0.0/com/bric/swing/resources/ColorPicker_de.properties 0000644 0001750 0001750 00000001042 11437472304 025210 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=Farbauswahl
Copy=Kopieren
blueLabel=Blau:
greenLabel=Grün:
redLabel=Rot:
brightnessLabel=Helligkeit:
saturationLabel=Sättigung:
hueLabel=Farbton:
opacityLabel=Deckkraft:
alphaLabel=Alphakanal: colorpicker-1.0.0/com/bric/swing/resources/DialogFooter.properties 0000644 0001750 0001750 00000000445 11437472304 024710 0 ustar andrew andrew dialogCancelButton=Cancel
dialogOKButton=OK
dialogSaveButton=Save
dialogDontSaveButton=Don't Save
dialogYesButton=Yes
dialogNoButton=No
dialogCancelMnemonic=C
dialogOKMnemonic=O
dialogSaveMnemonic=S
dialogDontSaveMnemonic=D
dialogYesMnemonic=Y
dialogNoMnemonic=N
dialogHelpLearnMore=Learn More colorpicker-1.0.0/com/bric/swing/resources/ColorPicker_nl.properties 0000644 0001750 0001750 00000001331 11437472304 025232 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=Kies de kleur
Copy=Kopiëren
# or 'copy'
blueLabel=Blauw:
greenLabel=Groen:
redLabel=Rood:
brightnessLabel=Helderheid:
hueLabel=Hue:
# or tint: 'tint' ; color spread: 'kleur(schakeriung)'
saturationLabel=Saturatie:
# 'kleur Intensiteit' which means color intensity
hexLabel=Hex:
opacityLabel=ondoorzichtigheid:
# or 'Opaciteit'
alphaLabel=doorzichtigheid:
#or 'transparantie' colorpicker-1.0.0/com/bric/swing/resources/ColorPicker_fr.properties 0000644 0001750 0001750 00000001116 11437472304 025231 0 ustar andrew andrew #
# Copyright (c) 2008 Jeremy Wood
# E-mail: mickleness@gmail.com
# All rights reserved.
#
# The copyright of this software is owned by Jeremy Wood.
# You may not use, copy or modify this software, except in
# accordance with the license agreement you entered into with
# Jeremy Wood. For details see accompanying license terms.
#
ColorPickerDialogTitle=Choisissez une couleur
Copy=Copier
blueLabel=Bleu:
greenLabel=Vert:
redLabel=Rouge:
brightnessLabel=Luminosité:
hueLabel=Teinte:
saturationLabel=Saturation:
hexLabel=Hex:
opacityLabel=Opacité:
alphaLabel=Tranparence:
# Alpha works too ! colorpicker-1.0.0/com/bric/swing/resources/DialogFooter_fr.properties 0000644 0001750 0001750 00000000123 11437472304 025370 0 ustar andrew andrew dialogCancelButton=Annuler
dialogOKButton=OK
dialogNoButton=Non
dialogYesButton=Oui colorpicker-1.0.0/com/bric/swing/ColorPickerDialog.java 0000644 0001750 0001750 00000005622 11437472304 022403 0 ustar andrew andrew /*
* @(#)ColorPickerDialog.java
*
* $Date: 2010-05-27 02:49:03 -0500 (Thu, 27 May 2010) $
*
* Copyright (c) 2009 by Jeremy Wood.
* All rights reserved.
*
* The copyright of this software is owned by Jeremy Wood.
* You may not use, copy or modify this software, except in
* accordance with the license agreement you entered into with
* Jeremy Wood. For details see accompanying license terms.
*
* This software is probably, but not necessarily, discussed here:
* http://javagraphics.blogspot.com/
*
* And the latest version should be available here:
* https://javagraphics.dev.java.net/
*/
package com.bric.swing;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComponent;
import javax.swing.JDialog;
/** This wraps a ColorPicker
in a simple dialog with "OK" and "Cancel" options.
*
(This object is used by the static calls in ColorPicker
to show a dialog.)
*
*/
class ColorPickerDialog extends JDialog {
private static final long serialVersionUID = 2L;
ColorPicker cp;
int alpha;
Color returnValue = null;
ActionListener okListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
returnValue = cp.getColor();
}
};
DialogFooter footer;
public ColorPickerDialog(Frame owner, Color color,boolean includeOpacity) {
super(owner);
initialize(owner,color,includeOpacity);
}
public ColorPickerDialog(Dialog owner, Color color,boolean includeOpacity) {
super(owner);
initialize(owner,color,includeOpacity);
}
private void initialize(Component owner,Color color,boolean includeOpacity) {
cp = new ColorPicker(true,includeOpacity);
setModal(true);
setResizable(false);
getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0; c.gridy = 0;
c.weightx = 1; c.weighty = 1; c.fill = GridBagConstraints.BOTH;
c.gridwidth = GridBagConstraints.REMAINDER;
c.insets = new Insets(10,10,10,10);
getContentPane().add(cp,c);
c.gridy++;
footer = DialogFooter.createDialogFooter(new JComponent[] {},
DialogFooter.OK_CANCEL_OPTION, DialogFooter.OK_OPTION, DialogFooter.ESCAPE_KEY_TRIGGERS_CANCEL);
c.gridy++; c.weighty = 0;
getContentPane().add(footer, c);
cp.setRGB(color.getRed(), color.getGreen(), color.getBlue());
cp.setOpacity( color.getAlpha() );
alpha = color.getAlpha();
pack();
setLocationRelativeTo(owner);
footer.getButton(DialogFooter.OK_OPTION).addActionListener(okListener);
}
/** @return the color committed when the user clicked 'OK'. Note this returns null
* if the user canceled this dialog, or exited via the close decoration.
*/
public Color getColor() {
return returnValue;
}
}
colorpicker-1.0.0/com/bric/swing/DelegateFocusTraversalPolicy.java 0000644 0001750 0001750 00000003300 11437472304 024614 0 ustar andrew andrew /*
* @(#)DelegateFocusTraversalPolicy.java
*
* $Date: 2010-03-19 18:47:16 -0500 (Fri, 19 Mar 2010) $
*
* Copyright (c) 2010 by Jeremy Wood.
* All rights reserved.
*
* The copyright of this software is owned by Jeremy Wood.
* You may not use, copy or modify this software, except in
* accordance with the license agreement you entered into with
* Jeremy Wood. For details see accompanying license terms.
*
* This software is probably, but not necessarily, discussed here:
* https://javagraphics.dev.java.net/
*
* That site should also contain the most recent official version
* of this software. (See the SVN repository for more details.)
*/
package com.bric.swing;
import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
/** A simple FocusTraversalPolicy
object that delegates to
* another object.
*
*/
public class DelegateFocusTraversalPolicy extends FocusTraversalPolicy {
FocusTraversalPolicy ftp;
public DelegateFocusTraversalPolicy(FocusTraversalPolicy policy) {
ftp = policy;
}
public Component getComponentAfter(Container focusCycleRoot,
Component component) {
return ftp.getComponentAfter(focusCycleRoot, component);
}
public Component getComponentBefore(Container focusCycleRoot,
Component component) {
return ftp.getComponentBefore(focusCycleRoot, component);
}
public Component getDefaultComponent(Container focusCycleRoot) {
return ftp.getDefaultComponent(focusCycleRoot);
}
public Component getFirstComponent(Container focusCycleRoot) {
return ftp.getFirstComponent(focusCycleRoot);
}
public Component getLastComponent(Container focusCycleRoot) {
return ftp.getLastComponent(focusCycleRoot);
}
}
colorpicker-1.0.0/com/bric/swing/ColorSwatch.java 0000644 0001750 0001750 00000007774 11437472304 021311 0 ustar andrew andrew /*
* @(#)ColorSwatch.java
*
* $Date: 2010-03-19 19:27:24 -0500 (Fri, 19 Mar 2010) $
*
* Copyright (c) 2009 by Jeremy Wood.
* All rights reserved.
*
* The copyright of this software is owned by Jeremy Wood.
* You may not use, copy or modify this software, except in
* accordance with the license agreement you entered into with
* Jeremy Wood. For details see accompanying license terms.
*
* This software is probably, but not necessarily, discussed here:
* http://javagraphics.blogspot.com/
*
* And the latest version should be available here:
* https://javagraphics.dev.java.net/
*/
package com.bric.swing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.Toolkit;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import com.bric.plaf.PlafPaintUtils;
/** This is a square, opaque panel used to indicate
* a certain color.
*
The color is assigned with the setForeground()
method.
*
Also the user can right-click this panel and select 'Copy' to send
* a 100x100 image of this color to the clipboard. (This feature was
* added at the request of a friend who paints; she wanted to select a
* color and then quickly print it off, and then mix her paints to match
* that shade.)
*
*/
public class ColorSwatch extends JPanel {
private static final long serialVersionUID = 1L;
JPopupMenu menu;
JMenuItem copyItem;
MouseListener mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(e.isPopupTrigger()) {
if(menu==null) {
menu = new JPopupMenu();
copyItem = new JMenuItem(ColorPicker.strings.getObject("Copy").toString());
menu.add(copyItem);
copyItem.addActionListener(actionListener);
}
menu.show(ColorSwatch.this,e.getX(),e.getY());
}
}
};
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src==copyItem) {
BufferedImage image = new BufferedImage(100,100,BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
g.setColor(getBackground());
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.dispose();
Transferable contents = new ImageTransferable(image);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, null);
}
}
};
int w;
public ColorSwatch(int width) {
w = width;
setPreferredSize(new Dimension(width,width));
setMinimumSize(new Dimension(width,width));
addMouseListener(mouseListener);
}
public ColorSwatch(Color color,int width) {
this(width);
setForeground(color);
}
private static TexturePaint checkerPaint = null;
private static TexturePaint getCheckerPaint() {
if(checkerPaint==null) {
int t = 8;
BufferedImage bi = new BufferedImage(t*2,t*2,BufferedImage.TYPE_INT_RGB);
Graphics g = bi.createGraphics();
g.setColor(Color.white);
g.fillRect(0,0,2*t,2*t);
g.setColor(Color.lightGray);
g.fillRect(0,0,t,t);
g.fillRect(t,t,t,t);
checkerPaint = new TexturePaint(bi,new Rectangle(0,0,bi.getWidth(),bi.getHeight()));
}
return checkerPaint;
}
public void paint(Graphics g0) {
super.paint(g0); //may be necessary for some look-and-feels?
Graphics2D g = (Graphics2D)g0;
Color c = getForeground();
int w2 = Math.min(getWidth(), w);
int h2 = Math.min(getHeight(), w);
Rectangle r = new Rectangle(getWidth()/2-w2/2,getHeight()/2-h2/2, w2, h2);
if(c.getAlpha()<255) {
TexturePaint checkers = getCheckerPaint();
g.setPaint(checkers);
g.fillRect(r.x, r.y, r.width, r.height);
}
g.setColor(c);
g.fillRect(r.x, r.y, r.width, r.height);
PlafPaintUtils.drawBevel(g, r);
}
}
colorpicker-1.0.0/com/bric/swing/ColorPickerPanel.java 0000644 0001750 0001750 00000045202 11437472304 022241 0 ustar andrew andrew /*
* @(#)ColorPickerPanel.java
*
* $Date: 2010-03-19 19:27:24 -0500 (Fri, 19 Mar 2010) $
*
* Copyright (c) 2009 by Jeremy Wood.
* All rights reserved.
*
* The copyright of this software is owned by Jeremy Wood.
* You may not use, copy or modify this software, except in
* accordance with the license agreement you entered into with
* Jeremy Wood. For details see accompanying license terms.
*
* This software is probably, but not necessarily, discussed here:
* http://javagraphics.blogspot.com/
*
* And the latest version should be available here:
* https://javagraphics.dev.java.net/
*/
package com.bric.swing;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.Vector;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.MouseInputListener;
import com.bric.plaf.PlafPaintUtils;
/** This is the large graphic element in the ColorPicker
* that depicts a wide range of colors.
*
This panel can operate in 6 different modes. In each mode a different
* property is held constant: hue, saturation, brightness, red, green, or blue.
* (Each property is identified with a constant in the ColorPicker
class,
* such as: ColorPicker.HUE
or ColorPicker.GREEN
.)
*
In saturation and brightness mode, a wheel is used. Although it doesn't * use as many pixels as a square does: it is a very aesthetic model since the hue can * wrap around in a complete circle. (Also, on top of looks, this is how most * people learn to think the color spectrum, so it has that advantage, too). * In all other modes a square is used. *
The user can click in this panel to select a new color. The selected color is * highlighted with a circle drawn around it. Also once this * component has the keyboard focus, the user can use the arrow keys to * traverse the available colors. *
Note this component is public and exists independently of the
* ColorPicker
class. The only way this class is dependent
* on the ColorPicker
class is when the constants for the modes
* are used.
*
The graphic in this panel will be based on either the width or * the height of this component: depending on which is smaller. * */ public class ColorPickerPanel extends JPanel { private static final long serialVersionUID = 1L; /** The maximum size the graphic will be. No matter * how big the panel becomes, the graphic will not exceed * this length. *
(This is enforced because only 1 BufferedImage is used
* to render the graphic. This image is created once at a fixed
* size and is never replaced.)
*/
public static final int MAX_SIZE = 325;
/** This controls how the colors are displayed. */
private int mode = ColorPicker.BRI;
/** The point used to indicate the selected color. */
private Point point = new Point(0,0);
private Vector changeListeners = new Vector();
/* Floats from [0,1]. They must be kept distinct, because
* when you convert them to RGB coordinates HSB(0,0,0) and HSB (.5,0,0)
* and then convert them back to HSB coordinates, the hue always shifts back to zero.
*/
float hue = -1, sat = -1, bri = -1;
int red = -1, green = -1, blue = -1;
MouseInputListener mouseListener = new MouseInputAdapter() {
public void mousePressed(MouseEvent e) {
requestFocus();
Point p = e.getPoint();
if(mode==ColorPicker.BRI || mode==ColorPicker.SAT ||
mode==ColorPicker.HUE) {
float[] hsb = getHSB(p);
setHSB(hsb[0], hsb[1], hsb[2]);
} else {
int[] rgb = getRGB(p);
setRGB(rgb[0], rgb[1], rgb[2]);
}
}
public void mouseDragged(MouseEvent e) {
mousePressed(e);
}
};
KeyListener keyListener = new KeyAdapter() {
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
));
}
}
};
FocusListener focusListener = new FocusListener() {
public void focusGained(FocusEvent e) {
repaint();
}
public void focusLost(FocusEvent e) {
repaint();
}
};
ComponentListener componentListener = new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
regeneratePoint();
regenerateImage();
}
};
BufferedImage image = new BufferedImage(MAX_SIZE, MAX_SIZE, BufferedImage.TYPE_INT_ARGB);
/** Creates a new If this panel is in HUE, SAT, or BRI mode, then
* this method converts these values to HSB coordinates
* and calls This method may regenerate the graphic if necessary.
*
* @param r the red value of the selected color.
* @param g the green value of the selected color.
* @param b the blue value of the selected color.
*/
public void setRGB(int r,int g,int b) {
if(r<0 || r>255)
throw new IllegalArgumentException("The red value ("+r+") must be between [0,255].");
if(g<0 || g>255)
throw new IllegalArgumentException("The green value ("+g+") must be between [0,255].");
if(b<0 || b>255)
throw new IllegalArgumentException("The blue value ("+b+") must be between [0,255].");
if(red!=r || green!=g || blue!=b) {
if(mode==ColorPicker.RED ||
mode==ColorPicker.GREEN ||
mode==ColorPicker.BLUE) {
int lastR = red;
int lastG = green;
int lastB = blue;
red = r;
green = g;
blue = b;
if(mode==ColorPicker.RED) {
if(lastR!=r) {
regenerateImage();
}
} else if(mode==ColorPicker.GREEN) {
if(lastG!=g) {
regenerateImage();
}
} else if(mode==ColorPicker.BLUE) {
if(lastB!=b) {
regenerateImage();
}
}
} else {
float[] hsb = new float[3];
Color.RGBtoHSB(r, g, b, hsb);
setHSB(hsb[0],hsb[1],hsb[2]);
return;
}
regeneratePoint();
repaint();
fireChangeListeners();
}
}
/** @return the HSB values of the selected color.
* Each value is between [0,1].
*/
public float[] getHSB() {
return new float[] {hue, sat, bri};
}
/** @return the RGB values of the selected color.
* Each value is between [0,255].
*/
public int[] getRGB() {
return new int[] {red, green, blue};
}
/** Returns the color at the indicated point in HSB values.
*
* @param p a point relative to this panel.
* @return the HSB values at the point provided.
*/
public float[] getHSB(Point p) {
if(mode==ColorPicker.RED || mode==ColorPicker.GREEN ||
mode==ColorPicker.BLUE) {
int[] rgb = getRGB(p);
float[] hsb = Color.RGBtoHSB(rgb[0], rgb[1], rgb[2], null);
return hsb;
}
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) {
return new float[] {
(float)(theta+.25f),
(float)(r),
bri};
} else {
return new float[] {
(float)(theta+.25f),
sat,
(float)(r)
};
}
} else {
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;
return new float[] {hue, s, b};
}
}
/** Returns the color at the indicated point in RGB values.
*
* @param p a point relative to this panel.
* @return the RGB values at the point provided.
*/
public int[] getRGB(Point p) {
if(mode==ColorPicker.BRI || mode==ColorPicker.SAT ||
mode==ColorPicker.HUE) {
float[] hsb = getHSB(p);
int rgb = Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]);
int r = (rgb & 0xff0000) >> 16;
int g = (rgb & 0xff00) >> 8;
int b = (rgb & 0xff);
return new int[] {r, g, b};
}
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));
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) {
return new int[] {red, x2, y2};
} else if(mode==ColorPicker.GREEN) {
return new int[] {x2, green, y2};
} else {
return new int[] {x2, y2, blue};
}
}
/** Sets the selected color of this panel.
* If this panel is in RED, GREEN, or BLUE mode, then
* this method converts these values to RGB coordinates
* and calls This method may regenerate the graphic if necessary.
*
* @param h the hue value of the selected color.
* @param s the saturation value of the selected color.
* @param b the brightness value of the selected color.
*/
public void setHSB(float h,float s,float b) {
//hue is cyclic: it can be any value
h = (float)(h-Math.floor(h));
if(s<0 || s>1)
throw new IllegalArgumentException("The saturation value ("+s+") must be between [0,1]");
if(b<0 || b>1)
throw new IllegalArgumentException("The brightness value ("+b+") must be between [0,1]");
if(hue!=h || sat!=s || bri!=b) {
if(mode==ColorPicker.HUE ||
mode==ColorPicker.BRI ||
mode==ColorPicker.SAT) {
float lastHue = hue;
float lastBri = bri;
float lastSat = sat;
hue = h;
sat = s;
bri = b;
if(mode==ColorPicker.HUE) {
if(lastHue!=hue) {
regenerateImage();
}
} else if(mode==ColorPicker.SAT) {
if(lastSat!=sat) {
regenerateImage();
}
} else if(mode==ColorPicker.BRI) {
if(lastBri!=bri) {
regenerateImage();
}
}
} else {
Color c = new Color(Color.HSBtoRGB(h, s, b));
setRGB(c.getRed(), c.getGreen(), c.getBlue());
return;
}
Color c = new Color(Color.HSBtoRGB(hue, sat, bri));
red = c.getRed();
green = c.getGreen();
blue = c.getBlue();
regeneratePoint();
repaint();
fireChangeListeners();
}
}
/** 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+.5),(int)(bri*size+.5));
} 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) );
}
}
/** A row of pixel data we recycle every time we regenerate this image. */
private int[] row = new int[MAX_SIZE];
/** 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; yColorPickerPanel
*/
public ColorPickerPanel() {
setMaximumSize(new Dimension(MAX_SIZE+imagePadding.left+imagePadding.right,
MAX_SIZE+imagePadding.top+imagePadding.bottom));
setPreferredSize(new Dimension( (int)(MAX_SIZE*.75), (int)(MAX_SIZE*.75)));
setRGB(0,0,0);
addMouseListener(mouseListener);
addMouseMotionListener(mouseListener);
setFocusable(true);
addKeyListener(keyListener);
addFocusListener(focusListener);
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
addComponentListener(componentListener);
}
/** This listener will be notified when the current HSB or RGB values
* change.
*/
public void addChangeListener(ChangeListener l) {
if(changeListeners.contains(l))
return;
changeListeners.add(l);
}
/** Remove a ChangeListener
so it is no longer
* notified when the selected color changes.
*/
public void removeChangeListener(ChangeListener l) {
changeListeners.remove(l);
}
protected void fireChangeListeners() {
if(changeListeners==null)
return;
for(int a = 0; aHUE
, SAT
, BRI
, RED
, GREEN
, or BLUE
*/
public void setMode(int mode) {
if(!(mode==ColorPicker.HUE || mode==ColorPicker.SAT || mode==ColorPicker.BRI ||
mode==ColorPicker.RED || mode==ColorPicker.GREEN || mode==ColorPicker.BLUE))
throw new IllegalArgumentException("The mode must be HUE, SAT, BRI, RED, GREEN, or BLUE.");
if(this.mode==mode)
return;
this.mode = mode;
regenerateImage();
regeneratePoint();
}
/** Sets the selected color of this panel.
* setHSB
.
* setRGB
.
*