KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.util.Hashtable JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
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.ui.themes.ColorUtil;
20 import org.eclipse.ui.themes.IColorFactory;
21
22 /**
23  * A <code>IColorFactory</code> that may be used to select a colour with
24  * a higher contrast. The colors to blend are specified as per method
25  * number two in {@link org.eclipse.core.runtime.IExecutableExtension}.
26  * <p>
27  * Example usage:
28  * <br/>
29  * <code>
30  * &lt;colorDefinition
31  * label="Red/Blue Contrast"
32  * id="example.redblueblend"&gt;
33  * &lt;colorFactory
34  * plugin="org.eclipse.ui"
35  * class="org.eclipse.ui.internal.themes.RGBContrastFactory"&gt;
36  * &lt;parameter name="foreground" value="0,0,0" /&gt;
37  * &lt;parameter name="background1" value="COLOR_RED" /&gt;
38  * &lt;parameter name="background2" value="COLOR_BLUE" /&gt;
39  * &lt;/colorFactory&gt;
40  * &lt;/colorDefinition&gt;
41  * </code>
42  * </p>
43  *
44  * <p>
45  * This will select whichever of Red or Blue has a higher contrst with black.
46  * The color values may be specified as RGB triples or as SWT constants.
47  * </p>
48  *
49  * @see org.eclipse.swt.SWT
50  * @since 3.0
51  */

52 public class RGBContrastFactory implements IColorFactory, IExecutableExtension {
53     private String JavaDoc fg, bg1, bg2;
54
55     /**
56      * Returns the intensity of an RGB component using the
57      * sRGB gamma function.
58      *
59      * @param val Value to convert.
60      * @return Light intensity of the component.
61      */

62     double voltage_to_intensity_srgb(double val) {
63         /* Handle invalid values before doing a gamma transform. */
64         if (val < 0.0) {
65             return 0.0;
66         }
67         if (val > 1.0) {
68             return 1.0;
69         }
70
71         if (val <= 0.04045) {
72             return val / 12.92;
73         }
74         return Math.pow((val + 0.055) / 1.055, 2.4);
75     }
76
77     /**
78      * Returns a measure of the lightness in the perceptual colourspace
79      * IPT.
80      *
81      * @param color The colour in sRGB
82      * @return Lightness in IPT space.
83      */

84     double lightness(RGB color) {
85         double r = voltage_to_intensity_srgb(color.red / 255.0);
86         double g = voltage_to_intensity_srgb(color.green / 255.0);
87         double b = voltage_to_intensity_srgb(color.blue / 255.0);
88         double l = (0.3139 * r) + (0.6395 * g) + (0.0466 * b);
89         double m = (0.1516 * r) + (0.7482 * g) + (0.1000 * b);
90         double s = (0.0177 * r) + (0.1095 * g) + (0.8729 * b);
91         double lp, mp, sp;
92
93         if (l < 0.0) {
94             lp = -Math.pow(-l, 0.43);
95         } else {
96             lp = Math.pow(l, 0.43);
97         }
98         if (m < 0.0) {
99             mp = -Math.pow(-m, 0.43);
100         } else {
101             mp = Math.pow(m, 0.43);
102         }
103         if (s < 0.0) {
104             sp = -Math.pow(-s, 0.43);
105         } else {
106             sp = Math.pow(s, 0.43);
107         }
108
109         return (0.4000 * lp) + (0.4000 * mp) + (0.2000 * sp);
110     }
111
112     public RGB createColor() {
113         /**
114          * Determine which pair has a higher contrast by selecting
115          * the colour with the furthest distance in lightness.
116          */

117         RGB cfg, cbg1, cbg2;
118
119         if (fg != null) {
120             cfg = ColorUtil.getColorValue(fg);
121         } else {
122             cfg = new RGB(255, 255, 255);
123         }
124         if (bg1 != null) {
125             cbg1 = ColorUtil.getColorValue(bg1);
126         } else {
127             cbg1 = new RGB(0, 0, 0);
128         }
129         if (bg2 != null) {
130             cbg2 = ColorUtil.getColorValue(bg2);
131         } else {
132             cbg2 = new RGB(0, 0, 0);
133         }
134
135         double lfg = lightness(cfg);
136         double lbg1 = lightness(cbg1);
137         double lbg2 = lightness(cbg2);
138
139         if (Math.abs(lbg1 - lfg) > Math.abs(lbg2 - lfg)) {
140             return cbg1;
141         } else {
142             return cbg2;
143         }
144     }
145
146     /**
147      * This executable extension requires parameters to be explicitly declared
148      * via the second method described in the <code>IExecutableExtension</code>
149      * documentation. This class expects that there will be three parameters,
150      * <code>foreground</code>, <code>background1</code> and
151      * <code>background2</code>, that describe the two colors to be blended.
152      * These values may either be RGB triples or SWT constants.
153      *
154      * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement, java.lang.String, java.lang.Object)
155      */

156     public void setInitializationData(IConfigurationElement config,
157             String JavaDoc propertyName, Object JavaDoc data) throws CoreException {
158         if (data instanceof Hashtable JavaDoc) {
159             Hashtable JavaDoc table = (Hashtable JavaDoc) data;
160             fg = (String JavaDoc) table.get("foreground"); //$NON-NLS-1$
161
bg1 = (String JavaDoc) table.get("background1"); //$NON-NLS-1$
162
bg2 = (String JavaDoc) table.get("background2"); //$NON-NLS-1$
163
}
164
165     }
166 }
167
Popular Tags