KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > themes > ColorUtils


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.internal.themes;
12
13 import java.lang.reflect.Field JavaDoc;
14 import java.lang.reflect.Modifier JavaDoc;
15
16 import org.eclipse.jface.resource.StringConverter;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Display;
20
21 /**
22  * Useful color utilities.
23  *
24  * @since 3.0
25  */

26 public final class ColorUtils {
27
28     /**
29      * @param value the SWT constant <code>String</code>.
30      * @return the value of the SWT constant, or <code>SWT.COLOR_BLACK</code> if it could
31      * not be determined.
32      */

33     private static RGB process(String JavaDoc value) {
34         try {
35             Class JavaDoc clazz = SWT.class; //$NON-NLS-1$
36
Field JavaDoc[] fields = clazz.getDeclaredFields();
37             for (int i = 0; i < fields.length; i++) {
38                 Field JavaDoc field = fields[i];
39                 if (field.getType() == Integer.TYPE
40                         && Modifier.isStatic(field.getModifiers())
41                         && Modifier.isPublic(field.getModifiers())
42                         && Modifier.isFinal(field.getModifiers())) {
43                     if (value.equals(field.getName())) {
44                         return getSystemColor(field.getInt(null));
45                     }
46                 }
47             }
48         } catch (IllegalArgumentException JavaDoc e) {
49             // no op - shouldnt happen. We check for static before calling getInt(null)
50
} catch (IllegalAccessException JavaDoc e) {
51             // no op - shouldnt happen. We check for public before calling getInt(null)
52
}
53         return getSystemColor(SWT.COLOR_BLACK);
54     }
55
56     /**
57      * Blend the two color values returning a value that is halfway between them.
58      *
59      * @param val1 the first value
60      * @param val2 the second value
61      * @return the blended color
62      */

63     public static RGB blend(RGB val1, RGB val2) {
64         int red = blend(val1.red, val2.red);
65         int green = blend(val1.green, val2.green);
66         int blue = blend(val1.blue, val2.blue);
67         return new RGB(red, green, blue);
68     }
69
70     /**
71      * Blend the two color values returning a value that is halfway between them.
72      *
73      * @param temp1 the first value
74      * @param temp2 the second value
75      * @return the blended int value
76      */

77     private static int blend(int temp1, int temp2) {
78         return (Math.abs(temp1 - temp2) / 2) + Math.min(temp1, temp2);
79     }
80
81     /**
82      * @param colorId the system color identifier.
83      * @return the RGB value of the supplied system color.
84      */

85     private static RGB getSystemColor(int colorId) {
86         return Display.getCurrent().getSystemColor(colorId).getRGB();
87     }
88
89     /**
90      * Get the RGB value for a given color.
91      *
92      * @param rawValue the raw value, either an RGB triple or an SWT constant.
93      * @return Returns the RGB value.
94      */

95     public static RGB getColorValue(String JavaDoc rawValue) {
96         if (rawValue == null)
97             return null;
98
99         rawValue = rawValue.trim();
100
101         if (!isDirectValue(rawValue)) {
102             return process(rawValue);
103         }
104
105         return StringConverter.asRGB(rawValue);
106     }
107
108     /**
109      * Get the RGB values for a given color array.
110      *
111      * @param rawValues the raw values, either RGB triple or an SWT constant.
112      * @return Returns the RGB values.
113      */

114     public static RGB[] getColorValues(String JavaDoc[] rawValues) {
115         RGB[] values = new RGB[rawValues.length];
116         for (int i = 0; i < rawValues.length; i++) {
117             values[i] = getColorValue(rawValues[i]);
118         }
119         return values;
120     }
121
122     /**
123      * @return whether the value returned by <code>getValue()</code> is already
124      * in RGB form.
125      */

126     private static boolean isDirectValue(String JavaDoc rawValue) {
127         return rawValue.indexOf(',') >= 0;
128     }
129
130     /**
131      * Not intended to be instantiated.
132      */

133     private ColorUtils() {
134         //no-op
135
}
136 }
137
Popular Tags