Skip to content

Instantly share code, notes, and snippets.

@jsvnm
Created November 15, 2012 15:42
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 jsvnm/4079258 to your computer and use it in GitHub Desktop.
Save jsvnm/4079258 to your computer and use it in GitHub Desktop.
get rgb value of wavelength
# from http://www.physics.sfasu.edu/astro/color/spectra.html
def rgbFromWavelength(wl, gamma=0.8)
rgb=case wl
when (380..439); [-1.0*(wl-440.0)/(440.0-380.0), 0.0, 1.0]
when (440..489); [0.0, (wl-440.0)/(490.0-440.0), 1.0]
when (490..509); [0.0, 1.0, -1.0*(wl-510.0)/(510.0-490.0)]
when (510..579); [(wl-510.0)/(580.0-510.0), 1.0, 0.0]
when (580..644); [1.0, -1.0*(wl-645.0)/(645-580.0), 0.0]
when (645..780); [1.0, 0.0, 0.0];
end
sss=(wl>700 && 0.3+0.7*(780.0-wl)/(780.0-700.0) ||
wl<420 && 0.3+0.7*(wl-380.0)/(420.0-380.0) ||
1)
rgb.map!{|c| (sss*c)**gamma}
def rgb.to_bytes; self.map{|c| (0xff*c)}; end
def rgb.to_s; ('#'+'%02x'*3)%self.to_bytes; end
rgb
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment