Browse Source

Support for THREE.Color( 'rgb(100%,0%,0%)' ) and capping values.

Mr.doob 12 years ago
parent
commit
19475d8320
1 changed files with 21 additions and 3 deletions
  1. 21 3
      src/core/Color.js

+ 21 - 3
src/core/Color.js

@@ -193,18 +193,36 @@ THREE.Color.prototype = {
 
 	setStyle: function ( style ) {
 
+		// rgb(255,0,0)
+
 		if ( /^rgb\((\d+),(\d+),(\d+)\)$/i.test( style ) ) {
 
 			var color = /^rgb\((\d+),(\d+),(\d+)\)$/i.exec( style );
 
-			this.r = parseInt( color[ 1 ], 10 ) / 255;
-			this.g = parseInt( color[ 2 ], 10 ) / 255;
-			this.b = parseInt( color[ 3 ], 10 ) / 255;
+			this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
+			this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
+			this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
+
+			return this;
+
+		}
+
+		// rgb(100%,0%,0%)
+
+		if ( /^rgb\((\d+)\%,(\d+)\%,(\d+)\%\)$/i.test( style ) ) {
+
+			var color = /^rgb\((\d+)\%,(\d+)\%,(\d+)\%\)$/i.exec( style );
+
+			this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
+			this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
+			this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
 
 			return this;
 
 		}
 
+		// red
+
 		if ( /^(\w+)$/i.test( style ) ) {
 
 			this.setHex( THREE.ColorKeywords[ style ] );