RGB values of visible spectrum

后端 未结 11 1719
难免孤独
难免孤独 2020-11-22 10:37

I need an algorithm or function to map each wavelength of visible range of spectrum to its equivalent RGB values. Is there any structural relation between the RGB System an

11条回答
  •  不要未来只要你来
    2020-11-22 11:14

    VBA code is derived from Approximate "RGB values for Visible Wavelengths" by Dan Bruton (astro@tamu.edu). Link to his original Fortran code: http://www.physics.sfasu.edu/astro/color/spectra.html Spectra program: http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm

    Sub Wavelength_To_RGB()
    
    'Purpose: Loop thru the wavelengths in the visible spectrum of light
    '         and output the RGB values and colors to a worksheet.
    '         Wavelength range: 380nm and 780nm
    
    Dim j As Long, CellRow As Long
    Dim R As Double, G As Double, B As Double
    Dim iR As Integer, iG As Integer, iB As Integer
    Dim WL As Double
    Dim Gamma As Double
    Dim SSS As Double
    
    
    Gamma = 0.8
    CellRow = 1
    
    For j = 380 To 780
    
      WL = j
    
      Select Case WL
    
      Case 380 To 440
          R = -(WL - 440#) / (440# - 380#)
          G = 0#
          B = 1#
      Case 440 To 490
          R = 0#
          G = ((WL - 440#) / (490# - 440#))
          B = 1#
      Case 490 To 510
          R = 0#
          G = 1#
          B = (-(WL - 510#) / (510# - 490#))
      Case 510 To 580
          R = ((WL - 510#) / (580# - 510#))
          G = 1#
          B = 0#
      Case 580 To 645
          R = 1#
          G = (-(WL - 645#) / (645# - 580#))
          B = 0#
      Case 645 To 780
          R = 1#
          G = 0#
          B = 0#
      Case Else
          R = 0#
          G = 0#
          B = 0#
      End Select
    
      'LET THE INTENSITY SSS FALL OFF NEAR THE VISION LIMITS
      If WL > 700 Then
         SSS = 0.3 + 0.7 * (780# - WL) / (780# - 700#)
      ElseIf WL < 420 Then
         SSS = 0.3 + 0.7 * (WL - 380#) / (420# - 380#)
      Else
         SSS = 1#
      End If
    
      'GAMMA ADJUST
      R = (SSS * R) ^ Gamma
      G = (SSS * G) ^ Gamma
      B = (SSS * B) ^ Gamma
    
      'Multiply by 255
      R = R * 255
      G = G * 255
      B = B * 255
    
      'Change RGB data type from Double to Integer.
      iR = CInt(R)
      iG = CInt(G)
      iB = CInt(B)
    
      'Output to worksheet
      Cells(CellRow, 1).Interior.Color = RGB(iR, iG, iB)
      Cells(CellRow, 2) = WL
      Cells(CellRow, 3) = "(" & iR & "," & iG & "," & iB & ")"
      CellRow = CellRow + 1
    
    Next j
    
    
    End Sub
    

提交回复
热议问题