KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > themes > ColorUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.themes;
12
13 import java.lang.reflect.Field JavaDoc;
14 import java.lang.reflect.Modifier JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 import org.eclipse.jface.resource.StringConverter;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.graphics.RGB;
20 import org.eclipse.swt.widgets.Display;
21
22 /**
23  * Useful color utilities.
24  *
25  * @since 3.0 - initial release
26  * @since 3.2 - public API
27  */

28 public final class ColorUtil {
29
30     private static Field JavaDoc[] cachedFields;
31     
32     /**
33      * Process the given string and return a corresponding RGB object.
34      *
35      * @param value
36      * the SWT constant <code>String</code>
37      * @return the value of the SWT constant, or <code>SWT.COLOR_BLACK</code>
38      * if it could not be determined
39      */

40     private static RGB process(String JavaDoc value) {
41         Field JavaDoc [] fields = getFields();
42         try {
43             for (int i = 0; i < fields.length; i++) {
44                 Field JavaDoc field = fields[i];
45                 if (field.getName().equals(value)) {
46                     return getSystemColor(field.getInt(null));
47                 }
48             }
49         } catch (IllegalArgumentException JavaDoc e) {
50             // no op - shouldnt happen. We check for static before calling
51
// getInt(null)
52
} catch (IllegalAccessException JavaDoc e) {
53             // no op - shouldnt happen. We check for public before calling
54
// getInt(null)
55
}
56         return getSystemColor(SWT.COLOR_BLACK);
57     }
58
59     /**
60      * Get the SWT constant fields.
61      *
62      * @return the fields
63      * @since 3.3
64      */

65     private static Field JavaDoc[] getFields() {
66         if (cachedFields == null) {
67             Class JavaDoc clazz = SWT.class;
68             Field JavaDoc[] allFields = clazz.getDeclaredFields();
69             ArrayList JavaDoc applicableFields = new ArrayList JavaDoc(allFields.length);
70             
71             for (int i = 0; i < allFields.length; i++) {
72                 Field JavaDoc field = allFields[i];
73                 if (field.getType() == Integer.TYPE
74                         && Modifier.isStatic(field.getModifiers())
75                         && Modifier.isPublic(field.getModifiers())
76                         && Modifier.isFinal(field.getModifiers())
77                         && field.getName().startsWith("COLOR")) { //$NON-NLS-1$
78

79                     applicableFields.add(field);
80                 }
81             }
82             cachedFields = (Field JavaDoc []) applicableFields.toArray(new Field JavaDoc [applicableFields.size()]);
83         }
84         return cachedFields;
85     }
86
87     /**
88      * Blends the two color values according to the provided ratio.
89      *
90      * @param c1
91      * first color
92      * @param c2
93      * second color
94      * @param ratio
95      * percentage of the first color in the blend (0-100)
96      * @return the RGB value of the blended color
97      *
98      * @since 3.3
99      */

100     public static RGB blend(RGB c1, RGB c2, int ratio) {
101         int r = blend(c1.red, c2.red, ratio);
102         int g = blend(c1.green, c2.green, ratio);
103         int b = blend(c1.blue, c2.blue, ratio);
104         return new RGB(r, g, b);
105     }
106
107     private static int blend(int v1, int v2, int ratio) {
108         int b = (ratio * v1 + (100 - ratio) * v2) / 100;
109         return Math.min(255, b);
110     }
111     
112     /**
113      * Blend the two color values returning a value that is halfway between
114      * them.
115      *
116      * @param val1
117      * the first value
118      * @param val2
119      * the second value
120      * @return the blended color
121      */

122     public static RGB blend(RGB val1, RGB val2) {
123         int red = blend(val1.red, val2.red);
124         int green = blend(val1.green, val2.green);
125         int blue = blend(val1.blue, val2.blue);
126         return new RGB(red, green, blue);
127     }
128
129     /**
130      * Blend the two color values returning a value that is halfway between
131      * them.
132      *
133      * @param temp1
134      * the first value
135      * @param temp2
136      * the second value
137      * @return the blended int value
138      */

139     private static int blend(int temp1, int temp2) {
140         return (Math.abs(temp1 - temp2) / 2) + Math.min(temp1, temp2);
141     }
142
143     /**
144      * Return the system color that matches the provided SWT constant value.
145      *
146      * @param colorId
147      * the system color identifier
148      * @return the RGB value of the supplied system color
149      */

150     private static RGB getSystemColor(int colorId) {
151         return Display.getCurrent().getSystemColor(colorId).getRGB();
152     }
153
154     /**
155      * Get the RGB value for a given color.
156      *
157      * @param rawValue
158      * the raw value, either an RGB triple or an SWT constant name
159      * @return the RGB value
160      */

161     public static RGB getColorValue(String JavaDoc rawValue) {
162         if (rawValue == null) {
163             return null;
164         }
165
166         rawValue = rawValue.trim();
167
168         if (!isDirectValue(rawValue)) {
169             return process(rawValue);
170         }
171
172         return StringConverter.asRGB(rawValue);
173     }
174
175     /**
176      * Get the RGB values for a given color array.
177      *
178      * @param rawValues
179      * the raw values, either RGB triple or an SWT constant
180      * @return the RGB values
181      */

182     public static RGB[] getColorValues(String JavaDoc[] rawValues) {
183         RGB[] values = new RGB[rawValues.length];
184         for (int i = 0; i < rawValues.length; i++) {
185             values[i] = getColorValue(rawValues[i]);
186         }
187         return values;
188     }
189
190     /**
191      * Return whether the value returned by <code>getValue()</code> is already
192      * in RGB form.
193      *
194      * @return whether the value returned by <code>getValue()</code> is
195      * already in RGB form
196      */

197     private static boolean isDirectValue(String JavaDoc rawValue) {
198         return rawValue.indexOf(',') >= 0;
199     }
200
201     /**
202      * Not intended to be instantiated.
203      */

204     private ColorUtil() {
205         // no-op
206
}
207 }
208
Popular Tags