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