Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f8c08f220fe3df1d2ab54a8603621fe60f41d049 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * 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.internal.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;
import org.eclipse.ui.themes.ColorUtil;
import org.eclipse.ui.themes.IColorFactory;

/**
 * A <code>IColorFactory</code> that may be used to select a color with a higher
 * contrast. The input colors are specified as per method number two in
 * {@link org.eclipse.core.runtime.IExecutableExtension}.
 * <p>
 * Example usage:
 *
 * <pre>
 * &lt;colorDefinition
 *     label="Declaration view background"
 *     id="org.eclipse.jdt.ui.DeclarationView.backgroundColor"&gt;
 *     &lt;colorFactory
 *             plugin="org.eclipse.ui"
 *             class="org.eclipse.ui.internal.themes.RGBContrastFactory"&gt;
 *         &lt;parameter name="foreground" value="0,0,0" /&gt;
 *         &lt;parameter name="background" value="COLOR_INFO_BACKGROUND" /&gt;
 *         &lt;parameter name="alternativeBackground" value="COLOR_LIST_BACKGROUND" /&gt;
 *     &lt;/colorFactory&gt;
 * &lt;/colorDefinition&gt;
 * </pre>
 *
 * <p>
 * Returns <em>background</em> if <em>foreground</em> is visibly distinct from
 * <em>background</em>. Otherwise, returns <em>alternativeBackground</em> if
 * that color has more difference in brightness to the foreground. If both
 * colors are bad, returns <em>background</em>. The color values may be
 * specified as RGB triples or as SWT constants.
 *
 * @see org.eclipse.swt.SWT
 * @since 3.107.100
 */
// This class is used by org.eclipse.jdt.ui/plugin.xml to fix
// https://bugs.eclipse.org/477487
public class RGBVisibleContrastColorFactory implements IColorFactory, IExecutableExtension {
	private String fg, bg, altBg;

	@Override
	public RGB createColor() {
		RGB cfg, cbg, cbgAlt;

		if (fg != null) {
			cfg = ColorUtil.getColorValue(fg);
		} else {
			cfg = new RGB(0, 0, 0);
		}
		if (bg != null) {
			cbg = ColorUtil.getColorValue(bg);
		} else {
			cbg = new RGB(255, 255, 255);
		}
		if (altBg != null) {
			cbgAlt = ColorUtil.getColorValue(altBg);
		} else {
			cbgAlt = new RGB(255, 255, 255);
		}

		float bfg = cfg.getHSB()[2];
		float bbg = cbg.getHSB()[2];
		float bbgAlt = cbgAlt.getHSB()[2];

		if (Math.abs(bbg - bfg) < 0.5f && Math.abs(bbgAlt - bfg) > Math.abs(bbg - bfg)) {
			return cbgAlt;
		}
		return cbg;
	}

	/**
	 * 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 three parameters,
	 * <code>foreground</code>, <code>background</code> and
	 * <code>alternativeBackground</code>. These values may either be RGB triples or
	 * SWT constants.
	 */
	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
			throws CoreException {
		if (data instanceof Hashtable) {
			@SuppressWarnings("unchecked")
			Hashtable<String, String> table = (Hashtable<String, String>) data;
			fg = table.get("foreground"); //$NON-NLS-1$
			bg = table.get("background"); //$NON-NLS-1$
			altBg = table.get("alternativeBackground"); //$NON-NLS-1$
		}
	}
}

Back to the top