浏览代码

Merge pull request #14332 from WestLangley/dev-sRGB_color

THREE.Color: added sRGB conversion methods
Mr.doob 7 年之前
父节点
当前提交
ebc162ada4
共有 2 个文件被更改,包括 84 次插入4 次删除
  1. 28 4
      docs/api/math/Color.html
  2. 56 0
      src/math/Color.js

+ 28 - 4
docs/api/math/Color.html

@@ -124,13 +124,23 @@ var color = new THREE.Color( 1, 0, 0 );
 		<h3>[method:Color convertGammaToLinear]( [param:Float gammaFactor] ) </h3>
 		<p>
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
-		Converts this color from gamma 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>
 		
 		<h3>[method:Color convertLinearToGamma]( [param:Float gammaFactor] ) </h3>
 		<p>
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
-		Converts this color from linear to gamma space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of 1 / [page:Float gammaFactor].
+		Converts this color from linear space to gamma space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of 1 / [page:Float gammaFactor].
+		</p>
+
+		<h3>[method:Color convertLinearToSRGB]() </h3>
+		<p>
+		Converts this color from linear space to sRGB space.
+		</p>
+
+		<h3>[method:Color convertSRGBToLinear]() </h3>
+		<p>
+		Converts this color from sRGB space to linear space.
 		</p>
 
 		<h3>[method:Color copyGammaToLinear]( [param:Color color], [param:Float gammaFactor] ) </h3>
@@ -138,7 +148,7 @@ var color = new THREE.Color( 1, 0, 0 );
 		[page:Color color] — Color to copy.<br />
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
 
-		Copies the given color into this color while converting it from gamma to linear space
+		Copies the given color into this color, and then 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>
 
@@ -147,10 +157,24 @@ var color = new THREE.Color( 1, 0, 0 );
 		[page:Color color] — Color to copy.<br />
 		[page:Float gammaFactor] - (optional). Default is *2.0*.<br /><br />
 
-		Copies the given color into this color while converting it from linear to gamma space
+		Copies the given color into this color, and then converts this color from linear space to gamma space
 		by taking [page:.r r], [page:.g g] and [page:.b b] to the power of 1 / [page:Float gammaFactor].
 		</p>
 
+		<h3>[method:Color copyLinearToSRGB]( [param:Color color]] ) </h3>
+		<p>
+		[page:Color color] — Color to copy.<br />
+
+		Copies the given color into this color, and then converts this color from linear space to sRGB space.
+		</p>
+
+		<h3>[method:Color copySRGBToLinear]( [param:Color color] ) </h3>
+		<p>
+		[page:Color color] — Color to copy.<br />
+
+		Copies the given color into this color, and then converts this color from sRGB space to linear space.
+		</p>
+
 		<h3>[method:Boolean equals]( [param:Color color] ) </h3>
 		<p>Compares the RGB values of [page:Color color] with those of this object. Returns true if they are the same, false otherwise.</p>
 

+ 56 - 0
src/math/Color.js

@@ -329,6 +329,62 @@ Object.assign( Color.prototype, {
 
 	},
 
+	copySRGBToLinear: function () {
+
+		function SRGBToLinear( c ) {
+
+			return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
+
+		}
+
+		return function copySRGBToLinear( color ) {
+
+			this.r = SRGBToLinear( color.r );
+			this.g = SRGBToLinear( color.g );
+			this.b = SRGBToLinear( color.b );
+
+			return this;
+
+		};
+
+	}(),
+
+	copyLinearToSRGB: function () {
+
+		function LinearToSRGB( c ) {
+
+			return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055;
+
+		}
+
+		return function copyLinearToSRGB( color ) {
+
+			this.r = LinearToSRGB( color.r );
+			this.g = LinearToSRGB( color.g );
+			this.b = LinearToSRGB( color.b );
+
+			return this;
+
+		};
+
+	}(),
+
+	convertSRGBToLinear: function () {
+
+		this.copySRGBToLinear( this );
+
+		return this;
+
+	},
+
+	convertLinearToSRGB: function () {
+
+		this.copyLinearToSRGB( this );
+
+		return this;
+
+	},
+
 	getHex: function () {
 
 		return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0;