KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.themes;
13
14 import java.util.Hashtable JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.core.runtime.IExecutableExtension;
18 import org.eclipse.swt.graphics.RGB;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.ui.themes.ColorUtil;
21 import org.eclipse.ui.themes.IColorFactory;
22
23 /**
24  * LightColorFactory returns tab begin and end colours based on taking
25  * a system color as input, analyzing it, and lightening it appropriately.
26  *
27  * @since 3.3
28  *
29  */

30 public class LightColorFactory implements IColorFactory,
31         IExecutableExtension {
32
33     protected static final RGB white = ColorUtil.getColorValue("COLOR_WHITE"); //$NON-NLS-1$
34
protected static final RGB black = ColorUtil.getColorValue("COLOR_BLACK"); //$NON-NLS-1$
35

36     String JavaDoc baseColorName;
37     String JavaDoc definitionId;
38
39     /**
40      * Return the highlight start (top of tab) color as an RGB
41      * @return the highlight start RGB
42      */

43     
44     public static RGB createHighlightStartColor(RGB tabStartColor) {
45         return ColorUtil.blend(white, tabStartColor);
46     }
47
48     /**
49      * This executable extension requires parameters to be explicitly declared
50      * via the second method described in the <code>IExecutableExtension</code>
51      * documentation. The following parameters are parsed:
52      * <code>base</code>, describes the base color to produce all other colours from.
53      * This value may either be an RGB triple or an SWT constant.
54      * <code>definitionId</code>, describes the id of color we are looking for, one of
55      * "org.eclipse.ui.workbench.ACTIVE_TAB_BG_START"
56      * "org.eclipse.ui.workbench.ACTIVE_TAB_BG_END"
57      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
58      * java.lang.String, java.lang.Object)
59      */

60     public void setInitializationData(IConfigurationElement config,
61             String JavaDoc propertyName, Object JavaDoc data) {
62
63         if (data instanceof Hashtable JavaDoc) {
64             Hashtable JavaDoc table = (Hashtable JavaDoc) data;
65             baseColorName = (String JavaDoc) table.get("base"); //$NON-NLS-1$
66
definitionId = (String JavaDoc) table.get("definitionId"); //$NON-NLS-1$
67
}
68     }
69
70     /*
71      * Return the number of RGB values in test that are
72      * equal to or between lower and upper.
73      */

74     protected int valuesInRange(RGB test, int lower, int upper) {
75         int hits = 0;
76         if(test.red >= lower && test.red <= upper) hits++;
77         if(test.blue >= lower && test.blue <= upper) hits++;
78         if(test.green >= lower && test.green <= upper) hits++;
79
80         return hits;
81     }
82     
83     /*
84      * Return the RGB value for the bottom tab color
85      * based on a blend of white and sample color
86      */

87     private RGB getLightenedColor(RGB sample) {
88         //Group 1
89
if(valuesInRange(sample, 180, 255) >= 2)
90             return sample;
91         
92         //Group 2
93
if(valuesInRange(sample, 100, 179) >= 2)
94             return ColorUtil.blend(white, sample, 40);
95         
96         //Group 3
97
if(valuesInRange(sample, 0, 99) >= 2)
98             return ColorUtil.blend(white, sample, 60);
99         
100         //Group 4
101
return ColorUtil.blend(white, sample, 30);
102     }
103
104     /*
105      * Return the Start (top of tab) color as an RGB
106      */

107     private RGB getActiveFocusStartColor() {
108         if (Display.getCurrent().getDepth() < 15)
109                 return getActiveFocusEndColor();
110
111         RGB startColor = ColorUtil.blend(white, getActiveFocusEndColor(), 75);
112         return startColor;
113     }
114
115     /*
116      * Return the End (top of tab) color as an RGB
117      */

118     private RGB getActiveFocusEndColor() {
119         if (Display.getCurrent().getDepth() < 15)
120             return ColorUtil.getColorValue(baseColorName);
121     
122         return getLightenedColor(
123                 ColorUtil.getColorValue(baseColorName));
124     }
125
126     /*
127      * Return the active focus tab text color as an RGB
128      */

129     private RGB getActiveFocusTextColor() {
130         if (Display.getCurrent().getDepth() < 15)
131             return ColorUtil.getColorValue(baseColorName); //typically TITLE_FOREGROUND
132

133         return ColorUtil.getColorValue("COLOR_BLACK"); //$NON-NLS-1$
134
}
135     /*
136      * Return the RGB value for the top tab color.
137      */

138     private RGB getActiveNofocusStartColor() {
139         RGB base = ColorUtil.getColorValue(baseColorName);
140         if (Display.getCurrent().getDepth() < 15)
141             return base;
142         
143         return ColorUtil.blend(white, base, 40);
144     }
145     
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.eclipse.ui.themes.IColorFactory#createColor()
150      */

151     public RGB createColor() {
152         //should have base, otherwise error in the xml
153
if (baseColorName == null || definitionId == null)
154             return white;
155
156         if (definitionId.equals("org.eclipse.ui.workbench.ACTIVE_TAB_BG_START")) //$NON-NLS-1$
157
return getActiveFocusStartColor();
158         if (definitionId.equals("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END")) //$NON-NLS-1$
159
return getActiveFocusEndColor();
160         if (definitionId.equals("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR")) //$NON-NLS-1$
161
return getActiveFocusTextColor();
162         if (definitionId.equals("org.eclipse.ui.workbench.ACTIVE_NOFOCUS_TAB_BG_START")) //$NON-NLS-1$
163
return getActiveNofocusStartColor();
164         
165         //should be one of start or end, otherwise error in the xml
166
return white;
167     }
168 }
169
Popular Tags