Function converts wavelength of light to RGB888 and RGB565

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
Stuartk
Posts: 54
Joined: 12 Jan 2013 04:17

Function converts wavelength of light to RGB888 and RGB565

#1 Post by Stuartk » 05 Feb 2017 22:02

In my current construction of a spectrometer, I am attaching a routine I modified to convert the wavelength of light from 340-780nm, to RGB888 format, then RGB565.

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)
regards,
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

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Function converts wavelength of light to RGB888 and RGB5

#2 Post by filip » 08 Feb 2017 10:13

Hi,

Thank you for this, I'm sure users will benefit from it :)

Regards,
Filip.

Post Reply

Return to “mikroBasic PRO for PIC32 General”