diff options
author | Stefan Winkler | 2015-03-08 17:37:09 +0000 |
---|---|---|
committer | Stefan Winkler | 2015-03-08 17:37:09 +0000 |
commit | 552ca4ba158f2ce8d3f0e1487cdf8f0b1669c993 (patch) | |
tree | afd54e8b0f8085bdecc65979a02732ab7ef1ef5c | |
parent | 734e359e9ca835525a4b653fd2e13d7126e822e9 (diff) | |
download | eclipse.platform.ui-552ca4ba158f2ce8d3f0e1487cdf8f0b1669c993.tar.gz eclipse.platform.ui-552ca4ba158f2ce8d3f0e1487cdf8f0b1669c993.tar.xz eclipse.platform.ui-552ca4ba158f2ce8d3f0e1487cdf8f0b1669c993.zip |
Bug 459961 - [CSS] Add CSS support for 'transparent'
Change-Id: I27539338de93097f25c1243162a18061f5854ece
Signed-off-by: Stefan Winkler <stefan@winklerweb.net>
5 files changed, 138 insertions, 51 deletions
diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java index 4adfdc076cd..c71e084c938 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/helpers/CSSSWTColorHelper.java @@ -10,6 +10,7 @@ * IBM Corporation * Kai Toedter - added radial gradient support * Robin Stocker - Bug 420035 - [CSS] Support SWT color constants in gradients + * Stefan Winkler <stefan@winklerweb.net> - Bug 459961 *******************************************************************************/ package org.eclipse.e4.ui.css.swt.helpers; @@ -29,6 +30,7 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CTabFolder; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.graphics.RGBA; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.w3c.dom.css.CSSPrimitiveValue; @@ -46,7 +48,7 @@ public class CSSSWTColorHelper { /*--------------- SWT Color Helper -----------------*/ public static Color getSWTColor(RGBColor rgbColor, Display display) { - RGB rgb = getRGB(rgbColor); + RGBA rgb = getRGBA(rgbColor); return new Color(display, rgb); } @@ -55,25 +57,25 @@ public class CSSSWTColorHelper { return null; } Color color = display.getSystemColor(SWT.COLOR_BLACK); - RGB rgb = getRGB((CSSPrimitiveValue) value, display); - if (rgb != null) { - color = new Color(display, rgb.red, rgb.green, rgb.blue); + RGBA rgba = getRGBA((CSSPrimitiveValue) value, display); + if (rgba != null) { + color = new Color(display, rgba.rgb.red, rgba.rgb.green, rgba.rgb.blue, rgba.alpha); } return color; } - private static RGB getRGB(CSSPrimitiveValue value, Display display) { - RGB rgb = getRGB(value); - if (rgb == null && display != null) { + private static RGBA getRGBA(CSSPrimitiveValue value, Display display) { + RGBA rgba = getRGBA(value); + if (rgba == null && display != null) { String name = value.getStringValue(); if (hasColorDefinitionAsValue(name)) { - rgb = findColorByDefinition(name); + rgba = findColorByDefinition(name); } else if (name.contains("-")) { name = name.replace('-', '_'); - rgb = process(display, name); + rgba = process(display, name); } } - return rgb; + return rgba; } public static boolean hasColorDefinitionAsValue(CSSValue value) { @@ -95,19 +97,19 @@ public class CSSSWTColorHelper { } /** - * Process the given string and return a corresponding RGB object. + * Process the given string and return a corresponding RGBA object. * * @param value * the SWT constant <code>String</code> - * @return the value of the SWT constant, or <code>SWT.COLOR_BLACK</code> - * if it could not be determined + * @return the value of the SWT constant, or <code>SWT.COLOR_BLACK</code> if + * it could not be determined */ - private static RGB process(Display display, String value) { + private static RGBA process(Display display, String value) { Field [] fields = getFields(); try { for (Field field : fields) { if (field.getName().equals(value)) { - return display.getSystemColor(field.getInt(null)).getRGB(); + return display.getSystemColor(field.getInt(null)).getRGBA(); } } } catch (IllegalArgumentException e) { @@ -117,7 +119,7 @@ public class CSSSWTColorHelper { // no op - shouldnt happen. We check for public before calling // getInt(null) } - return display.getSystemColor(SWT.COLOR_BLACK).getRGB(); + return display.getSystemColor(SWT.COLOR_BLACK).getRGBA(); } /** @@ -148,42 +150,45 @@ public class CSSSWTColorHelper { return cachedFields; } - public static RGB getRGB(String name) { + public static RGBA getRGBA(String name) { RGBColor color = CSS2ColorHelper.getRGBColor(name); if (color != null) { - return getRGB(color); + return getRGBA(color); } return null; } - public static RGB getRGB(RGBColor color) { - return new RGB((int) color.getRed().getFloatValue( + public static RGBA getRGBA(RGBColor color) { + return new RGBA((int) color.getRed().getFloatValue( CSSPrimitiveValue.CSS_NUMBER), (int) color.getGreen() .getFloatValue(CSSPrimitiveValue.CSS_NUMBER), (int) color - .getBlue().getFloatValue(CSSPrimitiveValue.CSS_NUMBER)); + .getBlue().getFloatValue(CSSPrimitiveValue.CSS_NUMBER), + // for now, we only support solid RGB colors in CSS - our CSS model + // as of now does not have an element for RGBAColor. + 255); } - public static RGB getRGB(CSSValue value) { + public static RGBA getRGBA(CSSValue value) { if (value.getCssValueType() != CSSValue.CSS_PRIMITIVE_VALUE) { return null; } - return getRGB((CSSPrimitiveValue) value); + return getRGBA((CSSPrimitiveValue) value); } - public static RGB getRGB(CSSPrimitiveValue value) { - RGB rgb = null; + public static RGBA getRGBA(CSSPrimitiveValue value) { + RGBA rgba = null; switch (value.getPrimitiveType()) { case CSSPrimitiveValue.CSS_IDENT: case CSSPrimitiveValue.CSS_STRING: String string = value.getStringValue(); - rgb = getRGB(string); + rgba = getRGBA(string); break; case CSSPrimitiveValue.CSS_RGBCOLOR: RGBColor rgbColor = value.getRGBColorValue(); - rgb = getRGB(rgbColor); + rgba = getRGBA(rgbColor); break; } - return rgb; + return rgba; } public static Integer getPercent(CSSPrimitiveValue value) { @@ -220,9 +225,12 @@ public class CSSSWTColorHelper { case CSSPrimitiveValue.CSS_IDENT: case CSSPrimitiveValue.CSS_STRING: case CSSPrimitiveValue.CSS_RGBCOLOR: - RGB rgb = getRGB((CSSPrimitiveValue) value, display); - if (rgb != null) { - gradient.addRGB(rgb, (CSSPrimitiveValue) value); + RGBA rgba = getRGBA((CSSPrimitiveValue) value, display); + if (rgba != null) { + // note that in this call we lose the RGBA alpha + // component - we do currently not support alpha + // gradients + gradient.addRGB(rgba, (CSSPrimitiveValue) value); } else { //check for vertical gradient gradient.setVertical(!value.getCssText().equals("false")); @@ -313,10 +321,11 @@ public class CSSSWTColorHelper { return new CSS2RGBColorImpl(red, green, blue); } - private static RGB findColorByDefinition(String name) { + private static RGBA findColorByDefinition(String name) { IColorAndFontProvider provider = CSSActivator.getDefault().getColorAndFontProvider(); if (provider != null) { - return provider.getColor(normalizeId(name.substring(1))); + RGB rgb = provider.getColor(normalizeId(name.substring(1))); + return new RGBA(rgb.red, rgb.green, rgb.blue, 255); } return null; } diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTRGBConverterImpl.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTRGBConverterImpl.java index 10e826262e8..30575426236 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTRGBConverterImpl.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/converters/CSSValueSWTRGBConverterImpl.java @@ -7,6 +7,7 @@ * * Contributors: * Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation + * Stefan Winkler <stefan@winklerweb.net> - Bug 459961 *******************************************************************************/ package org.eclipse.e4.ui.css.swt.properties.converters; @@ -26,10 +27,10 @@ import org.w3c.dom.css.RGBColor; * <li>CSS Value to {@link RGB}</li>. * <li>{@link RGB} to String CSS Value</li> * </ul> - * + * * @version 1.0.0 * @author <a href="mailto:angelo.zerr@gmail.com">Angelo ZERR</a> - * + * */ public class CSSValueSWTRGBConverterImpl extends AbstractCSSValueConverter { @@ -42,7 +43,7 @@ public class CSSValueSWTRGBConverterImpl extends AbstractCSSValueConverter { @Override public Object convert(CSSValue value, CSSEngine engine, Object context) throws Exception { - return CSSSWTColorHelper.getRGB(value); + return CSSSWTColorHelper.getRGBA(value).rgb; } @Override diff --git a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyColorDefinitionHandler.java b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyColorDefinitionHandler.java index 3489765ac28..de27192206b 100644 --- a/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyColorDefinitionHandler.java +++ b/bundles/org.eclipse.e4.ui.css.swt/src/org/eclipse/e4/ui/css/swt/properties/definition/CSSPropertyColorDefinitionHandler.java @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Stefan Winkler <stefan@winklerweb.net> - Bug 459961 *******************************************************************************/ package org.eclipse.e4.ui.css.swt.properties.definition; @@ -19,15 +20,15 @@ import org.w3c.dom.css.CSSValue; public class CSSPropertyColorDefinitionHandler implements ICSSPropertyHandler { private final static String COLOR_PROP = "color"; - + @Override public boolean applyCSSProperty(Object element, String property, CSSValue value, String pseudo, CSSEngine engine) throws Exception { if (element instanceof ColorDefinitionElement && COLOR_PROP.equals(property)) { - IColorDefinitionOverridable definition = - (IColorDefinitionOverridable) ((ColorDefinitionElement) element).getNativeWidget(); - definition.setValue(CSSSWTColorHelper.getRGB(value)); - } + IColorDefinitionOverridable definition = + (IColorDefinitionOverridable) ((ColorDefinitionElement) element).getNativeWidget(); + definition.setValue(CSSSWTColorHelper.getRGBA(value).rgb); + } return false; } @@ -35,5 +36,5 @@ public class CSSPropertyColorDefinitionHandler implements ICSSPropertyHandler { public String retrieveCSSProperty(Object element, String property, String pseudo, CSSEngine engine) throws Exception { return null; - } + } } diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/Bug459961Test.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/Bug459961Test.java new file mode 100644 index 00000000000..9f170963335 --- /dev/null +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/Bug459961Test.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2015 Stefan Winkler and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Stefan Winkler <stefan@winklerweb.net> - initial contribution + *******************************************************************************/ +package org.eclipse.e4.ui.tests.css.swt; + +import static org.junit.Assert.assertEquals; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.RGBA; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.junit.Test; + +public class Bug459961Test extends CSSSWTTestCase { + + @Test + public void testRegularColorConstantReference() { + String cssString = "Label { background-color: COLOR-GREEN; }"; + + Label label = createTestLabel(cssString); + + RGBA expected = Display.getDefault().getSystemColor(SWT.COLOR_GREEN).getRGBA(); + RGBA actual = label.getBackground().getRGBA(); + assertRGBAEquals(expected, actual); + } + + @Test + public void testTransparentColorConstantReference() { + String cssString = "Label { background-color: COLOR-TRANSPARENT; }"; + + Label label = createTestLabel(cssString); + + RGBA expected = Display.getDefault().getSystemColor(SWT.COLOR_TRANSPARENT).getRGBA(); + RGBA actual = label.getBackground().getRGBA(); + assertRGBAEquals(expected, actual); + } + + private void assertRGBAEquals(RGBA expected, RGBA actual) { + assertEquals(expected.rgb.red, actual.rgb.red); + assertEquals(expected.rgb.blue, actual.rgb.blue); + assertEquals(expected.rgb.green, actual.rgb.green); + assertEquals(expected.alpha, actual.alpha); + } +} diff --git a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java index 399514fe371..d1317883ea1 100644 --- a/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java +++ b/tests/org.eclipse.e4.ui.tests.css.swt/src/org/eclipse/e4/ui/tests/css/swt/CssSwtTestSuite.java @@ -8,6 +8,7 @@ * IBM Corporation - initial API and implementation * Stefan Winkler <stefan@winklerweb.net> - Bug 419482 * Thibault Le Ouay <thibaultleouay@gmail.com> - Bug 443094 + * Stefan Winkler <stefan@winklerweb.net> - Bug 459961 *******************************************************************************/ package org.eclipse.e4.ui.tests.css.swt; @@ -22,16 +23,40 @@ import org.eclipse.e4.ui.css.swt.properties.preference.EclipsePreferencesHandler import org.junit.runner.RunWith; import org.junit.runners.Suite; +// note to contributors: please ignore Eclipse default formatting and keep one class per line. @RunWith(Suite.class) -@Suite.SuiteClasses({ CSSSWTFontHelperTest.class, CSSSWTColorHelperTest.class, CSSResourcesHelpersTest.class, - SWTResourceRegistryKeyFactoryTest.class, SWTResourcesRegistryTest.class, FontDefinitionTest.class, - ColorDefinitionTest.class, ThemesExtensionTest.class, IEclipsePreferencesTest.class, - EclipsePreferencesHelperTest.class, CSSSWTWidgetTest.class, LabelTest.class, CTabFolderTest.class, - CTabItemTest.class, IdClassLabelColorTest.class, ShellTest.class, ButtonTest.class, GradientTest.class, - MarginTest.class, InnerClassElementTest.class, EclipsePreferencesHandlerTest.class, - PreferenceOverriddenByCssChangeListenerTest.class, ButtonTextTransformTest.class, LabelTextTransformTest.class, - TextTextTransformTest.class, DescendentTest.class, ThemeTest.class, Bug419482Test.class, ShellActiveTest.class, - InheritTest.class }) +@Suite.SuiteClasses({ + CSSSWTFontHelperTest.class, + CSSSWTColorHelperTest.class, + CSSResourcesHelpersTest.class, + SWTResourceRegistryKeyFactoryTest.class, + SWTResourcesRegistryTest.class, + FontDefinitionTest.class, + ColorDefinitionTest.class, + ThemesExtensionTest.class, + IEclipsePreferencesTest.class, + EclipsePreferencesHelperTest.class, + CSSSWTWidgetTest.class, + LabelTest.class, + CTabFolderTest.class, + CTabItemTest.class, + IdClassLabelColorTest.class, + ShellTest.class, + ButtonTest.class, + GradientTest.class, + MarginTest.class, + InnerClassElementTest.class, + EclipsePreferencesHandlerTest.class, + PreferenceOverriddenByCssChangeListenerTest.class, + ButtonTextTransformTest.class, + LabelTextTransformTest.class, + TextTextTransformTest.class, + DescendentTest.class, + ThemeTest.class, + Bug459961Test.class, + Bug419482Test.class, + ShellActiveTest.class, + InheritTest.class }) public class CssSwtTestSuite { } |