ColorScale.prototype.getColor = function (value) {
if (typeof this.normalize === 'function') {
value = this.normalize(value);
}
var lengthes = [];
var fullLength = 0;
var l;
for (var i = 0; i < this.colors.length - 1; i++) {
l = this.vectorLength(this.vectorSubtract(this.colors[i + 1], this.colors[i]));
lengthes.push(l);
fullLength += l;
}
var c = (this.maxValue - this.minValue) / fullLength;
for (i = 0; i < lengthes.length; i++) {
lengthes[i] *= c;
}
i = 0;
value -= this.minValue;
while (value - lengthes[i] >= 0) {
value -= lengthes[i];
i++;
}
var color;
if (i === this.colors.length - 1) {
color = this.vectorToNum(this.colors[i]).toString(16);
} else {
color = (this.vectorToNum(this.vectorAdd(this.colors[i], this.vectorMult(this.vectorSubtract(this.colors[i + 1], this.colors[i]), (value) / (lengthes[i]))))).toString(16);
}
while (color.length < 6) {
color = '0' + color;
}
return '#' + color;
};
|