From aa1a71ed966b90f89a63360658610a6fb608fa79 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 19 Jun 2013 13:38:57 +0200 Subject: [PATCH 1/3] Don't attempt to constrain values in setters --- src/ol/layer/layer.js | 39 +++++++++----------------------- test/spec/ol/layer/layer.test.js | 30 ------------------------ 2 files changed, 11 insertions(+), 58 deletions(-) diff --git a/src/ol/layer/layer.js b/src/ol/layer/layer.js index 6c0166ff3f..b5f69761f1 100644 --- a/src/ol/layer/layer.js +++ b/src/ol/layer/layer.js @@ -122,13 +122,13 @@ ol.layer.Layer.prototype.getLayerState = function() { var saturation = this.getSaturation(); var visible = this.getVisible(); return { - brightness: goog.isDef(brightness) ? brightness : 0, - contrast: goog.isDef(contrast) ? contrast : 1, + brightness: goog.isDef(brightness) ? goog.math.clamp(brightness, -1, 1) : 0, + contrast: goog.isDef(contrast) ? Math.max(contrast, 0) : 1, hue: goog.isDef(hue) ? hue : 0, - opacity: goog.isDef(opacity) ? opacity : 1, + opacity: goog.isDef(opacity) ? goog.math.clamp(opacity, 0, 1) : 1, ready: ready, - saturation: goog.isDef(saturation) ? saturation : 1, - visible: goog.isDef(visible) ? visible : true + saturation: goog.isDef(saturation) ? Math.max(saturation, 0) : 1, + visible: goog.isDef(visible) ? !!visible : true }; }; @@ -214,10 +214,7 @@ ol.layer.Layer.prototype.isReady = function() { * @param {number} brightness Brightness. */ ol.layer.Layer.prototype.setBrightness = function(brightness) { - brightness = goog.math.clamp(brightness, -1, 1); - if (brightness != this.getBrightness()) { - this.set(ol.layer.LayerProperty.BRIGHTNESS, brightness); - } + this.set(ol.layer.LayerProperty.BRIGHTNESS, brightness); }; goog.exportProperty( ol.layer.Layer.prototype, @@ -233,10 +230,7 @@ goog.exportProperty( * @param {number} contrast Contrast. */ ol.layer.Layer.prototype.setContrast = function(contrast) { - contrast = Math.max(0, contrast); - if (contrast != this.getContrast()) { - this.set(ol.layer.LayerProperty.CONTRAST, contrast); - } + this.set(ol.layer.LayerProperty.CONTRAST, contrast); }; goog.exportProperty( ol.layer.Layer.prototype, @@ -250,9 +244,7 @@ goog.exportProperty( * @param {number} hue Hue. */ ol.layer.Layer.prototype.setHue = function(hue) { - if (hue != this.getHue()) { - this.set(ol.layer.LayerProperty.HUE, hue); - } + this.set(ol.layer.LayerProperty.HUE, hue); }; goog.exportProperty( ol.layer.Layer.prototype, @@ -264,10 +256,7 @@ goog.exportProperty( * @param {number} opacity Opacity. */ ol.layer.Layer.prototype.setOpacity = function(opacity) { - opacity = goog.math.clamp(opacity, 0, 1); - if (opacity != this.getOpacity()) { - this.set(ol.layer.LayerProperty.OPACITY, opacity); - } + this.set(ol.layer.LayerProperty.OPACITY, opacity); }; goog.exportProperty( ol.layer.Layer.prototype, @@ -284,10 +273,7 @@ goog.exportProperty( * @param {number} saturation Saturation. */ ol.layer.Layer.prototype.setSaturation = function(saturation) { - saturation = Math.max(0, saturation); - if (saturation != this.getSaturation()) { - this.set(ol.layer.LayerProperty.SATURATION, saturation); - } + this.set(ol.layer.LayerProperty.SATURATION, saturation); }; goog.exportProperty( ol.layer.Layer.prototype, @@ -299,10 +285,7 @@ goog.exportProperty( * @param {boolean} visible Visible. */ ol.layer.Layer.prototype.setVisible = function(visible) { - visible = !!visible; - if (visible != this.getVisible()) { - this.set(ol.layer.LayerProperty.VISIBLE, visible); - } + this.set(ol.layer.LayerProperty.VISIBLE, visible); }; goog.exportProperty( ol.layer.Layer.prototype, diff --git a/test/spec/ol/layer/layer.test.js b/test/spec/ol/layer/layer.test.js index 3c57798477..2f23523426 100644 --- a/test/spec/ol/layer/layer.test.js +++ b/test/spec/ol/layer/layer.test.js @@ -101,16 +101,6 @@ describe('ol.layer.Layer', function() { expect(layer.getBrightness()).to.be(-0.7); }); - it('clamps to 1', function() { - layer.setBrightness(1.5); - expect(layer.getBrightness()).to.be(1); - }); - - it('clamps to -1', function() { - layer.setBrightness(-3); - expect(layer.getBrightness()).to.be(-1); - }); - }); describe('#setContrast', function() { @@ -134,11 +124,6 @@ describe('ol.layer.Layer', function() { expect(layer.getContrast()).to.be(0.3); }); - it('clamps to 0', function() { - layer.setContrast(-0.7); - expect(layer.getContrast()).to.be(0); - }); - it('accepts a big positive number', function() { layer.setContrast(42); expect(layer.getContrast()).to.be(42); @@ -207,16 +192,6 @@ describe('ol.layer.Layer', function() { expect(layer.getOpacity()).to.be(0.3); }); - it('clamps to 0', function() { - layer.setOpacity(-1.5); - expect(layer.getOpacity()).to.be(0); - }); - - it('clamps to 1', function() { - layer.setOpacity(3); - expect(layer.getOpacity()).to.be(1); - }); - }); @@ -241,11 +216,6 @@ describe('ol.layer.Layer', function() { expect(layer.getSaturation()).to.be(0.3); }); - it('clamps to 0', function() { - layer.setSaturation(-0.7); - expect(layer.getSaturation()).to.be(0); - }); - it('accepts a big positive number', function() { layer.setSaturation(42); expect(layer.getSaturation()).to.be(42); From 61f89bab6e400d775425467bf83ea4824c7e1cff Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 20 Jun 2013 05:29:35 +0200 Subject: [PATCH 2/3] Prevent buttons from setting out-of-range brightness/contrast/saturation values --- examples/brightness-contrast.js | 6 +++--- examples/hue-saturation.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/brightness-contrast.js b/examples/brightness-contrast.js index e6dac0eac7..c5726f3cca 100644 --- a/examples/brightness-contrast.js +++ b/examples/brightness-contrast.js @@ -29,7 +29,7 @@ function setResetBrightnessButtonHTML() { setResetBrightnessButtonHTML(); increaseBrightness.addEventListener('click', function() { - layer.setBrightness(layer.getBrightness() + 0.125); + layer.setBrightness(Math.min(layer.getBrightness() + 0.125, 1)); setResetBrightnessButtonHTML(); }, false); resetBrightness.addEventListener('click', function() { @@ -37,7 +37,7 @@ resetBrightness.addEventListener('click', function() { setResetBrightnessButtonHTML(); }, false); decreaseBrightness.addEventListener('click', function() { - layer.setBrightness(layer.getBrightness() - 0.125); + layer.setBrightness(Math.max(layer.getBrightness() - 0.125, -1)); setResetBrightnessButtonHTML(); }, false); @@ -59,6 +59,6 @@ resetContrast.addEventListener('click', function() { setResetContrastButtonHTML(); }, false); decreaseContrast.addEventListener('click', function() { - layer.setContrast(layer.getContrast() - 0.125); + layer.setContrast(Math.max(layer.getContrast() - 0.125, 0)); setResetContrastButtonHTML(); }, false); diff --git a/examples/hue-saturation.js b/examples/hue-saturation.js index 53049a4336..a2aba69fa1 100644 --- a/examples/hue-saturation.js +++ b/examples/hue-saturation.js @@ -63,6 +63,6 @@ resetSaturation.addEventListener('click', function() { setResetSaturationButtonHTML(); }, false); decreaseSaturation.addEventListener('click', function() { - layer.setSaturation(layer.getSaturation() - 0.25); + layer.setSaturation(Math.max(layer.getSaturation() - 0.25, 0)); setResetSaturationButtonHTML(); }, false); From 11cfcda37c659e13c0d256ce641459f1f50b57fc Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 20 Jun 2013 05:32:09 +0200 Subject: [PATCH 3/3] Stop buttons from moving so much when changing values --- examples/brightness-contrast.js | 5 +++-- examples/hue-saturation.js | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/brightness-contrast.js b/examples/brightness-contrast.js index c5726f3cca..c7304bca50 100644 --- a/examples/brightness-contrast.js +++ b/examples/brightness-contrast.js @@ -24,7 +24,8 @@ var resetBrightness = document.getElementById('reset-brightness'); var decreaseBrightness = document.getElementById('decrease-brightness'); function setResetBrightnessButtonHTML() { - resetBrightness.innerHTML = 'Brightness (' + layer.getBrightness() + ')'; + resetBrightness.innerHTML = 'Brightness (' + + layer.getBrightness().toFixed(3) + ')'; } setResetBrightnessButtonHTML(); @@ -46,7 +47,7 @@ var resetContrast = document.getElementById('reset-contrast'); var decreaseContrast = document.getElementById('decrease-contrast'); function setResetContrastButtonHTML() { - resetContrast.innerHTML = 'Contrast (' + layer.getContrast() + ')'; + resetContrast.innerHTML = 'Contrast (' + layer.getContrast().toFixed(3) + ')'; } setResetContrastButtonHTML(); diff --git a/examples/hue-saturation.js b/examples/hue-saturation.js index a2aba69fa1..2d2da4a57e 100644 --- a/examples/hue-saturation.js +++ b/examples/hue-saturation.js @@ -28,7 +28,7 @@ var resetHue = document.getElementById('reset-hue'); var decreaseHue = document.getElementById('decrease-hue'); function setResetHueButtonHTML() { - resetHue.innerHTML = 'Hue (' + layer.getHue() + ')'; + resetHue.innerHTML = 'Hue (' + layer.getHue().toFixed(2) + ')'; } setResetHueButtonHTML(); @@ -50,7 +50,8 @@ var resetSaturation = document.getElementById('reset-saturation'); var decreaseSaturation = document.getElementById('decrease-saturation'); function setResetSaturationButtonHTML() { - resetSaturation.innerHTML = 'Saturation (' + layer.getSaturation() + ')'; + resetSaturation.innerHTML = 'Saturation (' + + layer.getSaturation().toFixed(2) + ')'; } setResetSaturationButtonHTML();