001     
002    /*****************************************************************************
003    
004        jBudget.
005        Copyright (C) 2006  Dmitri Petrov
006    
007        This program is free software: you can redistribute it and/or modify
008        it under the terms of the GNU General Public License as published by
009        the Free Software Foundation, either version 3 of the License, or
010        (at your option) any later version.
011    
012        This program is distributed in the hope that it will be useful,
013        but WITHOUT ANY WARRANTY; without even the implied warranty of
014        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015        GNU General Public License for more details.
016    
017        You should have received a copy of the GNU General Public License
018        along with this program.  If not, see <http://www.gnu.org/licenses/>.
019     
020    /*****************************************************************************
021    
022    /*
023     * ColorParser.java
024     *
025     * Created on April 17, 2006, 10:10 AM
026     */
027    
028    package org.jbudget.util;
029    
030    import java.awt.Color;
031    import java.util.regex.Matcher;
032    import java.util.regex.Pattern;
033    
034    /** A class that contains functions for 
035     *  saving Colors as strings and creating Color objects from strings.
036     *
037     * @author petrov
038     */
039    public class ColorParser
040    {
041      /** A regular expression pattern that is used for parsing the colors */
042      private static Pattern colorPattern = 
043          Pattern.compile("\\s*^\\s*(\\d+)\\s+(\\d+)\\s+(\\d+)\\s*$.*");
044      
045      /** No need for any instances. */
046      private ColorParser()
047      {
048      }
049      
050      /** Creates a string representation of a color. We assume that the alpha is
051       * always 1, so only three rgb values are saved.
052       */
053      public static String colorToString(Color color)
054      {
055        StringBuffer buffer = new StringBuffer();
056        buffer.append(Integer.toString(color.getRed()));
057        buffer.append(" ");
058        buffer.append(Integer.toString(color.getGreen()));
059        buffer.append(" ");
060        buffer.append(Integer.toString(color.getBlue()));
061        return buffer.toString();
062      }
063      
064      /** Parses the string and returns a color object that is represented by the
065       *  string. The string should contain three integer rgb values separated by
066       * white space
067       *@throws IllegalArgumentException if the string can not be parsed.
068       */
069      public static Color parse(String string)
070      {
071        Matcher matcher = colorPattern.matcher(string);
072        if (! matcher.matches())
073          throw new IllegalArgumentException("Can not parse string '" + string +
074              "' into a color");
075        
076        try
077        {
078          int r = Integer.parseInt(matcher.group(1));
079          int g = Integer.parseInt(matcher.group(2));
080          int b = Integer.parseInt(matcher.group(3));
081          return new Color(r,g,b);
082        } 
083        catch (NumberFormatException e)
084        {
085          throw new IllegalArgumentException("Can not parse string '" + string +
086              "' into a color");
087        }
088      }
089      
090    }