P°evod RGB na HSL

Postup:
Public Type HSL
    Hue As Integer
    Saturation As Integer
    Luminance As Integer
End Type

Public Function RGBtoHSL(ByVal Red As Integer, _
                         ByVal Green As Integer, _
                         ByVal Blue As Integer) As HSL

    Dim pRed As Single
    Dim pGreen As Single
    Dim pBlue As Single
    Dim RetVal As HSL
    Dim pMax As Single
    Dim pMin As Single
    Dim pLum As Single
    Dim pSat As Single
    Dim pHue As Single
    
    pRed = Red / 255
    pGreen = Green / 255
    pBlue = Blue / 255
   
    If pRed > pGreen Then
       If pRed > pBlue Then
          pMax = pRed
       Else
          pMax = pBlue
       End If
    ElseIf pGreen > pBlue Then
        pMax = pGreen
    Else
        pMax = pBlue
    End If

    If pRed < pGreen Then
        If pRed < pBlue Then
            pMin = pRed
        Else
            pMin = pBlue
        End If
    ElseIf pGreen < pBlue Then
        pMin = pGreen
    Else
        pMin = pBlue
    End If

    pLum = (pMax + pMin) / 2
   
    If pMax = pMin Then
        pSat = 0
        pHue = 0
    Else
        If pLum < 0.5 Then
            pSat = (pMax - pMin) / (pMax + pMin)
        Else
            pSat = (pMax - pMin) / (2 - pMax - pMin)
        End If
        
        Select Case pMax!
        Case pRed
            pHue = (pGreen - pBlue) / (pMax - pMin)
        Case pGreen
            pHue = 2 + (pBlue - pRed) / (pMax - pMin)
        Case pBlue
            pHue = 4 + (pRed - pGreen) / (pMax - pMin)
        End Select
    End If

    RetVal.Hue = pHue * 239 \ 6
    If RetVal.Hue < 0 Then RetVal.Hue = RetVal.Hue + 240
    
    RetVal.Saturation = Int(pSat * 239)
    RetVal.Luminance = Int(pLum * 239)
    
    RGBtoHSL = RetVal
End Function

Zp∞t

Autor: The Bozena