You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by cz...@apache.org on 2012/12/30 23:21:43 UTC

svn commit: r1427002 - in /incubator/flex/falcon/trunk: compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/ compiler/src/org/apache/flex/compiler/internal/css/ compiler/src/org/apache/flex/compiler/internal/css/codegen/

Author: czadra
Date: Sun Dec 30 22:21:42 2012
New Revision: 1427002

URL: http://svn.apache.org/viewvc?rev=1427002&view=rev
Log:
FLEX-33275 added support to define a color with rgb(x,x,x) in css

Added:
    incubator/flex/falcon/trunk/compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValueTests.java   (with props)
    incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValue.java   (with props)
Modified:
    incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSS.g
    incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSTree.g
    incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/codegen/CSSReducer.java

Added: incubator/flex/falcon/trunk/compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValueTests.java
URL: http://svn.apache.org/viewvc/incubator/flex/falcon/trunk/compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValueTests.java?rev=1427002&view=auto
==============================================================================
--- incubator/flex/falcon/trunk/compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValueTests.java (added)
+++ incubator/flex/falcon/trunk/compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValueTests.java Sun Dec 30 22:21:42 2012
@@ -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) ));
+		
+	}
+
+}

Propchange: incubator/flex/falcon/trunk/compiler.tests/unit-tests/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValueTests.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSS.g
URL: http://svn.apache.org/viewvc/incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSS.g?rev=1427002&r1=1427001&r2=1427002&view=diff
==============================================================================
--- incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSS.g (original)
+++ incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSS.g Sun Dec 30 22:21:42 2012
@@ -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;}: . )* ')'

Added: incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValue.java
URL: http://svn.apache.org/viewvc/incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValue.java?rev=1427002&view=auto
==============================================================================
--- incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValue.java (added)
+++ incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValue.java Sun Dec 30 22:21:42 2012
@@ -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();
+    }
+}

Propchange: incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSRgbColorPropertyValue.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSTree.g
URL: http://svn.apache.org/viewvc/incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSTree.g?rev=1427002&r1=1427001&r2=1427002&view=diff
==============================================================================
--- incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSTree.g (original)
+++ incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/CSSTree.g Sun Dec 30 22:21:42 2012
@@ -300,6 +300,8 @@ singleValue returns [CSSPropertyValue pr
 		{ $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)

Modified: incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/codegen/CSSReducer.java
URL: http://svn.apache.org/viewvc/incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/codegen/CSSReducer.java?rev=1427002&r1=1427001&r2=1427002&view=diff
==============================================================================
--- incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/codegen/CSSReducer.java (original)
+++ incubator/flex/falcon/trunk/compiler/src/org/apache/flex/compiler/internal/css/codegen/CSSReducer.java Sun Dec 30 22:21:42 2012
@@ -55,6 +55,7 @@ import org.apache.flex.compiler.internal
 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 @@ public class CSSReducer implements ICSSC
         {
             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;