Module:Sandbox/AlexNB/nmColor

This is an old revision of this page, as edited by AlexNB (talk | contribs) at 12:38, 21 October 2013. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}

local redvalue = 0
local greenvalue = 0
local bluevalue = 0
 
local function nm2RGB(wavelength)
-- code based on an algorithm from Dan Bruton's Color Science Page: http://www.physics.sfasu.edu/astro/color/spectra.html
-- calculating RGB color components
    if (wavelength >= 380) and (wavelength < 440) then
        redvalue = (440 - wavelength) / 90
        greenvalue = 0
        bluevalue = 1
    elseif (wavelength >= 440) and (wavelength < 490) then
        redvalue = 0
        greenvalue = (wavelength - 440) / 50
        bluevalue = 1
    elseif (wavelength >= 490) and (wavelength < 510) then
        redvalue = 0
        greenvalue = 1
        bluevalue = (510 - wavelength) / 20
    elseif (wavelength >= 510) and (wavelength < 580) then
        redvalue = (wavelength - 510) / 70
        greenvalue = 1
        bluevalue = 0
    elseif (wavelength >= 580) and (wavelength < 645) then
        redvalue = 1
        greenvalue = (645 - wavelength) / 65
        bluevalue = 0
    elseif (wavelength >= 645) and (wavelength < 780) then
        redvalue = 1
        greenvalue = 0
        bluevalue = 0
    end
-- calculating intensity correction factor
    if (wavelength >= 380) and (wavelength < 420) then
        intensityfactor = 0.3 + 0.7*(wavelength - 350) / 70
    elseif (wavelength >= 420) and (wavelength <= 700) then
        intensityfactor = 1
    elseif (wavelength > 700) and (wavelength <= 780) then
        intensityfactor = 0.3 + 0.7*(780 - wavelength) / 80
    else
        intensityfactor = 0
    end
    redvalue = redvalue * intensityfactor
    greenvalue = greenvalue * intensityfactor
    bluevalue = bluevalue * intensityfactor
end

function p.emission(frame)
-- this function returns the string "#RRGGBB" with approximate RGB value for a wavelength passed as a first argument
    local wavelength = tonumber(frame.args[1])
    nm2RGB(wavelength)
    local result='#' .. string.format("%.2X%.2X%.2X", 255*redvalue, 255*greenvalue, 255*bluevalue) 
    return result
end

function p.absorption(frame)
-- this function returns the string "#RRGGBB" with approximate RGB value for a complementary color (reflection) to a color, defined as a wavelength passed as a first argument
-- The following formula is used for the complimentary color: {0xFF - RR, 0xFF - (RR + BB)/2, 0xFF - BB}
    local wavelength = tonumber(frame.args[1])
    nm2RGB(wavelength)
--    local result='#' .. string.format("%.2X%.2X%.2X", 255*(1-redvalue), 255*(1-greenvalue), 255*(1-bluevalue))
--    local result='#' .. string.format("%.2X%.2X%.2X", 255*(1-redvalue), 255*(1-(redvalue+bluevalue)/2), 255*(1-bluevalue))
    local result='#' .. string.format("%.2X%.2X%.2X", 255*(1-0.5*redvalue^2), 255*(1-0.5*greenvalue^2), 255*(1-0.5*bluevalue^2))
    return result
end

return p