001package com.hfg.graphics;
002
003
004import com.hfg.html.HTMLTag;
005import com.hfg.util.StringUtil;
006import com.hfg.util.Value;
007import com.hfg.xml.XMLName;
008import com.hfg.xml.XMLTag;
009import com.hfg.xml.XMLTaggable;
010
011import java.lang.reflect.Constructor;
012
013//------------------------------------------------------------------------------
014/**
015 Interface for color assignment.
016 <div>
017 @author J. Alex Taylor, hairyfatguy.com
018 </div>
019 */
020//------------------------------------------------------------------------------
021// com.hfg Library
022//
023// This library is free software; you can redistribute it and/or
024// modify it under the terms of the GNU Lesser General Public
025// License as published by the Free Software Foundation; either
026// version 2.1 of the License, or (at your option) any later version.
027//
028// This library is distributed in the hope that it will be useful,
029// but WITHOUT ANY WARRANTY; without even the implied warranty of
030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
031// Lesser General Public License for more details.
032//
033// You should have received a copy of the GNU Lesser General Public
034// License along with this library; if not, write to the Free Software
035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
036//
037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
038// jataylor@hairyfatguy.com
039//------------------------------------------------------------------------------
040public interface Colorist extends XMLTaggable, Comparable<Colorist>
041{
042   public ColorSpec colorByNumber(Number inValue);
043
044   public ColorSpec colorByNumber(Value inValue);
045
046   public HTMLTag toDisplayHTML();
047
048
049   static final XMLName XML_COLORIST   = new XMLName("Colorist");
050   static final XMLName XML_CLASS_ATT  = new XMLName("class");
051
052   //###########################################################################
053   // PUBLIC FUNCTIONS
054   //###########################################################################
055
056   //---------------------------------------------------------------------------
057   public static Colorist instantiate(XMLTag inXMLTag)
058   {
059      Colorist colorist;
060
061      String classname = inXMLTag.getAttributeValue(XML_CLASS_ATT);
062      if (! StringUtil.isSet(classname))
063      {
064         throw new RuntimeException("No Colorist classname defined in the root XML tag!");
065      }
066
067      try
068      {
069         Class clazz = Class.forName(classname);
070
071         try
072         {
073            Constructor constructor = clazz.getConstructor(XMLTag.class);
074            colorist = (Colorist) constructor.newInstance(inXMLTag);
075         }
076         catch (NoSuchMethodException e)
077         {
078            Constructor constructor = clazz.getConstructor();
079            colorist = (Colorist) constructor.newInstance();
080         }
081      }
082      catch (Exception e)
083      {
084         throw new RuntimeException("Problem instantiating a Colorist from XML!", e);
085      }
086
087      return colorist;
088   }
089}