001package com.hfg.graphics;
002
003import com.hfg.html.HTMLTag;
004import com.hfg.html.Span;
005import com.hfg.html.attribute.HTMLColor;
006import com.hfg.util.CompareUtil;
007import com.hfg.util.Value;
008import com.hfg.util.ValueImpl;
009import com.hfg.xml.XMLName;
010import com.hfg.xml.XMLTag;
011
012import java.util.HashMap;
013import java.util.Map;
014
015
016//------------------------------------------------------------------------------
017
018/**
019 Colorist implementation that uses ColorPalette.
020 <div>
021 @author J. Alex Taylor, hairyfatguy.com
022 </div>
023 */
024//------------------------------------------------------------------------------
025// com.hfg Library
026//
027// This library is free software; you can redistribute it and/or
028// modify it under the terms of the GNU Lesser General Public
029// License as published by the Free Software Foundation; either
030// version 2.1 of the License, or (at your option) any later version.
031//
032// This library is distributed in the hope that it will be useful,
033// but WITHOUT ANY WARRANTY; without even the implied warranty of
034// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
035// Lesser General Public License for more details.
036//
037// You should have received a copy of the GNU Lesser General Public
038// License along with this library; if not, write to the Free Software
039// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
040//
041// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
042// jataylor@hairyfatguy.com
043//------------------------------------------------------------------------------
044public class PaletteColorist<T extends Number> implements Colorist
045{
046
047   private ColorPaletteImpl mColorPalette;
048   private Map<String, ColorSpec> mColorSpecMap;
049
050
051   //###########################################################################
052   // CONSTRUCTORS
053   //###########################################################################
054
055   //---------------------------------------------------------------------------
056   public PaletteColorist(ColorPaletteImpl inColorPalette)
057   {
058      mColorPalette = inColorPalette;
059   }
060
061
062   //---------------------------------------------------------------------------
063   public PaletteColorist(XMLTag inXMLTag)
064   {
065      inXMLTag.verifyTagName(XML_COLORIST);
066
067      XMLTag colorPaletteTag = inXMLTag.getRequiredSubtagByName(ColorPalette.XML_COLOR_PALETTE);
068      mColorPalette = new ColorPaletteImpl(colorPaletteTag);
069   }
070
071
072
073   //###########################################################################
074   // PUBLIC METHODS
075   //###########################################################################
076
077   //---------------------------------------------------------------------------
078   public ColorPalette getColorPalette()
079   {
080      return mColorPalette;
081   }
082
083
084   //---------------------------------------------------------------------------
085   public XMLTag toXMLTag()
086   {
087      XMLTag tag = new XMLTag(XML_COLORIST);
088      tag.setAttribute(XML_CLASS_ATT, getClass().getName());
089
090      tag.addSubtag(mColorPalette.toXMLTag());
091
092      return tag;
093   }
094
095   //---------------------------------------------------------------------------
096   @Override
097   public HTMLTag toDisplayHTML()
098   {
099      Span span = new Span();
100
101      /*(
102      TODO
103      if (CollectionUtil.hasValues(mRangeMap))
104      {
105         List<Range> orderedRanges = new ArrayList<>(mRangeMap.keySet());
106         Collections.sort(orderedRanges);
107
108         for (Range<T> range : orderedRanges)
109         {
110            String rangeText = (range.getStart() != null ? range.getStart() + "" : (range.getEnd() != null ? "-" : "") + "&#8734;") // Entity is for the infinity symbol
111                  + (CompareUtil.compare(range.getStart(), range.getEnd()) != 0 ? " - " + (range.getEnd() != null ? range.getEnd() + "" : "&#8734;") : "");
112
113            Span rangeSpan = span.addSpan();
114            rangeSpan.addContentWithoutEscaping(rangeText);
115            rangeSpan.addStyle("padding: 2px 8px");
116
117            ColorSpec colorSpec = mRangeMap.get(range);
118            if (colorSpec != null)
119            {
120               if (colorSpec.getBackgroundColor() != null)
121               {
122                  rangeSpan.addStyle(CSS.bgColor(colorSpec.getBackgroundColor()));
123               }
124
125               if (colorSpec.getForegroundColor() != null)
126               {
127                  rangeSpan.addStyle(CSS.color(colorSpec.getForegroundColor()));
128               }
129            }
130         }
131      }*/
132
133      return span;
134   }
135
136   //---------------------------------------------------------------------------
137   @Override
138   public int hashCode()
139   {
140      int hashcode = 1;
141
142      if (mColorPalette != null)
143      {
144         hashcode += 31 * mColorPalette.hashCode();
145      }
146
147      return hashcode;
148   }
149
150   //---------------------------------------------------------------------------
151   @Override
152   public boolean equals(Object inObj2)
153   {
154      return (inObj2 != null
155            && inObj2 instanceof PaletteColorist
156            && 0 == compareTo((PaletteColorist) inObj2));
157   }
158
159   //---------------------------------------------------------------------------
160   @Override
161   public int compareTo(Colorist inObj2)
162   {
163      int result = -1;
164
165      if (this == inObj2)
166      {
167         result = 0;
168      }
169      else if (inObj2 instanceof PaletteColorist)
170      {
171         if (inObj2 != null)
172         {
173            result = CompareUtil.compare(mColorPalette, ((PaletteColorist)inObj2).mColorPalette);
174         }
175      }
176
177      return result;
178   }
179
180
181
182   //---------------------------------------------------------------------------
183   @Override
184   public ColorSpec colorByNumber(Value inValue)
185   {
186      ColorSpec colorSpec = new ColorSpec();
187
188      if(mColorSpecMap == null)
189      {
190         mColorSpecMap = new HashMap<>(25);
191      }
192
193      String key = inValue.stringValue();
194
195      if(mColorSpecMap.containsKey(key))
196      {
197         colorSpec = mColorSpecMap.get(key);
198      }
199      else
200      {
201         HTMLColor color = mColorPalette.nextColor();
202         colorSpec.setBackgroundColor(color);
203         colorSpec.setForegroundColor(color.getContrastingColor());
204         mColorSpecMap.put(key, colorSpec);
205      }
206
207      return colorSpec;
208   }
209
210   //---------------------------------------------------------------------------
211   @Override
212   public ColorSpec colorByNumber(Number inValue)
213   {
214      return colorByNumber(new ValueImpl(inValue));
215   }
216
217
218
219
220}