001package com.hfg.graphics;
002
003import java.awt.Color;
004
005import com.hfg.util.CompareUtil;
006import com.hfg.xml.XMLTag;
007
008//------------------------------------------------------------------------------
009/**
010 Encapsulates foreground and background color specifications. The default
011 foreground color is BLACK and no default background color is defined.
012 <div>
013  @author J. Alex Taylor, hairyfatguy.com
014 </div>
015 */
016//------------------------------------------------------------------------------
017// com.hfg XML/HTML Coding Library
018//
019// This library is free software; you can redistribute it and/or
020// modify it under the terms of the GNU Lesser General Public
021// License as published by the Free Software Foundation; either
022// version 2.1 of the License, or (at your option) any later version.
023//
024// This library is distributed in the hope that it will be useful,
025// but WITHOUT ANY WARRANTY; without even the implied warranty of
026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027// Lesser General Public License for more details.
028//
029// You should have received a copy of the GNU Lesser General Public
030// License along with this library; if not, write to the Free Software
031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
032//
033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
034// jataylor@hairyfatguy.com
035//------------------------------------------------------------------------------
036
037
038public class ColorSpec implements Cloneable, Comparable<ColorSpec>
039{
040    //##########################################################################
041    // PRIVATE FIELDS
042    //##########################################################################
043
044    private Color mForegroundColor = Color.BLACK;
045    private Color mBackgroundColor;
046
047
048    private static final String COLORSPEC      = "ColorSpec";
049    private static final String FOREGROUND_ATT = "foreground";
050    private static final String BACKGROUND_ATT = "background";
051
052    //##########################################################################
053    // CONSTRUCTORS
054    //##########################################################################
055
056    //--------------------------------------------------------------------------
057    public ColorSpec()
058    {
059    }
060
061    //--------------------------------------------------------------------------
062    public ColorSpec(XMLTag inXMLTag)
063    {
064        inXMLTag.verifyTagName(COLORSPEC);
065
066        if (inXMLTag.hasAttribute(FOREGROUND_ATT))
067        {
068            setForegroundColor(Color.decode(inXMLTag.getAttributeValue(FOREGROUND_ATT)));
069        }
070
071        if (inXMLTag.hasAttribute(BACKGROUND_ATT))
072        {
073            setBackgroundColor(Color.decode(inXMLTag.getAttributeValue(BACKGROUND_ATT)));
074        }
075    }
076
077    //##########################################################################
078    // PUBLIC METHODS
079    //##########################################################################
080
081    //--------------------------------------------------------------------------
082    @Override
083    public String toString()
084    {
085        return "#" + ColorUtil.colorToHex(getForegroundColor()) + ";" + "#" + ColorUtil.colorToHex(getBackgroundColor());
086    }
087
088    //--------------------------------------------------------------------------
089    @Override
090    public ColorSpec clone()
091    {
092        ColorSpec obj;
093        try
094        {
095            obj = (ColorSpec) super.clone();
096        }
097        catch (CloneNotSupportedException e)
098        {
099            throw new RuntimeException(e);
100        }
101
102        return obj;
103    }
104
105    //--------------------------------------------------------------------------
106    @Override
107    public int hashCode()
108    {
109        int hash = 5;
110        hash += 89 * (mForegroundColor != null ? mForegroundColor.hashCode() : 0);
111        hash += 89 * (mBackgroundColor != null ? mBackgroundColor.hashCode() : 0);
112        return hash;
113    }
114
115    //---------------------------------------------------------------------------
116    @Override
117    public boolean equals(Object inObj2)
118    {
119        return (inObj2 != null
120              && inObj2 instanceof ColorSpec
121              && 0 == compareTo((ColorSpec) inObj2));
122    }
123
124    //---------------------------------------------------------------------------
125    @Override
126    public int compareTo(ColorSpec inObj2)
127    {
128        int result = -1;
129
130        if (this == inObj2)
131        {
132            result = 0;
133        }
134        else
135        {
136            if (inObj2 != null)
137            {
138                result = CompareUtil.compare(getForegroundColor(), inObj2.getForegroundColor());
139
140                if (0 == result)
141                {
142                    result = CompareUtil.compare(getBackgroundColor(), inObj2.getBackgroundColor());
143                }
144            }
145        }
146
147        return result;
148    }
149
150    //--------------------------------------------------------------------------
151    /**
152     Sets the foreground color; null values are allowed.
153     @param inValue the color to use for the foreground
154     @return this ColorSpec object to enable method chaining
155     */
156    public ColorSpec setForegroundColor(Color inValue)
157    {
158        mForegroundColor = inValue;
159        return this;
160    }
161
162    //--------------------------------------------------------------------------
163    public Color getForegroundColor()
164    {
165        return mForegroundColor;
166    }
167
168    //--------------------------------------------------------------------------
169    /**
170     Sets the background color; null values are allowed.
171     @param inValue the color to use for the background
172     @return this ColorSpec object to enable method chaining
173     */
174    public ColorSpec setBackgroundColor(Color inValue)
175    {
176        mBackgroundColor = inValue;
177        return this;
178    }
179
180    //--------------------------------------------------------------------------
181    public Color getBackgroundColor()
182    {
183        return mBackgroundColor;
184    }
185
186    //--------------------------------------------------------------------------
187    public XMLTag toXMLTag()
188    {
189        XMLTag tag = new XMLTag(COLORSPEC);
190
191        if (getForegroundColor() != null)
192        {
193            tag.setAttribute(FOREGROUND_ATT, "#" + ColorUtil.colorToHex(getForegroundColor()));
194        }
195
196        if (getBackgroundColor() != null)
197        {
198            tag.setAttribute(BACKGROUND_ATT, "#" + ColorUtil.colorToHex(getBackgroundColor()));
199        }
200
201        return tag;
202    }
203}