Android Code Snippet [B4X]RGB to HSL Conversion

I couldn't find anything on the forum to do this so I'll leave it here in case anyone needs it in the future.

It takes Red, Green, Blue inputs and returns Hue, Saturation, and Lightness values.

The return type
B4X:
Type HSL (hue As Double , saturation As Double, lightness As Double)

Function itself

B4X:
Sub RGBToHSL(r As Double , g As Double, b As Double) As HSL
 
    Dim hsl As HSL
    Dim rgb As List

    r = r / 255
    g = g / 255
    b = b / 255

    rgb.Initialize2 (Array As Double (r, g, b))
    
    Dim cmin As Double = MathMin(rgb)
    Dim cmax As Double = MathMax(rgb)
    Dim delta As Double = cmax - cmin
    Dim h As Double = 0
    Dim s As Double = 0
    Dim l As Double = 0
    
    ' Calculate hue

    If delta = 0 Then                        'No difference
        h = 0
    
    Else If cmax = r Then                    'Red Is max
        h = (g - b) / delta Mod 6
    
    else if cmax = g Then                    'Green is max
        h = (b - r) / delta + 2
        
    Else                                    'Blue is max
        h = (r - g) / delta + 4
        
    End If

    h = Round(h * 60)
    
    If (h < 0) Then h = h + 360                'Make negative hues positive

    l = (cmax + cmin) / 2                    'Calculate lightness

    s = IIf (delta = 0, 0 , delta / (1 - Abs(2 * l - 1)))        'Calculate saturation
    
    'Multiply l And s by 100
    s = NumberFormat (s * 100, 0, 1).Replace (",", "")
    l = NumberFormat (l * 100, 0, 1).Replace (",", "")

    hsl.hue = h
    hsl.saturation = s
    hsl.lightness = l

    Return hsl
    
End Sub

Sub MathMin (l As List) As Double        'Get the min value in an unsorted list
    
    Dim x As Double = 1
    
    For Each i As Double In l
        If i < x Then x = i
    Next
    
    Return x
End Sub

Sub MathMax (l As List) As Double        'Get the max value in an unsorted list
    
    Dim x As Double = 0
    
    For Each i As Double In l
        If i > x Then x = i
    Next
    
    Return x
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Similar code from B4XColorTemplate (XUI Views):
B4X:
Public Sub ColorToHSV(clr As Int) As Object()
    Dim a As Int = Bit.And(0xff, Bit.UnsignedShiftRight(clr, 24))
    Dim r As Int = Bit.And(0xff, Bit.UnsignedShiftRight(clr, 16)) 
    Dim g As Int = Bit.And(0xff, Bit.UnsignedShiftRight(clr, 8))
    Dim b As Int = Bit.And(0xff, Bit.UnsignedShiftRight(clr, 0)) 
    Dim h, s, v As Float
    Dim cmax As Int = Max(Max(r, g), b)
    Dim cmin As Int = Min(Min(r, g), b)
    v = cmax / 255
    If cmax <> 0 Then
        s = (cmax - cmin) / cmax
    End If
    If s = 0 Then
        h = 0
    Else
        Dim rc As Float = (cmax - r) / (cmax - cmin)
        Dim gc As Float = (cmax - g) / (cmax - cmin)
        Dim bc As Float = (cmax - b) / (cmax - cmin)
        If r = cmax Then
            h = bc - gc
        Else If g = cmax Then
            h = 2 + rc - bc
        Else
            h = 4 + gc - rc
        End If
        h = h / 6
        If h < 0 Then h = h + 1
    End If
    Return
Code to convert from HSV to RGB color is available in BitmapCreator. B4XColorTemplate uses this code for the conversion:
B4X:
tempBC.SetHSV(0, 0, SelectedAlpha, hsv(0), hsv(1), hsv(2)) 'this is a single pixel BitmapCreator
Return tempBC.GetColor(0, 0)
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…