Merge pull request #798 from twpayne/fix-layer-setters

Don't attempt to constrain values in setters
This commit is contained in:
Tom Payne
2013-06-20 09:52:40 -07:00
4 changed files with 21 additions and 66 deletions

View File

@@ -24,12 +24,13 @@ 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();
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 +38,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);
@@ -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();
@@ -59,6 +60,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);

View File

@@ -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();
@@ -63,6 +64,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);

View File

@@ -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,

View File

@@ -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);