From 6ed264a77557887dfc4c0d425c0fc4a3369a92bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 5 Jan 2014 05:10:42 +0100 Subject: [PATCH] =?UTF-8?q?No=20need=20to=20clamp=20colors=20in=20conversi?= =?UTF-8?q?ons,=20just=20make=20sure=20hue=20stays=20within=20360=C2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/style/Color.js | 30 +++++++----------------------- 1 file changed, 7 insertions(+), 23 deletions(-) diff --git a/src/style/Color.js b/src/style/Color.js index e97337c4..e00ec80c 100644 --- a/src/style/Color.js +++ b/src/style/Color.js @@ -132,8 +132,9 @@ var Color = Base.extend(new function() { }, 'hsb-rgb': function(h, s, b) { - var h = (h / 60) % 6, // Scale to 0..6 - i = Math.floor(h), // 0..5 + // Scale h to 0..6 with modulo for negative values too + h = (((h / 60) % 6) + 6) % 6; + var i = Math.floor(h), // 0..5 f = h - i, i = hsbIndices[i], v = [ @@ -164,7 +165,8 @@ var Color = Base.extend(new function() { }, 'hsl-rgb': function(h, s, l) { - h /= 360; + // Scale h to 0..1 with modulo for negative values too + h = (((h / 360) % 1) + 1) % 1; if (s === 0) return [l, l, l]; var t3s = [ h + 1 / 3, h, h - 1 / 3 ], @@ -647,24 +649,6 @@ var Color = Base.extend(new function() { this._owner._changed(/*#=*/ Change.STYLE); }, - /** - * Returns a copy of the components array with all values clamped to - * valid numbers, based on the type of property they represent. - */ - _clamp: function() { - var components = this._components.slice(), - properties = this._properties; - if (this._type !== 'gradient') { - for (var i = 0, l = properties.length; i < l; i++) { - var value = components[i]; - components[i] = properties[i] === 'hue' - ? ((value % 360) + 360) % 360 - : value < 0 ? 0 : value > 1 ? 1 : value; - } - } - return components; - }, - /** * @return {Number[]} the converted components as an array */ @@ -673,11 +657,11 @@ var Color = Base.extend(new function() { return this._type === type ? this._components.slice() : (converter = converters[this._type + '-' + type]) - ? converter.apply(this, this._clamp()) + ? converter.apply(this, this._components) // Convert to and from rgb if no direct converter exists : converters['rgb-' + type].apply(this, converters[this._type + '-rgb'].apply(this, - this._clamp())); + this._components)); }, /**