Skip to content

Instantly share code, notes, and snippets.

@fredmaggiowski
Created September 7, 2016 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredmaggiowski/269f027af7c8b23bd963e40fd739cb8e to your computer and use it in GitHub Desktop.
Save fredmaggiowski/269f027af7c8b23bd963e40fd739cb8e to your computer and use it in GitHub Desktop.
A basic function to transform a float into a RGB value (from green to red)
function f2R(f) {
var r = 255
var g = 255
var b = 0
var x = Math.abs(f - 0.5);
var i = Math.ceil(600 * (0.5 - x));
// range between green and yellow
if (f < 0.4) {
r = i
}
// range between yellow and red
if (f >= 0.4) {
g = i
}
return "rgb(" + r + "," + g + "," + b + ")";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment