This was part of this thread: viewtopic.php?f=160&t=60572
Calling the function returns the RGB565 value:
Example:
Code: Select all
temp = Convert_Wavelength(450) 'Converts 450nm to RGB565
temp = 0x46ff 'RGB565=0x46ff RGB888=rgb(0,70, 255)
Stuart
Code: Select all
Sub function Convert_Wavelength(dim nm_input as float) as word ' Converts wavelength of light in nm to RGB565
' This code is derived from from Academo.org
' Result is supplied as RGB565 value
Gamma = 0.80 ' The R G B 0-255 values are also available
red = 0.0
green = 0.0
blue = 0.0
if (nm_input >= 380) and (nm_input < 440) then
red = (-1 *(nm_input - 440)) / (440 - 380)
green = 0.0
blue = 1.0
Intensity = 255 'Maximum Intensity = 255
end if
if (nm_input >= 440) and (nm_input < 490) then
red = 0.0
green = (nm_input - 440) / (490 - 440)
blue = 1.0
Intensity = 255
end if
if (nm_input >= 490) and (nm_input < 510) then
red = 0.0
green = 1.0
blue = (-1 *(nm_input - 510)) / (510 - 490)
Intensity = 255
end if
if (nm_input >= 510) and (nm_input < 580) then
red = (nm_input - 510) / (580 - 510)
green = 1.0
blue = 0.0
Intensity = 255
end if
if (nm_input >= 580) and (nm_input < 645) then
red = 1.0
green =(-1*(nm_input - 645)) / (645 - 580)
blue = 0.0
Intensity = 255
end if
if (nm_input >= 645) and (nm_input < 781) then
red = 1.0
green = 0.0
blue = 0.0
Intensity = 255
end if
'Let the intensity fall off near the vision limits
factor = 0.0
if (nm_input >= 380) and (nm_input < 420) then
factor = 0.3 + ((0.7*(nm_input - 380)) / (420 - 380))
end if
if (nm_input >= 420) and (nm_input < 701) then
factor = 1.0
end if
if (nm_input >= 701) and (nm_input < 781) then
factor = 0.3 + ((0.7*(780 - nm_input)) / (780 - 700))
Intensity = 255-(2*(nm_input-701)) 'Fade Intensity over 700nm
end if
if (nm_input >= 340) and (nm_input < 380) then 'Routine for Ultraviolet color approximation as unwilling to write black.
red = 97-(380-nm_input) 'Outputs progrssion of dark purple
green = 0.0
blue = red
Intensity = 255
end if
if (nm_input >= 380) and (nm_input < 781) then ' Routine for visible light
if red <> 0 then
exp_var = red * factor
red = (Intensity * pow(exp_var, Gamma))
res = modf(red, iptr) ' res = decimal eg 0.49, iptr = whole number remainder
if res >= 0.5 then ' Round variable to whole number
red = ceil(red)
else
red = floor(red)
end if
end if
if green <> 0 then
exp_var = green * factor
green = (Intensity * pow(exp_var, Gamma))
res = modf(green, iptr) ' res = decimal eg 0.49, iptr = whole number remainder
if res >= 0.5 then ' Round variable to whole number
green = ceil(green)
else
green = floor(green)
end if
end if
if blue <> 0 then
exp_var = blue * factor
blue = (Intensity * pow(exp_var, Gamma))
res = modf(blue, iptr) ' res = decimal eg 0.49, iptr = whole number remainder
if res >= 0.5 then ' Round variable to whole number
blue = ceil(blue)
else
blue = floor(blue)
end if
end if
end if
'rgb is now in the RGB888 format
'Convert to RGB565
R = Red 'Storage variable otherwise error in compiling
B = Blue 'Storage variable otherwise error in compiling
G = Green 'Storage variable otherwise error in compiling
Result=(((R and 0xF8) << 8) or ((G and 0xFC) << 3) or (B >> 3)) 'Convert RGB888 to RGB565
end sub