Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FLEX-33275 added support to define a color with rgb(x,x,x) in css

git-svn-id: https://svn.apache.org/repos/asf/incubator/flex/falcon/trunk@1427002 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
commit d8652af23b53d477f602e97a7f7968f40c748464 1 parent 4677305
Cyrill authored
View
108 compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValueTests.java
@@ -0,0 +1,108 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.flex.compiler.internal.css;
+
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.flex.compiler.css.ICSSPropertyValue;
+import org.junit.Test;
+
+/**
+ * JUnit tests for {@link CSSRgbColorPropertyValue}.
+ *
+ * @author Gordon Smith
+ */
+public class CSSRgbColorPropertyValueTests extends CSSPropertyValueTests {
+
+ private List<CSSRgbColorPropertyValue> getCSSRgbPropertyValues(String code) {
+ List<ICSSPropertyValue> propertyValues = getCSSPropertyValues(code);
+ List<CSSRgbColorPropertyValue> colorPropertyValues = new ArrayList<CSSRgbColorPropertyValue>();
+ for (ICSSPropertyValue icssPropertyValue : propertyValues) {
+ if(icssPropertyValue instanceof CSSRgbColorPropertyValue)
+ colorPropertyValues.add( (CSSRgbColorPropertyValue) icssPropertyValue );
+ }
+ return colorPropertyValues;
+ }
+
+ @Test
+ public void CSSColorPropertyValue_textColor()
+ {
+ String code = " color: rgb(100%, 0%, 0%); ";
+
+ List<CSSRgbColorPropertyValue> rgbColorProperties = getCSSRgbPropertyValues(code);
+ assertThat("rgbColorProperties.size()" , rgbColorProperties.size(), is(1) );
+
+ CSSRgbColorPropertyValue rgbColorPropertyValue = rgbColorProperties.get(0);
+ assertThat("rgbColorPropertyValue.getOperator()" , rgbColorPropertyValue.getOperator(), is( CSSModelTreeType.PROPERTY_VALUE ) );
+ assertThat("rgbColorPropertyValue.getRawRgb()" , rgbColorPropertyValue.getRawRgb(), is( "rgb(100%, 0%, 0%)" ) );
+ assertThat("rgbColorPropertyValue.getColorAsInt()" , rgbColorPropertyValue.getColorAsInt(), is( Integer.parseInt("ff0000", 16) ) );
+ }
+
+ @Test
+ public void CSSColorPropertyValue_textColor2()
+ {
+ String code = " color: rgb(100%, 255, 100%); ";
+
+ List<CSSRgbColorPropertyValue> rgbColorProperties = getCSSRgbPropertyValues(code);
+ assertThat("rgbColorProperties.size()" , rgbColorProperties.size(), is(1) );
+
+ CSSRgbColorPropertyValue rgbColorPropertyValue = rgbColorProperties.get(0);
+ assertThat("rgbColorPropertyValue.getOperator()" , rgbColorPropertyValue.getOperator(), is( CSSModelTreeType.PROPERTY_VALUE ) );
+ assertThat("rgbColorPropertyValue.getRawRgb()" , rgbColorPropertyValue.getRawRgb(), is( "rgb(100%, 255, 100%)" ) );
+ assertThat("rgbColorPropertyValue.getColorAsInt()" , rgbColorPropertyValue.getColorAsInt(), is( Integer.parseInt("ffffff",16) ) );
+ }
+
+ @Test
+ public void CSSColorPropertyValue_textColor3()
+ {
+ String code = " color: rgb(100%, , 100%); ";
+
+ List<CSSRgbColorPropertyValue> rgbColorProperties = getCSSRgbPropertyValues(code);
+ assertThat("rgbColorProperties.size()" , rgbColorProperties.size(), is(0) );
+ }
+
+ @Test
+ public void CSSRgbPropertyValue_getRgbValues() {
+
+ assertThat(CSSRgbColorPropertyValue.getIntValue( "rgb(100%, 255, 0)" ),
+ is( Integer.parseInt("ffff00", 16) ));
+
+ assertThat(CSSRgbColorPropertyValue.getIntValue( "rgb(100%, 100%, 100%)" ),
+ is( Integer.parseInt("ffffff", 16) ));
+
+ assertThat(CSSRgbColorPropertyValue.getIntValue( "rgb(100%, 255, 100%)" ),
+ is( Integer.parseInt("ffffff", 16) ));
+
+ assertThat(CSSRgbColorPropertyValue.getIntValue( "rgb(100%, 2%, 100%)" ),
+ is( Integer.parseInt("ff05ff", 16) ));
+
+ assertThat(CSSRgbColorPropertyValue.getIntValue( "rgb(256, 0, 0)" ),
+ is( Integer.parseInt("000000", 16) ));
+
+ assertThat(CSSRgbColorPropertyValue.getIntValue( "rgb(254.5, 99.5%, 0)" ),
+ is( Integer.parseInt("fefc00", 16) ));
+
+ }
+
+}
View
10 compiler/src/org/apache/flex/compiler/internal/css/CSS.g
@@ -384,6 +384,7 @@ value
* For example:
* 12, 12px, 100%.
* #FF3322
+ * rgb(100%, 100%, 100%)
* ClassReference("mx.controls.Button")
* PropertyReference("size")
* Embed(source="assets/logo.png", mimeType="images/png")
@@ -403,6 +404,7 @@ singleValue
-> ^(EMBED ARGUMENTS)
| URL ARGUMENTS -> ^(URL ARGUMENTS)
| LOCAL ARGUMENTS -> ^(LOCAL ARGUMENTS)
+ | RGB
| STRING
| ID
;
@@ -428,6 +430,14 @@ URL : 'url' ;
LOCAL : 'local' ;
NULL : 'null' ;
+/**
+ * Matches a rgb definition - rgb(100%,100%,100%)
+ */
+RGB : 'rgb(' ( WS* NUMBER ( PERCENT | ) WS* ) ','
+ ( WS* NUMBER ( PERCENT | ) WS* ) ','
+ ( WS* NUMBER ( PERCENT | ) WS* )
+ ')' ;
+
/** Arguments of a function call property value. */
ARGUMENTS
: '(' ( options {greedy=false;}: . )* ')'
View
113 compiler/src/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValue.java
@@ -0,0 +1,113 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.flex.compiler.internal.css;
+
+import java.util.StringTokenizer;
+
+import org.antlr.runtime.Token;
+import org.antlr.runtime.TokenStream;
+import org.antlr.runtime.tree.CommonTree;
+
+/**
+ * Implementation for CSS rgb (0,0,0) property values.
+ */
+public class CSSRgbColorPropertyValue extends CSSPropertyValue
+{
+
+ /**
+ * Create a color property value from AST with rgb color value such as
+ * {@code rgb(100%, 0%, 0%) }.
+ *
+ * @param rawRgb
+ * @param tree AST
+ * @param tokenStream token stream
+ */
+ protected CSSRgbColorPropertyValue(final String rawRgb,
+ final CommonTree tree, final TokenStream tokenStream)
+ {
+ super(tree, tokenStream, CSSModelTreeType.PROPERTY_VALUE);
+ this.token = tree.token;
+ this.rawRgb = rawRgb;
+ this.colorInt = getIntValue( this.rawRgb ) ;
+ }
+
+
+ /**
+ * Computes from the given rgb definition a int value.
+ *
+ * @param String rgb definition - rgb(100, 0, 0)
+ * @return int value bit color.
+ */
+ protected static int getIntValue(String rgb)
+ {
+ rgb = rgb.substring(4, rgb.length() - 1);
+
+ StringTokenizer st = new StringTokenizer(rgb, ",");
+ StringBuffer sb = new StringBuffer();
+ while (st.hasMoreElements())
+ {
+ int digit;
+ String t = (String)st.nextElement();
+ t = t.trim();
+ if(t.contains("%")) {
+ t = t.replaceAll("%", "");
+ digit = ( Float.valueOf(t).intValue() * 255) / 100;
+ sb.append(Character.forDigit((digit >> 4) & 15, 16));
+ sb.append(Character.forDigit(digit & 15, 16));
+
+ } else {
+ digit = Float.valueOf(t).intValue();
+ sb.append(Character.forDigit((digit >> 4) & 15, 16));
+ sb.append(Character.forDigit(digit & 15, 16));
+ }
+ }
+ return Integer.parseInt( sb.toString(), 16 );
+ }
+
+ private final Token token;
+ private final int colorInt;
+ private final String rawRgb;
+
+ /**
+ * @return Integer value bit color.
+ */
+ public int getColorAsInt()
+ {
+ return colorInt;
+ }
+
+ /**
+ * Get the original rgb definition in the CSS document.
+ * <p>
+ * For example: {@code 255,0,0}, {@code 100%,0,0}, {@code 255,0,100%}.
+ *
+ * @return Original rgb property value text in the CSS document.
+ */
+ public String getRawRgb()
+ {
+ return rawRgb;
+ }
+
+ @Override
+ public String toString()
+ {
+ return token.getText();
+ }
+}
View
2  compiler/src/org/apache/flex/compiler/internal/css/CSSTree.g
@@ -300,6 +300,8 @@ singleValue returns [CSSPropertyValue propertyValue]
{ $propertyValue = new CSSNumberPropertyValue($NUMBER_WITH_UNIT.text, $start, tokenStream); }
| HASH_WORD
{ $propertyValue = new CSSColorPropertyValue($start, tokenStream); }
+ | RGB
+ { $propertyValue = new CSSRgbColorPropertyValue($RGB.text, $start, tokenStream); }
| ^(CLASS_REFERENCE cr=ARGUMENTS)
{ $propertyValue = new CSSFunctionCallPropertyValue($CLASS_REFERENCE.text, $cr.text, $start, tokenStream); }
| ^(PROPERTY_REFERENCE pr=ARGUMENTS)
View
5 compiler/src/org/apache/flex/compiler/internal/css/codegen/CSSReducer.java
@@ -55,6 +55,7 @@
import org.apache.flex.compiler.internal.css.CSSFunctionCallPropertyValue;
import org.apache.flex.compiler.internal.css.CSSKeywordPropertyValue;
import org.apache.flex.compiler.internal.css.CSSNumberPropertyValue;
+import org.apache.flex.compiler.internal.css.CSSRgbColorPropertyValue;
import org.apache.flex.compiler.internal.css.CSSRule;
import org.apache.flex.compiler.internal.css.CSSSelector;
import org.apache.flex.compiler.internal.css.CSSStringPropertyValue;
@@ -352,6 +353,10 @@ else if (value instanceof CSSColorPropertyValue)
{
valueInstructions.addInstruction(ABCConstants.OP_pushint, new Integer(((CSSColorPropertyValue)value).getColorAsInt()));
}
+ else if (value instanceof CSSRgbColorPropertyValue)
+ {
+ valueInstructions.addInstruction(ABCConstants.OP_pushint, new Integer(((CSSRgbColorPropertyValue)value).getColorAsInt()));
+ }
else if (value instanceof CSSKeywordPropertyValue)
{
CSSKeywordPropertyValue keywordValue = (CSSKeywordPropertyValue)value;
Please sign in to comment.
Something went wrong with that request. Please try again.