Skip to content

Instantly share code, notes, and snippets.

@peteroome
Created March 24, 2014 20:42
Show Gist options
  • Save peteroome/9748701 to your computer and use it in GitHub Desktop.
Save peteroome/9748701 to your computer and use it in GitHub Desktop.
Generate a colour within a range based on a value
function Interpolate(start, end, steps, count) {
var s = start,
e = end,
final = s + (((e - s) / steps) * count);
return Math.floor(final);
}
function Color(_r, _g, _b) {
var r, g, b;
var setColors = function(_r, _g, _b) {
r = _r;
g = _g;
b = _b;
};
setColors(_r, _g, _b);
this.getColors = function() {
var colors = {
r: r,
g: g,
b: b
};
return colors;
};
}
function strokeColour(val){
var arrayOfVals = [10,20,30,40,50],
max = parseFloat(arrayOfVals[(arrayOfVals.length - 1)]),
min = parseFloat(arrayOfVals[0]),
average = ((max + min)/2).toFixed(),
val = ((parseFloat(val)/average)*100).toFixed(),
light_blue = new Color(127, 127, 255), // #0000fb #7f7fff
blue = new Color(198, 127, 184), // #800080 #c67fb8
red = new Color(255, 127, 127), // #ff0000 #ff7f7f
start = light_blue,
end = blue;
if (val > 50) {
start = blue,
end = red,
val = val % 51;
}
var startColors = start.getColors(),
endColors = end.getColors();
var r = Interpolate(startColors.r, endColors.r, 50, val);
var g = Interpolate(startColors.g, endColors.g, 50, val);
var b = Interpolate(startColors.b, endColors.b, 50, val);
return "rgb(" + r + "," + g + "," + b + ")";
}
strokeColour(25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment