1

Cleanup of range

This commit is contained in:
Alex Yatskov 2014-11-08 11:48:10 +09:00
parent 8b7759a82a
commit 74b9331044

View File

@ -31,41 +31,41 @@
// Range // Range
// //
function Range(start, end) { function Range(min, max) {
this.start = Math.min(start, end); this.min = Math.min(min, max);
this.end = Math.max(start, end); this.max = Math.max(min, max);
this.contains = function(value) { this.contains = function(value) {
return value >= this.start && value <= this.end; return value >= this.min && value <= this.max;
}; };
this.length = function() { this.length = function() {
return this.end - this.start; return this.max - this.min;
}; };
this.clamp = function(value) { this.clamp = function(value) {
if (value < this.start) { if (value < this.min) {
return this.start; return this.min;
} }
if (value > this.end) { if (value > this.max) {
return this.end; return this.max;
} }
return value; return value;
}; };
this.include = function(range) { this.include = function(range) {
this.start = Math.min(this.start, range.start); this.min = Math.min(this.min, range.min);
this.end = Math.max(this.end, range.end); this.max = Math.max(this.max, range.max);
}; };
this.offset = function(value) { this.offset = function(value) {
return (value - this.start) / this.length(); return (value - this.min) / this.length();
}; };
this.project = function(value) { this.project = function(value) {
return this.start + this.length() * value; return this.min + this.length() * value;
}; };
} }
@ -125,7 +125,7 @@
// indicator // indicator
var range = computeIndicatorRange(); var range = computeIndicatorRange();
_elements.indicator = _canvas.rect( _elements.indicator = _canvas.rect(
_tickSize, range.start, _width - (_densitySize + _tickSize), (range.end - range.start) _tickSize, range.min, _width - (_densitySize + _tickSize), (range.max - range.min)
).attr({cursor: 'crosshair', fill: computeIndicatorColor()}).click(clicked); ).attr({cursor: 'crosshair', fill: computeIndicatorColor()}).click(clicked);
// tick // tick
@ -159,8 +159,8 @@
var range = computeIndicatorRange(); var range = computeIndicatorRange();
_elements.indicator.attr({ _elements.indicator.attr({
y: range.start, y: range.min,
height: range.end - range.start, height: range.max - range.min,
fill: computeIndicatorColor() fill: computeIndicatorColor()
}); });
} }
@ -174,7 +174,7 @@
var colorPercent = 0; var colorPercent = 0;
if (_scale.length() > 0) { if (_scale.length() > 0) {
colorPercent = Math.max(0, groupSize - _scale.start) / _scale.length(); colorPercent = Math.max(0, groupSize - _scale.min) / _scale.length();
} }
var colorByte = 0xff - Math.min(0xff, Math.round(0xff * colorPercent)); var colorByte = 0xff - Math.min(0xff, Math.round(0xff * colorPercent));
@ -195,7 +195,7 @@
var hintGroups = []; var hintGroups = [];
for (var i = 0; i < _steps; ++i) { for (var i = 0; i < _steps; ++i) {
var stepMax = _range.end - stepSize * i; var stepMax = _range.max - stepSize * i;
var stepMin = stepMax - stepSize; var stepMin = stepMax - stepSize;
var hintCount = 0; var hintCount = 0;
@ -224,8 +224,8 @@
function valueColorAdjust(color, offset) { function valueColorAdjust(color, offset) {
var colorObj = tinycolor(color); var colorObj = tinycolor(color);
var rangeEnd = _data.value >= 0.0 ? _range.end : _range.start; var rangeEnd = _data.value >= 0.0 ? _range.max : _range.min;
var rangeMid = (_range.start + _range.end) / 2.0; var rangeMid = (_range.min + _range.max) / 2.0;
var rangeRat = (_data.value - rangeMid) / (rangeEnd - rangeMid); var rangeRat = (_data.value - rangeMid) / (rangeEnd - rangeMid);
var desatVal = Math.max(0.0, 1.0 - rangeRat + offset) * 100.0; var desatVal = Math.max(0.0, 1.0 - rangeRat + offset) * 100.0;
return colorObj.desaturate(desatVal).toHexString(); return colorObj.desaturate(desatVal).toHexString();