diff --git a/docs/api/math/Color.html b/docs/api/math/Color.html index 00d7d8535d8684a51cdb1760d7184339665c11b8..91191bbcc4be24c715582165bc2b51bfd9680b09 100644 --- a/docs/api/math/Color.html +++ b/docs/api/math/Color.html @@ -162,10 +162,9 @@ var color = new THREE.Color( 1, 0, 0 );

[method:String getHexString]()

Returns the hexadecimal value of this color as a string (for example, 'FFFFFF').
-

[method:Object getHSL]( [param:Object optionalTarget] )

+

[method:Object getHSL]( [param:Object target] )

- [page:Object optionalTarget] — (optional) if specified, adds h, s and l keys to the object (if not already present) - and sets the results there. Otherwise, a new Object will be created.

+ [page:Object target] — the result will be copied into this Object. Adds h, s and l keys to the object (if not already present).

Convert this Color's [page:.r r], [page:.g g] and [page:.b b] values to [link:https://en.wikipedia.org/wiki/HSL_and_HSV HSL] format and returns an object of the form: diff --git a/examples/js/math/ColorConverter.js b/examples/js/math/ColorConverter.js index 8a49f05716db6df1e4292a024807828af5f9d5fe..15dd84e76593971d35fa46afea6ffa29bfb73ff4 100644 --- a/examples/js/math/ColorConverter.js +++ b/examples/js/math/ColorConverter.js @@ -17,20 +17,33 @@ THREE.ColorConverter = { }, - getHSV: function( color ) { + getHSV: function() { - var hsl = color.getHSL(); + var hsl = {}; - // based on https://gist.github.com/xpansive/1337890#file-index-js - hsl.s *= ( hsl.l < 0.5 ) ? hsl.l : ( 1 - hsl.l ); + return function getHSV( color, target ) { + + if ( target === undefined ) { + + console.warn( 'THREE.ColorConverter: .getHSV() target is now required' ); + target = { h: 0, s: 0, l: 0 }; + + } + + color.getHSL( hsl ); + + // based on https://gist.github.com/xpansive/1337890#file-index-js + hsl.s *= ( hsl.l < 0.5 ) ? hsl.l : ( 1 - hsl.l ); + + target.h = hsl.h; + target.s = 2 * hsl.s / ( hsl.l + hsl.s ); + target.v = hsl.l + hsl.s; + + return target; - return { - h: hsl.h, - s: 2 * hsl.s / ( hsl.l + hsl.s ), - v: hsl.l + hsl.s }; - }, + }(), // where c, m, y, k is between 0 and 1 @@ -44,19 +57,30 @@ THREE.ColorConverter = { }, - getCMYK: function ( color ) { + getCMYK: function ( color, target ) { + + if ( target === undefined ) { + + console.warn( 'THREE.ColorConverter: .getCMYK() target is now required' ); + target = { c: 0, m: 0, y: 0, k:0 }; + + } var r = color.r; var g = color.g; var b = color.b; + var k = 1 - Math.max( r, g, b ); var c = ( 1 - r - k ) / ( 1 - k ); var m = ( 1 - g - k ) / ( 1 - k ); var y = ( 1 - b - k ) / ( 1 - k ); - return { - c: c, m: m, y: y, k: k - }; + target.c = c; + target.m = m; + target.y = y; + target.k = k; + + return target; } diff --git a/src/math/Color.js b/src/math/Color.js index 56a909d073877cc2ab900f0f60c67f13ef99ab9c..34987a347954f4e9bcbe789c2bfd1b96e403ab26 100644 --- a/src/math/Color.js +++ b/src/math/Color.js @@ -347,11 +347,16 @@ Object.assign( Color.prototype, { }, - getHSL: function ( optionalTarget ) { + getHSL: function ( target ) { // h,s,l ranges are in 0.0 - 1.0 - var hsl = optionalTarget || { h: 0, s: 0, l: 0 }; + if ( target === undefined ) { + + console.warn( 'THREE.Color: .getHSL() target is now required' ); + target = { h: 0, s: 0, l: 0 }; + + } var r = this.r, g = this.g, b = this.b; @@ -384,11 +389,11 @@ Object.assign( Color.prototype, { } - hsl.h = hue; - hsl.s = saturation; - hsl.l = lightness; + target.h = hue; + target.s = saturation; + target.l = lightness; - return hsl; + return target; }, @@ -398,17 +403,23 @@ Object.assign( Color.prototype, { }, - offsetHSL: function ( h, s, l ) { + offsetHSL: function () { - var hsl = this.getHSL(); + var hsl = {}; - hsl.h += h; hsl.s += s; hsl.l += l; + return function ( h, s, l ) { - this.setHSL( hsl.h, hsl.s, hsl.l ); + this.getHSL( hsl ); - return this; + hsl.h += h; hsl.s += s; hsl.l += l; - }, + this.setHSL( hsl.h, hsl.s, hsl.l ); + + return this; + + }; + + }(), add: function ( color ) {