Skip to content

Instantly share code, notes, and snippets.

@mattneary
Created January 8, 2013 02:02
Show Gist options
  • Save mattneary/4480436 to your computer and use it in GitHub Desktop.
Save mattneary/4480436 to your computer and use it in GitHub Desktop.
Wavelength-To-RGB
var color = function(wavelength) {
var bounds = [380, 440, 490, 510, 580, 645, 780];
var pairs = ["BR", "BG", "GB", "GR", "RG", "R"]
var colorGen = function(combo, borders, wl) {
var RGB = {R:0,G:0,B:0};
RGB[combo[0]] = 255;
RGB[combo[1]] = 255 * (borders[1] - wl)/(borders[1] - borders[0]);
return RGB;
};
var residuals = bounds.map(function(bound){return bound-wavelength}).map(Math.abs);
var floorIndex = residuals.indexOf(Math.min.apply(Math, residuals));
var borders = [ bounds[floorIndex], bounds[floorIndex+1] ]; // [floor, ceiling]
return colorGen(pairs[floorIndex].split(''), borders, wavelength);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment