瀏覽代碼

Merge remote-tracking branch 'bhouston/color-converter' into dev

Mr.doob 12 年之前
父節點
當前提交
e5d3ea7be8

+ 76 - 0
examples/js/math/ColorConverter.js

@@ -0,0 +1,76 @@
+/**
+ * @author bhouston / http://exocortex.com/
+ */
+
+THREE.ColorConverter = {
+
+	fromRGB: function ( color, r, g, b ) {
+
+		return color.setRGB( r, g, b );
+
+	},
+
+	fromHex: function ( color, hex ) {
+
+		return color.setHex( hex );
+
+	},
+
+	toHex: function ( color ) {
+
+		return color.getHex();
+
+	},
+	 
+	toHexString: function ( color ) {
+
+		return color.getHexString( color );
+
+	},
+
+	fromHSL: function ( color, h, s, l ) {
+
+		return color.setHSL( h, s, l );
+	},
+
+	toHSL: function ( color ) {
+
+		return color.getHSL( color );
+
+	},
+
+	fromHSV: function ( color, h, s, v ) {
+
+		// https://gist.github.com/xpansive/1337890#file-index-js
+		return color.setHSL( h, ( s * v ) / ( ( h = ( 2 - s ) * v ) < 1 ? h : ( 2 - h ) ), h * 0.5 );
+
+	},
+
+	toHSV: function( color ) {
+
+		var hsl = color.getHSL();
+
+		// based on https://gist.github.com/xpansive/1337890#file-index-js
+		hsl.s *= ( hsl.l < 0.5 ) ? hsl.l : ( 1 - hsl.l );
+
+		return {
+			h: hsl.h,
+			s: 2 * hsl.s / ( hsl.l + hsl.s ),
+			v: hsl.l + hsl.s
+		};
+		
+	},
+
+	fromStyle: function ( color, style ) {
+
+		return color.setStyle( style );
+
+	},
+
+	toStyle: function ( color ) {
+
+		return color.getStyle( color );
+
+	}
+
+};

+ 6 - 0
src/math/Color.js

@@ -351,6 +351,12 @@ THREE.extend( THREE.Color.prototype, {
 
 	},
 
+	equals: function ( c ) {
+
+		return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
+
+	},
+
 	clone: function () {
 
 		return new THREE.Color().setRGB( this.r, this.g, this.b );

+ 35 - 0
test/unit/math/ColorConverter.js

@@ -0,0 +1,35 @@
+module( "ColorConverter" );
+
+
+test( "fromRGB", function(){
+    var c1 = new THREE.Color();
+    var c2 = new THREE.Color().setRGB( 0, 0.5, 1 );
+    THREE.ColorConverter.fromRGB( c1, 0, 0.5, 1 );
+    ok( c1.equals( c2 ), "Ok" );
+});
+
+test( "fromHex/toHex", function(){
+    var c1 = new THREE.Color();
+    var c2 = new THREE.Color().setHex( 0x11aaff );
+    THREE.ColorConverter.fromHex( c1, 0x11aaff );
+    ok( c1.equals( c2 ), "Ok" );
+    var hex = THREE.ColorConverter.toHex( c1 );
+    ok( hex === 0x11aaff, "Ok" );
+});
+
+test( "fromHSV/toHSV", function(){
+    var c1 = new THREE.Color();
+   var c2 = new THREE.Color().setHSL( 0.25, 0.5, 0.75 );
+    var hsv = THREE.ColorConverter.toHSV( c2 );
+    THREE.ColorConverter.fromHSV( c1, hsv.h, hsv.s, hsv.v );
+    ok( c1.equals( c2 ), "Ok" );
+});
+
+test( "fromHSL/toHSL", function(){
+    var c1 = new THREE.Color();
+    var c2 = new THREE.Color().setHSL( 0.25, 0.5, 0.75 );
+    var hsl = THREE.ColorConverter.toHSL( c2 );
+    THREE.ColorConverter.fromHSL( c1, hsl.h, hsl.s, hsl.l );
+    ok( c1.equals( c2 ), "Ok" );
+});
+

+ 0 - 1
test/unit/math/Line3.js

@@ -55,7 +55,6 @@ test( "closestPointToPoint/closestPointToPointParameter", function() {
 	// nearby the ray
 	ok( a.closestPointToPointParameter( zero3.clone(), false ) == -1, "Passed!" );
 	var b2 = a.closestPointToPoint( zero3.clone(), false );
-	console.log( b2 );
 	ok( b2.distanceTo( new THREE.Vector3( 1, 1, 0 ) ) < 0.0001, "Passed!" );
 
 	// nearby the ray

+ 2 - 0
test/unit/unittests_sources.html

@@ -28,6 +28,7 @@
   <script src="../../src/math/Sphere.js"></script>
   <script src="../../src/math/Math.js"></script>  
   <script src="../../src/math/Triangle.js"></script>
+  <script src="../../examples/js/math/ColorConverter.js"></script>
 
   <!-- add class-based unit tests below -->
 
@@ -47,6 +48,7 @@
   <script src="math/Matrix4.js"></script>
   <script src="math/Frustum.js"></script>
   <script src="math/Color.js"></script>
+  <script src="math/ColorConverter.js"></script>
 
 </body>
 </html>

+ 3 - 1
test/unit/unittests_three-math.html

@@ -12,6 +12,7 @@
   <!-- add ThreeJS sources to test below -->
 
   <script src="../../build/three-math.js"></script>
+  <script src="../../examples/js/math/ColorConverter.js"></script>
 
   <!-- add class-based unit tests below -->
 
@@ -30,6 +31,7 @@
   <script src="math/Matrix3.js"></script>
   <script src="math/Matrix4.js"></script>
   <script src="math/Frustum.js"></script>
-
+  <script src="math/ColorConverter.js"></script>
+  
 </body>
 </html>

+ 3 - 1
test/unit/unittests_three.html

@@ -12,6 +12,7 @@
   <!-- add ThreeJS sources to test below -->
 
   <script src="../../build/three.js"></script>
+  <script src="../../examples/js/math/ColorConverter.js"></script>
 
   <!-- add class-based unit tests below -->
 
@@ -30,6 +31,7 @@
   <script src="math/Matrix3.js"></script>
   <script src="math/Matrix4.js"></script>
   <script src="math/Frustum.js"></script>
-
+  <script src="math/ColorConverter.js"></script>
+  
 </body>
 </html>

+ 3 - 1
test/unit/unittests_three.min.html

@@ -12,6 +12,7 @@
   <!-- add ThreeJS sources to test below -->
 
   <script src="../../build/three.min.js"></script>
+  <script src="../../examples/js/math/ColorConverter.js"></script>
 
   <!-- add class-based unit tests below -->
 
@@ -30,6 +31,7 @@
   <script src="math/Matrix3.js"></script>
   <script src="math/Matrix4.js"></script>
   <script src="math/Frustum.js"></script>
-
+  <script src="math/ColorConverter.js"></script>
+  
 </body>
 </html>