001/* ========================================================================
002 * JCommon : a free general purpose class library for the Java(tm) platform
003 * ========================================================================
004 *
005 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006 * 
007 * Project Info:  http://www.jfree.org/jcommon/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it 
010 * under the terms of the GNU Lesser General Public License as published by 
011 * the Free Software Foundation; either version 2.1 of the License, or 
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but 
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022 * USA.  
023 *
024 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025 * in the United States and other countries.]
026 * 
027 * -------------------
028 * PaintUtilities.java
029 * -------------------
030 * (C) Copyright 2003-2005, by Object Refinery Limited.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 * $Id: PaintUtilities.java,v 1.10 2007/11/02 17:50:37 taqua Exp $
036 *
037 * Changes
038 * -------
039 * 13-Nov-2003 : Version 1 (DG);
040 * 04-Oct-2004 : Renamed PaintUtils --> PaintUtilities (DG);
041 * 23-Feb-2005 : Rewrote equal() method with less indenting required (DG);
042 *
043 */
044
045package org.jfree.util;
046
047import java.awt.Color;
048import java.awt.GradientPaint;
049import java.awt.Paint;
050import java.lang.reflect.Field;
051import java.lang.reflect.Modifier;
052
053/**
054 * Utility code that relates to <code>Paint</code> objects.
055 *
056 * @author David Gilbert
057 */
058public class PaintUtilities {
059
060    /**
061     * Private constructor prevents object creation.
062     */
063    private PaintUtilities() {
064    }
065
066    /**
067     * Returns <code>true</code> if the two <code>Paint</code> objects are equal 
068     * OR both <code>null</code>.  This method handles
069     * <code>GradientPaint</code> as a special case.
070     *
071     * @param p1  paint 1 (<code>null</code> permitted).
072     * @param p2  paint 2 (<code>null</code> permitted).
073     *
074     * @return A boolean.
075     */
076    public static boolean equal(final Paint p1, final Paint p2) {
077
078        // handle cases where either or both arguments are null
079        if (p1 == null) {
080            return (p2 == null);   
081        }
082        if (p2 == null) {
083            return false;   
084        }
085        
086        boolean result = false;
087        // handle GradientPaint as a special case...
088        if (p1 instanceof GradientPaint && p2 instanceof GradientPaint) {
089            final GradientPaint gp1 = (GradientPaint) p1;
090            final GradientPaint gp2 = (GradientPaint) p2;
091            result = gp1.getColor1().equals(gp2.getColor1()) 
092                && gp1.getColor2().equals(gp2.getColor2())
093                && gp1.getPoint1().equals(gp2.getPoint1())    
094                && gp1.getPoint2().equals(gp2.getPoint2())
095                && gp1.isCyclic() == gp2.isCyclic()
096                && gp1.getTransparency() == gp1.getTransparency(); 
097        }
098        else {
099            result = p1.equals(p2);
100        }
101        return result;
102
103    }
104
105    /**
106     * Converts a color into a string. If the color is equal to one of the
107     * defined constant colors, that name is returned instead. Otherwise the
108     * color is returned as hex-string.
109     *
110     * @param c the color.
111     * @return the string for this color.
112     */
113    public static String colorToString (final Color c)
114    {
115      try {
116          final Field[] fields = Color.class.getFields();
117          for (int i = 0; i < fields.length; i++) {
118              final Field f = fields[i];
119              if (Modifier.isPublic(f.getModifiers())
120                  && Modifier.isFinal(f.getModifiers())
121                  && Modifier.isStatic(f.getModifiers())) {
122                  final String name = f.getName();
123                  final Object oColor = f.get(null);
124                  if (oColor instanceof Color) {
125                      if (c.equals(oColor)) {
126                          return name;
127                      }
128                  }
129              }
130          }
131      }
132      catch (Exception e) {
133          //
134      }
135
136      // no defined constant color, so this must be a user defined color
137      final String color = Integer.toHexString(c.getRGB() & 0x00ffffff);
138      final StringBuffer retval = new StringBuffer(7);
139      retval.append("#");
140
141      final int fillUp = 6 - color.length();
142      for (int i = 0; i < fillUp; i++) {
143          retval.append("0");
144      }
145
146      retval.append(color);
147      return retval.toString();
148    }
149
150    /**
151     * Converts a given string into a color.
152     *
153     * @param value the string, either a name or a hex-string.
154     * @return the color.
155     */
156    public static Color stringToColor (final String value)
157    {
158      if (value == null) {
159          return Color.black;
160      }
161      try {
162          // get color by hex or octal value
163          return Color.decode(value);
164      }
165      catch (NumberFormatException nfe) {
166          // if we can't decode lets try to get it by name
167          try {
168              // try to get a color by name using reflection
169              final Field f = Color.class.getField(value);
170
171              return (Color) f.get(null);
172          }
173          catch (Exception ce) {
174              Log.info("No such Color : " + value);
175              // if we can't get any color return black
176              return Color.black;
177          }
178      }
179    }
180}