Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a848c565cb4fd506d226c0369b1c1f978cd02b43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*******************************************************************************
 * Copyright (c) 2004, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.themes;

import java.util.Hashtable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.swt.graphics.RGB;

/**
 * A reusable <code>IColorFactory</code> that may be used to blend two colors.
 * The colors to blend are specified as per method number two in
 * {@link org.eclipse.core.runtime.IExecutableExtension}.
 * <p>
 * Example usage:
 * </p>
 *
 * <pre>
 * <code>
 * &lt;colorDefinition label="Red/Blue Blend" id="example.redblueblend"&gt;
 *     &lt;colorFactory plugin="org.eclipse.ui"
 * 		class="org.eclipse.ui.themes.RGBBlendColorFactory"&gt;
 *      	&lt;parameter name="color1" value="255,0,0" /&gt;
 *  	&lt;parameter name="color2" value="COLOR_BLUE" /&gt;
 *     &lt;/colorFactory&gt;
 * &lt;/colorDefinition&gt;
 * </code>
 * </pre>
 *
 * <p>
 * The color values may be specified as RGB triples or as SWT constants.
 * </p>
 *
 * @see org.eclipse.swt.SWT
 * @since 3.0
 */
public class RGBBlendColorFactory implements IColorFactory, IExecutableExtension {

	private String color1, color2;

	@Override
	public RGB createColor() {
		if (color1 == null && color2 == null) {
			return new RGB(0, 0, 0);
		} else if (color1 != null && color2 == null) {
			return ColorUtil.getColorValue(color1);
		} else if (color1 == null && color2 != null) {
			return ColorUtil.getColorValue(color2);
		} else {
			RGB rgb1 = ColorUtil.getColorValue(color1);
			RGB rgb2 = ColorUtil.getColorValue(color2);
			return ColorUtil.blend(rgb1, rgb2);
		}
	}

	/**
	 * This executable extension requires parameters to be explicitly declared via
	 * the second method described in the <code>IExecutableExtension</code>
	 * documentation. This class expects that there will be two parameters,
	 * <code>color1</code> and <code>color2</code>, that describe the two colors to
	 * be blended. These values may either be RGB triples or SWT constants.
	 *
	 * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
	 *      java.lang.String, java.lang.Object)
	 */
	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
			throws CoreException {

		if (data instanceof Hashtable) {
			Hashtable table = (Hashtable) data;
			color1 = (String) table.get("color1"); //$NON-NLS-1$
			color2 = (String) table.get("color2"); //$NON-NLS-1$
		}
	}
}

Back to the top