001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.themes;
011:
012: import java.util.Hashtable;
013:
014: import org.eclipse.core.runtime.IConfigurationElement;
015: import org.eclipse.core.runtime.IExecutableExtension;
016: import org.eclipse.swt.graphics.RGB;
017: import org.eclipse.swt.widgets.Display;
018: import org.eclipse.ui.themes.ColorUtil;
019: import org.eclipse.ui.themes.IColorFactory;
020:
021: /**
022: * LightColorFactory returns tab begin and end colours based on taking
023: * a system color as input, analyzing it, and lightening it appropriately.
024: *
025: * @since 3.3
026: *
027: */
028: public class LightColorFactory implements IColorFactory,
029: IExecutableExtension {
030:
031: protected static final RGB white = ColorUtil
032: .getColorValue("COLOR_WHITE"); //$NON-NLS-1$
033: protected static final RGB black = ColorUtil
034: .getColorValue("COLOR_BLACK"); //$NON-NLS-1$
035:
036: String baseColorName;
037: String definitionId;
038:
039: /**
040: * Return the highlight start (top of tab) color as an RGB
041: * @return the highlight start RGB
042: */
043:
044: public static RGB createHighlightStartColor(RGB tabStartColor) {
045: return ColorUtil.blend(white, tabStartColor);
046: }
047:
048: /**
049: * This executable extension requires parameters to be explicitly declared
050: * via the second method described in the <code>IExecutableExtension</code>
051: * documentation. The following parameters are parsed:
052: * <code>base</code>, describes the base color to produce all other colours from.
053: * This value may either be an RGB triple or an SWT constant.
054: * <code>definitionId</code>, describes the id of color we are looking for, one of
055: * "org.eclipse.ui.workbench.ACTIVE_TAB_BG_START"
056: * "org.eclipse.ui.workbench.ACTIVE_TAB_BG_END"
057: * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
058: * java.lang.String, java.lang.Object)
059: */
060: public void setInitializationData(IConfigurationElement config,
061: String propertyName, Object data) {
062:
063: if (data instanceof Hashtable) {
064: Hashtable table = (Hashtable) data;
065: baseColorName = (String) table.get("base"); //$NON-NLS-1$
066: definitionId = (String) table.get("definitionId"); //$NON-NLS-1$
067: }
068: }
069:
070: /*
071: * Return the number of RGB values in test that are
072: * equal to or between lower and upper.
073: */
074: protected int valuesInRange(RGB test, int lower, int upper) {
075: int hits = 0;
076: if (test.red >= lower && test.red <= upper)
077: hits++;
078: if (test.blue >= lower && test.blue <= upper)
079: hits++;
080: if (test.green >= lower && test.green <= upper)
081: hits++;
082:
083: return hits;
084: }
085:
086: /*
087: * Return the RGB value for the bottom tab color
088: * based on a blend of white and sample color
089: */
090: private RGB getLightenedColor(RGB sample) {
091: //Group 1
092: if (valuesInRange(sample, 180, 255) >= 2)
093: return sample;
094:
095: //Group 2
096: if (valuesInRange(sample, 100, 179) >= 2)
097: return ColorUtil.blend(white, sample, 40);
098:
099: //Group 3
100: if (valuesInRange(sample, 0, 99) >= 2)
101: return ColorUtil.blend(white, sample, 60);
102:
103: //Group 4
104: return ColorUtil.blend(white, sample, 30);
105: }
106:
107: /*
108: * Return the Start (top of tab) color as an RGB
109: */
110: private RGB getActiveFocusStartColor() {
111: if (Display.getCurrent().getDepth() < 15)
112: return getActiveFocusEndColor();
113:
114: RGB startColor = ColorUtil.blend(white,
115: getActiveFocusEndColor(), 75);
116: return startColor;
117: }
118:
119: /*
120: * Return the End (top of tab) color as an RGB
121: */
122: private RGB getActiveFocusEndColor() {
123: if (Display.getCurrent().getDepth() < 15)
124: return ColorUtil.getColorValue(baseColorName);
125:
126: return getLightenedColor(ColorUtil.getColorValue(baseColorName));
127: }
128:
129: /*
130: * Return the active focus tab text color as an RGB
131: */
132: private RGB getActiveFocusTextColor() {
133: if (Display.getCurrent().getDepth() < 15)
134: return ColorUtil.getColorValue(baseColorName); //typically TITLE_FOREGROUND
135:
136: return ColorUtil.getColorValue("COLOR_BLACK"); //$NON-NLS-1$
137: }
138:
139: /*
140: * Return the RGB value for the top tab color.
141: */
142: private RGB getActiveNofocusStartColor() {
143: RGB base = ColorUtil.getColorValue(baseColorName);
144: if (Display.getCurrent().getDepth() < 15)
145: return base;
146:
147: return ColorUtil.blend(white, base, 40);
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see org.eclipse.ui.themes.IColorFactory#createColor()
154: */
155: public RGB createColor() {
156: //should have base, otherwise error in the xml
157: if (baseColorName == null || definitionId == null)
158: return white;
159:
160: if (definitionId
161: .equals("org.eclipse.ui.workbench.ACTIVE_TAB_BG_START")) //$NON-NLS-1$
162: return getActiveFocusStartColor();
163: if (definitionId
164: .equals("org.eclipse.ui.workbench.ACTIVE_TAB_BG_END")) //$NON-NLS-1$
165: return getActiveFocusEndColor();
166: if (definitionId
167: .equals("org.eclipse.ui.workbench.ACTIVE_TAB_TEXT_COLOR")) //$NON-NLS-1$
168: return getActiveFocusTextColor();
169: if (definitionId
170: .equals("org.eclipse.ui.workbench.ACTIVE_NOFOCUS_TAB_BG_START")) //$NON-NLS-1$
171: return getActiveNofocusStartColor();
172:
173: //should be one of start or end, otherwise error in the xml
174: return white;
175: }
176: }
|