Created
January 8, 2013 02:02
-
-
Save mattneary/4480436 to your computer and use it in GitHub Desktop.
Wavelength-To-RGB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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