瀏覽代碼

Merge pull request #14784 from pauloddr/patch-color-improvements

Color: add .setColorName() and Color.NAMES
Michael Herzog 5 年之前
父節點
當前提交
bb640b1c84
共有 4 個文件被更改,包括 62 次插入20 次删除
  1. 10 1
      docs/api/en/math/Color.html
  2. 12 0
      src/math/Color.d.ts
  3. 18 9
      src/math/Color.js
  4. 22 10
      test/unit/src/math/Color.tests.js

+ 10 - 1
docs/api/en/math/Color.html

@@ -126,7 +126,7 @@ var color = new THREE.Color( 1, 0, 0 );
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
 		Converts this color from gamma space to linear space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of [page:Float gammaFactor].
 		Converts this color from gamma space to linear space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of [page:Float gammaFactor].
 		</p>
 		</p>
-		
+
 		<h3>[method:Color convertLinearToGamma]( [param:Float gammaFactor] ) </h3>
 		<h3>[method:Color convertLinearToGamma]( [param:Float gammaFactor] ) </h3>
 		<p>
 		<p>
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
@@ -302,6 +302,15 @@ var color = new THREE.Color( 1, 0, 0 );
 		Note that for X11 color names, multiple words such as Dark Orange become the string 'darkorange' (all lowercase).
 		Note that for X11 color names, multiple words such as Dark Orange become the string 'darkorange' (all lowercase).
 		</p>
 		</p>
 
 
+		<h3>[method:Color setColorName]( [param:String style] ) </h3>
+		<p>
+		[page:String style] — color name ( from [link:https://en.wikipedia.org/wiki/X11_color_names#Color_name_chart X11 color names] ).<br /><br />
+
+		Sets this color from a color name. Faster than [page:.setStyle] method if you don't need the other CSS-style formats.<br/><br/>
+
+		For convenience, the list of names is exposed in Color.NAMES as a hash: <code>Color.NAMES.aliceblue // returns 0xF0F8FF</code>
+		</p>
+
 		<h3>[method:Color sub]( [param:Color color] ) </h3>
 		<h3>[method:Color sub]( [param:Color color] ) </h3>
 		<p>
 		<p>
 		Subtracts the RGB components of the given color from the RGB components of this color.
 		Subtracts the RGB components of the given color from the RGB components of this color.

+ 12 - 0
src/math/Color.d.ts

@@ -68,6 +68,13 @@ export class Color {
 	 */
 	 */
 	setStyle( style: string ): Color;
 	setStyle( style: string ): Color;
 
 
+	/**
+	 * Sets this color from a color name.
+	 * Faster than {@link Color#setStyle .setStyle()} method if you don't need the other CSS-style formats.
+	 * @param style Color name in X11 format.
+	 */
+	setColorName( style: string ): Color;
+
 	/**
 	/**
 	 * Clones this color.
 	 * Clones this color.
 	 */
 	 */
@@ -183,4 +190,9 @@ export class Color {
 	 */
 	 */
 	toArray( xyz: ArrayLike<number>, offset?: number ): ArrayLike<number>;
 	toArray( xyz: ArrayLike<number>, offset?: number ): ArrayLike<number>;
 
 
+	/**
+	 * List of X11 color names.
+	 */
+	static NAMES: Record<string, number>;
+
 }
 }

+ 18 - 9
src/math/Color.js

@@ -261,20 +261,28 @@ Object.assign( Color.prototype, {
 
 
 		if ( style && style.length > 0 ) {
 		if ( style && style.length > 0 ) {
 
 
-			// color keywords
-			var hex = _colorKeywords[ style ];
+			return this.setColorName( style );
 
 
-			if ( hex !== undefined ) {
+		}
 
 
-				// red
-				this.setHex( hex );
+		return this;
 
 
-			} else {
+	},
 
 
-				// unknown color
-				console.warn( 'THREE.Color: Unknown color ' + style );
+	setColorName: function ( style ) {
 
 
-			}
+		// color keywords
+		var hex = _colorKeywords[ style ];
+
+		if ( hex !== undefined ) {
+
+			// red
+			this.setHex( hex );
+
+		} else {
+
+			// unknown color
+			console.warn( 'THREE.Color: Unknown color ' + style );
 
 
 		}
 		}
 
 
@@ -580,5 +588,6 @@ Object.assign( Color.prototype, {
 
 
 } );
 } );
 
 
+Color.NAMES = _colorKeywords;
 
 
 export { Color };
 export { Color };

+ 22 - 10
test/unit/src/math/Color.tests.js

@@ -28,6 +28,13 @@ export default QUnit.module( 'Maths', () => {
 
 
 		} );
 		} );
 
 
+		// EXPOSED CONSTANTS
+		QUnit.test( "Color.NAMES", ( assert ) => {
+
+			assert.ok( Color.NAMES.aliceblue == 0xF0F8FF, "Exposed Color.NAMES" );
+
+		} );
+
 		// PUBLIC STUFF
 		// PUBLIC STUFF
 		QUnit.test( "isColor", ( assert ) => {
 		QUnit.test( "isColor", ( assert ) => {
 
 
@@ -137,6 +144,21 @@ export default QUnit.module( 'Maths', () => {
 			assert.ok( a.g == 0xAB / 255, "Green: " + a.g );
 			assert.ok( a.g == 0xAB / 255, "Green: " + a.g );
 			assert.ok( a.b == 0xC1 / 255, "Blue: " + a.b );
 			assert.ok( a.b == 0xC1 / 255, "Blue: " + a.b );
 
 
+			a.setStyle( "aliceblue" );
+			assert.ok( a.r == 0xF0 / 255, "Red: " + a.r );
+			assert.ok( a.g == 0xF8 / 255, "Green: " + a.g );
+			assert.ok( a.b == 0xFF / 255, "Blue: " + a.b );
+
+		} );
+
+		QUnit.test( "setColorName", ( assert ) => {
+
+			var c = new Color();
+			var res = c.setColorName( "aliceblue" );
+
+			assert.ok( c.getHex() == 0xF0F8FF, "Hex: " + c.getHex() );
+			assert.ok( c == res, "Returns Self" );
+
 		} );
 		} );
 
 
 		QUnit.test( "clone", ( assert ) => {
 		QUnit.test( "clone", ( assert ) => {
@@ -343,16 +365,6 @@ export default QUnit.module( 'Maths', () => {
 
 
 		} );
 		} );
 
 
-		QUnit.test( "setRGB", ( assert ) => {
-
-			var c = new Color();
-			c.setRGB( 1, 0.2, 0.1 );
-			assert.ok( c.r == 1, "Red: " + c.r );
-			assert.ok( c.g == 0.2, "Green: " + c.g );
-			assert.ok( c.b == 0.1, "Blue: " + c.b );
-
-		} );
-
 		QUnit.test( "lerp", ( assert ) => {
 		QUnit.test( "lerp", ( assert ) => {
 
 
 			var c = new Color();
 			var c = new Color();