Skip to content

Instantly share code, notes, and snippets.

@derappelt
Last active March 20, 2018 17:38
Show Gist options
  • Save derappelt/35619334108b61a3d33a5d31f8fff27f to your computer and use it in GitHub Desktop.
Save derappelt/35619334108b61a3d33a5d31f8fff27f to your computer and use it in GitHub Desktop.
var colorStops = [
{ p: 0.1, c: [244, 67, 54] },
{ p: 0.5, c: [255, 236, 59] },
{ p: 1.0, c: [76, 175, 79] }
];
var getColorForPercentage = function (p, stops) {
p = (p < 0) ? 0 : (p > 1) ? 1 : p;
for (var i = 1; i < stops.length - 1; i++) {
if (p < stops[i].p) {
break;
}
}
var l = stops[i - 1];
var u = stops[i];
var r = u.p - l.p;
var rp = (p - l.p) / r;
return [
Math.floor(l.c[0] * (1 - rp) + u.c[0] * rp),
Math.floor(l.c[1] * (1 - rp) + u.c[1] * rp),
Math.floor(l.c[2] * (1 - rp) + u.c[2] * rp)
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment