001package com.hfg.xml.msofficexml.xlsx;
002
003
004import java.awt.Color;
005import java.util.HashMap;
006import java.util.Map;
007
008//------------------------------------------------------------------------------
009/**
010 Colors from the Excel indexed color palette. See section 18.8.27 of ECMA-376 Part I
011
012 @author J. Alex Taylor, hairyfatguy.com
013 */
014//------------------------------------------------------------------------------
015// com.hfg XML/HTML Coding Library
016//
017// This library is free software; you can redistribute it and/or
018// modify it under the terms of the GNU Lesser General Public
019// License as published by the Free Software Foundation; either
020// version 2.1 of the License, or (at your option) any later version.
021//
022// This library is distributed in the hope that it will be useful,
023// but WITHOUT ANY WARRANTY; without even the implied warranty of
024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
025// Lesser General Public License for more details.
026//
027// You should have received a copy of the GNU Lesser General Public
028// License along with this library; if not, write to the Free Software
029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030//
031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
032// jataylor@hairyfatguy.com
033//------------------------------------------------------------------------------
034
035public class ExcelIndexedColor extends Color
036{
037   private static Map<Integer, ExcelIndexedColor> sIndexedColorMap = new HashMap<Integer, ExcelIndexedColor>();
038   private static Map<String, ExcelIndexedColor>  sNamedColorMap   = new HashMap<String, ExcelIndexedColor>(8);
039
040   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,   0,   0)' ></span> (  0,   0,   0) */
041   public static final ExcelIndexedColor COLOR_0              = new ExcelIndexedColor(  0,   0,   0).setIndex(0).setName("Black");
042   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 255, 255)' ></span> (255, 255, 255) */
043   public static final ExcelIndexedColor COLOR_1              = new ExcelIndexedColor(255, 255, 255).setIndex(1).setName("White");
044   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255,   0,   0)' ></span> (255,   0,   0) */
045   public static final ExcelIndexedColor COLOR_2              = new ExcelIndexedColor(255,   0,   0).setIndex(2).setName("Red");
046   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 255,   0)' ></span> (  0, 255,   0) */
047   public static final ExcelIndexedColor COLOR_3              = new ExcelIndexedColor(  0, 255,   0).setIndex(3).setName("Green");
048   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,   0, 255)' ></span> (  0,   0, 255) */
049   public static final ExcelIndexedColor COLOR_4              = new ExcelIndexedColor(  0,   0, 255).setIndex(4).setName("Blue");
050   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 255,   0)' ></span> (255, 255,   0) */
051   public static final ExcelIndexedColor COLOR_5              = new ExcelIndexedColor(255, 255,   0).setIndex(5).setName("Yellow");
052   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255,   0, 255)' ></span> (255,   0, 255) */
053   public static final ExcelIndexedColor COLOR_6              = new ExcelIndexedColor(255,   0, 255).setIndex(6).setName("Magenta");
054   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 255, 255)' ></span> (  0, 255, 255) */
055   public static final ExcelIndexedColor COLOR_7              = new ExcelIndexedColor(  0, 255, 255).setIndex(7).setName("Cyan");
056
057   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,   0,   0)' ></span> (  0,   0,   0) */
058   public static final ExcelIndexedColor COLOR_8              = new ExcelIndexedColor(  0,   0,   0).setIndex(8);
059   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 255, 255)' ></span> (255, 255, 255) */
060   public static final ExcelIndexedColor COLOR_9              = new ExcelIndexedColor(255, 255, 255).setIndex(9);
061   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255,   0,   0)' ></span> (255,   0,   0) */
062   public static final ExcelIndexedColor COLOR_10              = new ExcelIndexedColor(255,   0,   0).setIndex(10);
063   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 255,   0)' ></span> (  0, 255,   0) */
064   public static final ExcelIndexedColor COLOR_11              = new ExcelIndexedColor(  0, 255,   0).setIndex(11);
065   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,   0, 255)' ></span> (  0,   0, 255) */
066   public static final ExcelIndexedColor COLOR_12              = new ExcelIndexedColor(  0,   0, 255).setIndex(12);
067   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 255,   0)' ></span> (255, 255,   0) */
068   public static final ExcelIndexedColor COLOR_13              = new ExcelIndexedColor(255, 255,   0).setIndex(13);
069   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255,   0, 255)' ></span> (255,   0, 255) */
070   public static final ExcelIndexedColor COLOR_14              = new ExcelIndexedColor(255,   0, 255).setIndex(14);
071   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 255, 255)' ></span> (  0, 255, 255) */
072   public static final ExcelIndexedColor COLOR_15              = new ExcelIndexedColor(  0, 255, 255).setIndex(15);
073   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(128,   0,   0)' ></span> (128,   0,   0) */
074   public static final ExcelIndexedColor COLOR_16              = new ExcelIndexedColor(128,   0,   0).setIndex(16);
075   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 128,   0)' ></span> (  0, 128,   0) */
076   public static final ExcelIndexedColor COLOR_17             = new ExcelIndexedColor(  0, 128,   0).setIndex(17);
077   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,   0, 128)' ></span> (  0,   0, 128) */
078   public static final ExcelIndexedColor COLOR_18             = new ExcelIndexedColor(  0,   0, 128).setIndex(18);
079   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(128, 128,   0)' ></span> (128, 128,   0) */
080   public static final ExcelIndexedColor COLOR_19             = new ExcelIndexedColor(128, 128,   0).setIndex(19);
081   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(128,   0, 128)' ></span> (128,   0, 128) */
082   public static final ExcelIndexedColor COLOR_20             = new ExcelIndexedColor(128,   0, 128).setIndex(20);
083   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 128, 128)' ></span> (  0, 128, 128) */
084   public static final ExcelIndexedColor COLOR_21             = new ExcelIndexedColor(  0, 128, 128).setIndex(21);
085   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(192, 192, 192)' ></span> (192, 192, 192) */
086   public static final ExcelIndexedColor COLOR_22             = new ExcelIndexedColor(192, 192, 192).setIndex(22);
087   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(128, 128, 128)' ></span> (128, 128, 128) */
088   public static final ExcelIndexedColor COLOR_23             = new ExcelIndexedColor(128, 128, 128).setIndex(23);
089   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(153, 153, 255)' ></span> (153, 153, 255) */
090   public static final ExcelIndexedColor COLOR_24             = new ExcelIndexedColor(153, 153, 255).setIndex(24);
091   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(153,  51, 102)' ></span> (153,  51, 102) */
092   public static final ExcelIndexedColor COLOR_25             = new ExcelIndexedColor(153,  51, 102).setIndex(25);
093   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 255, 204)' ></span> (255, 255, 204) */
094   public static final ExcelIndexedColor COLOR_26             = new ExcelIndexedColor(255, 255, 204).setIndex(26);
095   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(204, 255, 255)' ></span> (204, 255, 255) */
096   public static final ExcelIndexedColor COLOR_27             = new ExcelIndexedColor(204, 255, 255).setIndex(27);
097   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(102,   0, 102)' ></span> (102,   0, 102) */
098   public static final ExcelIndexedColor COLOR_28             = new ExcelIndexedColor(102,   0, 102).setIndex(28);
099   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 128, 128)' ></span> (255, 128, 128) */
100   public static final ExcelIndexedColor COLOR_29             = new ExcelIndexedColor(255, 128, 128).setIndex(29);
101   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 102, 204)' ></span> (  0, 102, 204) */
102   public static final ExcelIndexedColor COLOR_30             = new ExcelIndexedColor(  0, 102, 204).setIndex(30);
103   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(204, 204, 255)' ></span> (204, 204, 255) */
104   public static final ExcelIndexedColor COLOR_31             = new ExcelIndexedColor(204, 204, 255).setIndex(31);
105   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,   0, 128)' ></span> (  0,   0, 128) */
106   public static final ExcelIndexedColor COLOR_32             = new ExcelIndexedColor(  0,   0, 128).setIndex(32);
107   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255,   0, 255)' ></span> (255,   0, 255) */
108   public static final ExcelIndexedColor COLOR_33             = new ExcelIndexedColor(255,   0, 255).setIndex(33);
109   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 255,   0)' ></span> (255, 255,   0) */
110   public static final ExcelIndexedColor COLOR_34             = new ExcelIndexedColor(255, 255,   0).setIndex(34);
111   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 255, 255)' ></span> (  0, 255, 255) */
112   public static final ExcelIndexedColor COLOR_35             = new ExcelIndexedColor(  0, 255, 255).setIndex(35);
113   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(128,   0, 128)' ></span> (128,   0, 128) */
114   public static final ExcelIndexedColor COLOR_36             = new ExcelIndexedColor(128,   0, 128).setIndex(36);
115   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(128,   0,   0)' ></span> (128,   0,   0) */
116   public static final ExcelIndexedColor COLOR_37             = new ExcelIndexedColor(128,   0,   0).setIndex(37);
117   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 128, 128)' ></span> (  0, 128, 128) */
118   public static final ExcelIndexedColor COLOR_38             = new ExcelIndexedColor(  0, 128, 128).setIndex(38);
119   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,   0, 255)' ></span> (  0,   0, 255) */
120   public static final ExcelIndexedColor COLOR_39             = new ExcelIndexedColor(  0,   0, 255).setIndex(39);
121   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0, 204, 255)' ></span> (  0, 204, 255) */
122   public static final ExcelIndexedColor COLOR_40             = new ExcelIndexedColor(  0, 204, 255).setIndex(40);
123   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(204, 255, 255)' ></span> (204, 255, 255) */
124   public static final ExcelIndexedColor COLOR_41             = new ExcelIndexedColor(204, 255, 255).setIndex(41);
125   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(204, 255, 204)' ></span> (204, 255, 204) */
126   public static final ExcelIndexedColor COLOR_42             = new ExcelIndexedColor(204, 255, 204).setIndex(42);
127   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 255, 153)' ></span> (255, 255, 153) */
128   public static final ExcelIndexedColor COLOR_43             = new ExcelIndexedColor(255, 255, 153).setIndex(43);
129   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(153, 204, 255)' ></span> (153, 204, 255) */
130   public static final ExcelIndexedColor COLOR_44             = new ExcelIndexedColor(153, 204, 255).setIndex(44);
131   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 153, 204)' ></span> (255, 153, 204) */
132   public static final ExcelIndexedColor COLOR_45             = new ExcelIndexedColor(255, 153, 204).setIndex(45);
133   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(204, 153, 255)' ></span> (204, 153, 255) */
134   public static final ExcelIndexedColor COLOR_46             = new ExcelIndexedColor(204, 153, 255).setIndex(46);
135   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 204, 153)' ></span> (255, 204, 153) */
136   public static final ExcelIndexedColor COLOR_47             = new ExcelIndexedColor(255, 204, 153).setIndex(47);
137   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb( 51, 102, 255)' ></span> ( 51, 102, 255) */
138   public static final ExcelIndexedColor COLOR_48             = new ExcelIndexedColor( 51, 102, 255).setIndex(48);
139   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb( 51, 204, 204)' ></span> ( 51, 204, 204) */
140   public static final ExcelIndexedColor COLOR_49             = new ExcelIndexedColor( 51, 204, 204).setIndex(49);
141   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(153, 204,   0)' ></span> (153, 204,   0) */
142   public static final ExcelIndexedColor COLOR_50             = new ExcelIndexedColor(153, 204,   0).setIndex(50);
143   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 204,   0)' ></span> (255, 204,   0) */
144   public static final ExcelIndexedColor COLOR_51             = new ExcelIndexedColor(255, 204,   0).setIndex(51);
145   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 153,   0)' ></span> (255, 153,   0) */
146   public static final ExcelIndexedColor COLOR_52             = new ExcelIndexedColor(255, 153,   0).setIndex(52);
147   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(255, 102,   0)' ></span> (255, 102,   0) */
148   public static final ExcelIndexedColor COLOR_53             = new ExcelIndexedColor(255, 102,   0).setIndex(53);
149   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(102, 102, 153)' ></span> (102, 102, 153) */
150   public static final ExcelIndexedColor COLOR_54             = new ExcelIndexedColor(102, 102, 153).setIndex(54);
151   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(150, 150, 150)' ></span> (150, 150, 150) */
152   public static final ExcelIndexedColor COLOR_55             = new ExcelIndexedColor(150, 150, 150).setIndex(55);
153   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,  51, 102)' ></span> (  0,  51, 102) */
154   public static final ExcelIndexedColor COLOR_56             = new ExcelIndexedColor(  0,  51, 102).setIndex(56);
155   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb( 51, 153, 102)' ></span> ( 51, 153, 102) */
156   public static final ExcelIndexedColor COLOR_57             = new ExcelIndexedColor( 51, 153, 102).setIndex(57);
157   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(  0,  51,   0)' ></span> (  0,  51,   0) */
158   public static final ExcelIndexedColor COLOR_58             = new ExcelIndexedColor(  0,  51,   0).setIndex(58);
159   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb( 51,  51,   0)' ></span> ( 51,  51,   0) */
160   public static final ExcelIndexedColor COLOR_59             = new ExcelIndexedColor( 51,  51,   0).setIndex(59);
161   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(153,  51,   0)' ></span> (153,  51,   0) */
162   public static final ExcelIndexedColor COLOR_60             = new ExcelIndexedColor(153,  51,   0).setIndex(60);
163   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb(153,  51, 102)' ></span> (153,  51, 102) */
164   public static final ExcelIndexedColor COLOR_61             = new ExcelIndexedColor(153,  51, 102).setIndex(61);
165   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb( 51,  51, 153)' ></span> ( 51,  51, 153) */
166   public static final ExcelIndexedColor COLOR_62             = new ExcelIndexedColor( 51,  51, 153).setIndex(62);
167   /** <span style='width:100px; height:1em; display:inline-block; background-color:rgb( 51,  51,  51)' ></span> ( 51,  51,  51) */
168   public static final ExcelIndexedColor COLOR_63             = new ExcelIndexedColor( 51,  51,  51).setIndex(63);
169   //TODO: Color 64 is System Foreground and Color 65 is System Background
170   public static final ExcelIndexedColor COLOR_64             = new ExcelIndexedColor(  0,   0,   0).setIndex(64).setName("System Foreground");
171   public static final ExcelIndexedColor COLOR_65             = new ExcelIndexedColor(  0,   0,   0).setIndex(65).setName("System Background");
172
173
174   private int    mIndex;
175   private String mName;
176
177   //##########################################################################
178   // CONSTRUCTORS
179   //##########################################################################
180
181   //---------------------------------------------------------------------------
182   private ExcelIndexedColor(int inRed, int inGreen, int inBlue)
183   {
184      super(inRed, inGreen, inBlue);
185   }
186
187   //###########################################################################
188   // PUBLIC FUNCTIONS
189   //###########################################################################
190
191   //---------------------------------------------------------------------------
192   /**
193    Returns the indexed color that is closest to the specified color.
194    */
195   public static ExcelIndexedColor findNearestMatch(Color inColor)
196   {
197      ExcelIndexedColor nearestColor = null;
198      double nearestDistance = Integer.MAX_VALUE;
199
200      for (ExcelIndexedColor indexedColor : sIndexedColorMap.values())
201      {
202         double distance = Math.sqrt(
203                       Math.pow(inColor.getRed()   - indexedColor.getRed(), 2)
204                     + Math.pow(inColor.getGreen() - indexedColor.getGreen(), 2)
205                     + Math.pow(inColor.getBlue()  - indexedColor.getBlue(), 2));
206
207         if (nearestDistance > distance)
208         {
209            nearestColor = indexedColor;
210            if (0 == distance)
211            {
212               break;
213            }
214            else
215            {
216               nearestDistance = distance;
217            }
218         }
219      }
220
221      return nearestColor;
222   }
223
224   //##########################################################################
225   // PUBLIC METHODS
226   //##########################################################################
227
228   //---------------------------------------------------------------------------
229   /**
230    Returns the object corresponding to the specified index.
231    * @param inValue color index (0 - 65)
232    */
233   public static ExcelIndexedColor valueOf(int inValue)
234   {
235      return sIndexedColorMap.get(inValue);
236   }
237
238   //---------------------------------------------------------------------------
239   /**
240    Returns the object corresponding to the specified name. Only 8 colors have names.
241    * @param inValue color name (ex: 'Blue')
242    */
243   public static ExcelIndexedColor valueOf(String inValue)
244   {
245      return sNamedColorMap.get(inValue.toUpperCase());
246   }
247
248   //---------------------------------------------------------------------------
249   /**
250    Returns the index value for this color.
251    */
252   public int getIndex()
253   {
254      return mIndex;
255   }
256
257   //---------------------------------------------------------------------------
258   /**
259    Returns the name for this color. (Only colors 1 through 8 are named.)
260    */
261   public String getName()
262   {
263      return mName;
264   }
265
266   //##########################################################################
267   // PRIVATE METHODS
268   //##########################################################################
269
270   //---------------------------------------------------------------------------
271   private ExcelIndexedColor setIndex(int inValue)
272   {
273      mIndex = inValue;
274      sIndexedColorMap.put(inValue, this);
275      return this;
276   }
277
278   //---------------------------------------------------------------------------
279   private ExcelIndexedColor setName(String inValue)
280   {
281      mName = inValue;
282      sNamedColorMap.put(inValue.toUpperCase(), this);
283      return this;
284   }
285
286}