KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > graphics > ColorHelper


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.deliver.util.graphics;
25
26 import java.awt.Color JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import org.infoglue.deliver.util.MathHelper;
30
31 /**
32  * This class is an attempt to give template-coders access to the Color-class in some ways.
33  * Basically used to enable good control in the templates.
34  */

35
36 public class ColorHelper
37 {
38     public ColorHelper()
39     {
40     }
41     
42     /**
43      * Used to get a color-object
44      */

45     
46     public Color JavaDoc getColor(int r, int g, int b)
47     {
48         return new Color JavaDoc(r, g, b);
49     }
50
51
52     /**
53      * Used to create a new color-object by hex-values.
54      */

55     
56     public Color JavaDoc getHexColor(String JavaDoc hexadecimalValue)
57     {
58         return new Color JavaDoc(new MathHelper().hexToDecimal(hexadecimalValue));
59     }
60
61     /**
62      * Creates a Color object from a comma separated string like "12:34:6:154"
63      * values 0-255 and in order R,G,B,A
64      * @param colonSeparatedRGBA a string
65      * @return a Color object
66      */

67     public static Color JavaDoc getColor( String JavaDoc colonSeparatedRGBA )
68     {
69         return ColorHelper.getColor( colonSeparatedRGBA, ":" );
70     }
71     
72     /**
73      * Creates a Color object from a separated string like "12-34-6-154" values
74      * 0-255 and in order R,G,B,A
75      * @param separatedRGBA a string
76      * @param delimiter a delimiter
77      * @return a Color object
78      * @author Per Jonsson - per.jonsson@it-huset.se
79      */

80     
81     public static Color JavaDoc getColor( String JavaDoc separatedRGBA, String JavaDoc delimiter )
82     {
83         //System.out.println("separatedRGBA: " + separatedRGBA + ", " + delimiter );
84
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( separatedRGBA, delimiter, false );
85         int r = 0, g = 0, b = 0, a = 0;
86         if ( st.hasMoreTokens() )
87         {
88             r = Integer.parseInt( st.nextToken().trim() );
89         }
90         if ( st.hasMoreTokens() )
91         {
92             g = Integer.parseInt( st.nextToken().trim() );
93         }
94         if ( st.hasMoreTokens() )
95         {
96             b = Integer.parseInt( st.nextToken().trim() );
97         }
98         if ( st.hasMoreTokens() )
99         {
100             a = Integer.parseInt( st.nextToken().trim() );
101         }
102         //System.out.println( r + ", " + g + ", " + b + ", " + a );
103
return new Color JavaDoc( r, g, b, a );
104     }
105
106 }
107
Popular Tags