001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: ColorFormatter.java,v $
023   Revision 1.7  2004/05/05 22:47:37  markl
024   comment block updates
025
026   Revision 1.6  2003/01/19 09:34:27  markl
027   Javadoc & comment header updates.
028
029   Revision 1.5  2001/03/12 06:10:57  markl
030   Javadoc cleanup.
031
032   Revision 1.4  2001/03/12 02:18:28  markl
033   Source code cleanup.
034
035   Revision 1.3  2000/10/11 10:44:57  markl
036   Made the color array public.
037
038   Revision 1.2  1999/01/10 03:37:18  markl
039   added GPL header & RCS tag
040   ----------------------------------------------------------------------------
041*/
042
043package kiwi.text;
044
045import java.awt.Color;
046
047/** This class provides methods for converting colors to and from string
048  * representations. Known colors (those defined as constants in the
049  * <code>Color</code> class) are converted directly to or from symbolic names
050  * such as "green" or "magenta". Other colors are converted to or from RGB
051  * specifications in the format #RRGGBB - a '#' followed by 6 hexadecimal
052  * digits.
053  *
054  * @see java.awt.Color
055  *
056  * @author Mark Lindner
057  */
058
059public class ColorFormatter
060  {
061  private static String colorNames[] = { "black", "blue", "cyan", "gray",
062                                         "green", "magenta", "orange", "pink",
063                                         "red", "white", "yellow" };
064  /** A list of basic colors. */
065  public static Color basicColors[] = { Color.black, Color.blue, Color.cyan,
066                                       Color.gray, Color.green, Color.magenta,
067                                       Color.orange, Color.pink, Color.red,
068                                       Color.white, Color.yellow };
069
070  private ColorFormatter() {}
071
072  /** Get a name for a color. Returns a name for the color, if the color is one
073    * of the colors predefined in the <code>Color</code> class: black, blue,
074    * cyan, gray, green, magenta, orange, pink, red, white, or yellow.
075    *
076    * @param c The color.
077    * @return The name of the color, or <code>null</code> if <code>c</code> is
078    * not one of the colors listed above.
079    *
080    * @see #nameForColor
081    */
082  
083  public static String nameForColor(Color c)
084    {
085    for(int i = 0; i < basicColors.length; i++)
086      if(c.equals(basicColors[i]))
087        return(colorNames[i]);
088
089    return(null);
090    }
091
092  /** Get a color for a name. Returns a color for a color name, if the name
093    * identifies one of the colors predefined in the <code>Color</code> class.
094    *
095    * @param name The name of the color.
096    * @return The <code>Color</code> object for the given name, or
097    * <code>null</code> if <code>name</code> does not identify one of the
098    * colors listed above.
099    *
100    * @see #colorForName
101    */
102  
103  public static Color colorForName(String name)
104    {
105    for(int i = 0; i < basicColors.length; i++)
106      if(name.equalsIgnoreCase(colorNames[i]))
107        return(basicColors[i]);
108
109    return(null);
110    }
111
112  /** Format a color as a string. Returns a string representation of the given
113    * color as either a symbolic name (if the color is one of the colors
114    * predefined in the <code>Color</code> class), or a hex representation of
115    * the color in the format <tt>#RRGGBB</tt>.
116    *
117    * @param color The <code>Color</code> to parse.
118    * @return A string representation of the color.
119    *
120    * @see #parse
121    */
122  
123  public static String format(Color color)
124    {
125    String s = nameForColor(color);
126    if(s != null)
127      return(s);
128
129    // otherwise format it as #RRGGBB
130
131    StringBuffer sb = new StringBuffer();
132    sb.append('#');
133
134    s = Integer.toHexString(color.getRGB() & 0x1FFFFFF);
135    sb.append(s.substring(1));
136
137    return(sb.toString());
138    }
139
140  /** Parse a color representation, returning an appropriate <code>Color</code>
141    * object.
142    *
143    * @param name The name of the color; one of the strings <i>black, blue,
144    * cyan, gray, green, magenta, orange, pink, red, white, yellow</i>, or an
145    * RGB color specification of the form <tt>#RRGGBB</tt>.
146    * @return An appropriate <code>Color</code> object.
147    * @exception kiwi.text.ParsingException If <code>name</code> is an
148    * invalid color representation.
149    *
150    * @see #format
151    */
152
153  public static Color parse(String name) throws ParsingException
154    {
155    Color r;
156
157    if(name.charAt(0) == '#')
158      {
159      // process #RRGGBB specification
160      
161      try
162        {
163        Integer rgb = Integer.valueOf(name.substring(1), 16);
164        return(new Color(rgb.intValue()));
165        }
166      catch(NumberFormatException e)
167        {
168        throw(new ParsingException("Invalid RGB color syntax: " + name));
169        }
170      }
171    else
172      {
173      r = colorForName(name);
174      if(r == null)
175        throw(new ParsingException("Unknown color name: " + name));
176      else
177        return(r);
178      }
179    }
180  
181  }
182
183/* end of source file */