001package armyc2.c2sd.renderer.utilities;
002
003public class Color {
004        
005    /**
006     * The color white.  In the default sRGB space.
007     */
008    public final static Color white     = new Color(255, 255, 255);
009
010    /**
011     * The color white.  In the default sRGB space.
012     * @since 1.4
013     */
014    public final static Color WHITE = white;
015
016    /**
017     * The color light gray.  In the default sRGB space.
018     */
019    public final static Color lightGray = new Color(192, 192, 192);
020
021    /**
022     * The color light gray.  In the default sRGB space.
023     * @since 1.4
024     */
025    public final static Color LIGHT_GRAY = lightGray;
026
027    /**
028     * The color gray.  In the default sRGB space.
029     */
030    public final static Color gray      = new Color(128, 128, 128);
031
032    /**
033     * The color gray.  In the default sRGB space.
034     * @since 1.4
035     */
036    public final static Color GRAY = gray;
037
038    /**
039     * The color dark gray.  In the default sRGB space.
040     */
041    public final static Color darkGray  = new Color(64, 64, 64);
042
043    /**
044     * The color dark gray.  In the default sRGB space.
045     * @since 1.4
046     */
047    public final static Color DARK_GRAY = darkGray;
048
049    /**
050     * The color black.  In the default sRGB space.
051     */
052    public final static Color black     = new Color(0, 0, 0);
053    
054    /**
055     * The color black.  In the default sRGB space.
056     * @since 1.4
057     */
058    public final static Color BLACK = black;
059    
060    /**
061     * The color red.  In the default sRGB space.
062     */
063    public final static Color red       = new Color(255, 0, 0);
064
065    /**
066     * The color red.  In the default sRGB space.
067     * @since 1.4
068     */
069    public final static Color RED = red;
070
071    /**
072     * The color pink.  In the default sRGB space.
073     */
074    public final static Color pink      = new Color(255, 175, 175);
075
076    /**
077     * The color pink.  In the default sRGB space.
078     * @since 1.4
079     */
080    public final static Color PINK = pink;
081
082    /**
083     * The color orange.  In the default sRGB space.
084     */
085    public final static Color orange    = new Color(255, 200, 0);
086
087    /**
088     * The color orange.  In the default sRGB space.
089     * @since 1.4
090     */
091    public final static Color ORANGE = orange;
092
093    /**
094     * The color yellow.  In the default sRGB space.
095     */
096    public final static Color yellow    = new Color(255, 255, 0);
097
098    /**
099     * The color yellow.  In the default sRGB space.
100     * @since 1.4
101     */
102    public final static Color YELLOW = yellow;
103
104    /**
105     * The color green.  In the default sRGB space.
106     */
107    public final static Color green     = new Color(0, 255, 0);
108
109    /**
110     * The color green.  In the default sRGB space.
111     * @since 1.4
112     */
113    public final static Color GREEN = green;
114
115    /**
116     * The color magenta.  In the default sRGB space.
117     */
118    public final static Color magenta   = new Color(255, 0, 255);
119
120    /**
121     * The color magenta.  In the default sRGB space.
122     * @since 1.4
123     */
124    public final static Color MAGENTA = magenta;
125
126    /**
127     * The color cyan.  In the default sRGB space.
128     */
129    public final static Color cyan      = new Color(0, 255, 255);
130
131    /**
132     * The color cyan.  In the default sRGB space.
133     * @since 1.4
134     */
135    public final static Color CYAN = cyan;
136
137    /**
138     * The color blue.  In the default sRGB space.
139     */
140    public final static Color blue      = new Color(0, 0, 255);
141
142    /**
143     * The color blue.  In the default sRGB space.
144     * @since 1.4
145     */
146    public final static Color BLUE = blue;
147    
148    private int _A=255;
149        private int _R=0;
150        private int _G=0;
151        private int _B=0;//default to black
152    
153    public Color(int R, int G, int B)
154    {
155        _A = 255;
156                _R = R;
157                _G = G;
158                _B = B;
159    }
160    
161    public Color(int R, int G, int B, int A)
162    {
163        _A = A;
164                _R = R;
165                _G = G;
166                _B = B;
167    }
168    
169    public Color(Color color)
170    {
171        if(color != null)
172        {
173                _A = color.getAlpha();
174                _R = color.getRed();
175                _G = color.getGreen();
176                _B = color.getBlue();
177        }
178        else
179        {
180                _A = 255;
181                _R = 0;
182                _G = 0;
183                _B = 0;
184        }
185    }
186    
187    public Color(int color)
188    {
189                _A = getAlphaFromColor(color);
190                _R = getRedFromColor(color);
191                _G = getGreenFromColor(color);
192                _B = getBlueFromColor(color);
193    }
194
195    public Color(String hexString)
196    {
197        Color temp = SymbolUtilities.getColorFromHexString(hexString);
198        if(temp != null)
199        {
200                _A = temp.getAlpha();
201                _R = temp.getRed();
202                _G = temp.getGreen();
203                _B = temp.getBlue();
204        }
205        else
206        {
207                _A = 255;
208                _R = 0;
209                _G = 0;
210                _B = 0;
211        }
212    }
213    
214    public Color(Object color)
215    {
216        
217        Color cTemp;
218        int iTemp;
219        try
220        {
221                if(color instanceof String)
222                {
223                        cTemp = SymbolUtilities.getColorFromHexString((String)color);
224                        if(cTemp != null)
225                        {
226                                _A = cTemp.getAlpha();
227                                _R = cTemp.getRed();
228                                _G = cTemp.getGreen();
229                                _B = cTemp.getBlue();
230                        }
231                }
232                else if(color instanceof Integer)
233                {
234                        iTemp = (Integer)color;
235                        _A = getAlphaFromColor(iTemp);
236                        _R = getRedFromColor(iTemp);
237                        _G = getGreenFromColor(iTemp);
238                        _B = getBlueFromColor(iTemp);
239                }
240                else
241                {
242                        _A=255;
243                        _R=0;
244                        _G=0;
245                        _B=0;
246                }
247        }
248        catch(Exception exc)
249        {
250                _A=255;
251                _R=0;
252                _G=0;
253                _B=0;
254        }
255    }
256    
257    public int toARGB()
258    {
259        int returnVal = 0;
260                returnVal = (_A << 24) + ((_R & 0xFF) << 16) + ((_G & 0xFF) << 8) + (_B & 0xFF);
261                return returnVal;
262    }
263    
264    public String toHexString()
265    {
266                return Integer.toHexString(this.toARGB());
267    }
268    
269    @Override
270    public String toString()
271    {
272        return "Color{A=" + String.valueOf(_A) + ",R=" + String.valueOf(_R) + 
273                                ",G=" + String.valueOf(_G) + ",B=" + String.valueOf(_B) + "}";
274    }
275    
276    public int getRed()
277    {
278        return _R;
279    }
280    
281    public int getGreen()
282    {
283        return _G;
284    }
285    
286    public int getBlue()
287    {
288        return _B;
289    }
290    
291    public int getAlpha()
292    {
293        return _A;
294    }
295
296        /**
297         *
298         * @param alpha 0-255
299         * @return
300         */
301        public void setAlpha(int alpha)
302        {
303                _A = alpha;
304        }
305    
306    public int toInt()
307    {
308        return android.graphics.Color.argb(_A, _R, _G, _B);
309    }
310    
311    /**
312         * get alpha value from uint
313         * */
314        private int getAlphaFromColor(int color)
315        {
316                int alpha = 255;
317                if(color > 16777215)
318                        alpha = (color >>> 24);
319                return alpha;
320        }
321        /**
322         * get red value from uint
323         * */
324        private int getRedFromColor(int color)
325        {
326                int red = 255;
327                red = (color >> 16) & 0xFF;
328                return red;
329        }
330        /**
331         * get green value from uint
332         * */
333        private int getGreenFromColor(int color)
334        {
335                int green = 255;
336                green = (color >> 8) & 0xFF;
337                return green;
338        }
339        /**
340         * get blue value from uint
341         * */
342        private int getBlueFromColor(int color)
343        {
344                int blue = 255;
345                if(color > 16777215)
346                        blue = color & 0x000000FF;
347                else
348                        blue = color & 0x0000FF;
349                return blue;
350        }
351        
352        
353
354}