Overflow

DR00ME

Well-known member
Joined
Feb 6, 2004
Messages
169
Location
Finland
Code:
Public Shared Sub HLSToRGB(ByVal inputH As Short, ByVal inputS As Single, ByVal inputL As Single)

            Dim rR As Single = 0
            Dim rG As Single = 0
            Dim rB As Single = 0
            Dim Min As Single, Max As Single
            inputH = inputH / 60

            If inputS = 0 Then
                 Achromatic case:
                rR = inputL
                rG = inputL
                rB = inputL
            Else
                 Chromatic case:
                If inputL <= 0.5 Then
                     Get Min value:
                    Min = inputL * (1 - inputS)
                Else
                     Get Min value:
                    Min = inputL - inputS * (1 - inputL)
                End If
                 Get the Max value:
                Max = 2 * inputL - Min

                 Now depending on sector we can evaluate the h,l,s:
                If (inputH < 1) Then
                    rR = Max
                    If (inputH < 0) Then
                        rG = Min
                        rB = rG - inputH * (Max - Min)
                    Else
                        rB = Min
                        rG = inputH * (Max - Min) + rB
                    End If
                ElseIf (inputH < 3) Then
                    rG = Max
                    If (inputH < 2) Then
                        rB = Min
                        rR = rB - (inputH - 2) * (Max - Min)
                    Else
                        rR = Min
                        rB = (inputH - 2) * (Max - Min) + rR
                    End If
                Else
                    rB = Max
                    If (inputH < 4) Then
                        rR = Min
                        rG = rR - (inputH - 4) * (Max - Min)
                    Else
                        rG = Min
                        rR = (inputH - 4) * (Max - Min) + rG
                    End If

                End If

            End If
            AIvision.cR = rR * 255
            AIvision.cG = rG * 255
            AIvision.cB = rB * 255


all right... AIvision.cR = rR * 255 is overflowing because rR is getting over 1 when inputH is over around 340 (also depends on the other input values)... does anyone know why ? btw. this code is HSL to RGB converter... lol hope you are good at math.

AIvision class output values (RGB) cR, cG and rB are type of Byte. And after doing a bit more of researching I found out that cR got 288 and it should be between 0-255.


Anyway the overflowing happened e.g. with these input values:
346,8 ; 0,4464286 ; 0,4392157
 
Last edited by a moderator:
Exactly... but it is overflowing because there is something dodgy in the code... after chaging the inputH / 60 to inputH / 68 it works even with inputH value of 358 (notice the max value of hue is 360).... but then again now I cant know if the HSL to RGB conversion is accurate...
 
Is there a reason why inputH, inputS and inputL are declared single? I was always under the impression that these are integer values.
 
Yes, if you get .hue .saturation and .brightness(luminance) from some pixel using vb.net... you get .hue as 0-360 (short in code)(sometimes talked as degrees because the colors are in a colorcircle) .saturation = 0.0000000-1.0000000 and same with brightness....I think there is some bug in the code.... but after checking one professional material (?) this is the official conversion formula (not in proper vb.net format)


Code:
 if ( S == 0 )                       //HSL values = From 0 to 1
{
   R = L * 255                      //RGB results = From 0 to 255
   G = L * 255
   B = L * 255
}
else
{
   if ( L < 0.5 ) var_2 = L * ( 1 + S )
   else           var_2 = ( L + S ) - ( S * L )

   var_1 = 2 * L - var_2

   R = 255 * Hue_2_RGB( var_1, var_2, H + ( 1 / 3 ) ) 
   G = 255 * Hue_2_RGB( var_1, var_2, H )
   B = 255 * Hue_2_RGB( var_1, var_2, H - ( 1 / 3 ) )
} 


--------------------------------------------------------------------------------

Hue_2_RGB( v1, v2, vH )             //Function Hue_2_RGB
{
   if ( vH < 0 ) vH += 1
   if ( vH > 1 ) vH -= 1
   if ( ( 6 * vH ) < 1 ) return ( v1 + ( v2 - v1 ) * 6 * vH )
   if ( ( 2 * vH ) < 1 ) return ( v2 )
   if ( ( 3 * vH ) < 2 ) return ( v1 + ( v2 - v1 ) * ( ( 2 / 3 ) - vH ) * 6 )
   return ( v1 )
}


I would be really glad if someone converted it to vb.net format :) sure Im able to to it but function 2 concerns me.. there are 4 return cases .. but I just wonder why there is return v1 in the end... like if one of the if cases are true it returns a value and then in the end it returns a value AGAIN ? eh... confusing...

or how does return function work in c++ or whatever that language is (or just pseudo?)... if it has returned a value does it exit the function ?
 
Last edited by a moderator:
Now I made the conversion to vb.net... im getting wrong colors :)

[Vb] Public Shared Sub HLSToRGB(ByVal inputH As Short, ByVal inputS As Single, ByVal inputL As Single)


Dim rR As Single = 0
Dim rG As Single = 0
Dim rB As Single = 0
Dim Min As Single, Max As Single


If inputS = 0 Then
rR = inputL * 255
rG = inputL * 255
rB = inputL * 255
Else
If inputL < 0.5 Then
Min = inputL * (1 + inputS)
Else
Min = (inputL + inputS) - (inputS * inputL)
End If

Max = 2 * inputL - Min

rR = 255 * Hue_To_RGB(Max, Min, (inputH + (1 / 3)))
rG = 255 * Hue_To_RGB(Max, Min, inputH)
rB = 255 * Hue_To_RGB(Max, Min, (inputH - (1 / 3)))

End If

AIvision.cR = rR
AIvision.cG = rG
AIvision.cB = rB
End Sub

Private Shared Function Hue_To_RGB(ByVal inputMax As Single, ByVal inputMin As Single, ByVal inputHue As Single) As Single

If inputHue < 0 Then
inputHue += 1
End If

If (inputHue > 1) Then
inputHue -= 1
End If

If ((6 * inputHue) < 1) Then
Return (inputMax + (inputMin - inputMax) * 6 * inputHue)
End If

If ((2 * inputHue) < 1) Then
Return (inputMin)
End If

If ((3 * inputHue) < 2) Then
Return (inputMax + (inputMin - inputMax) * ((2 / 3) - inputHue) * 6)
End If

Return (inputMax)

End Function[/code]
 
Last edited by a moderator:
I red them both but nothing useful there....2nd was one kind of conversion which is non-standard = you cant use it to reverse the vb.net RGB to HSL because it is using integer values for HSL. damn I dont know what im doing wrong.... It is still showing wrong colors :(

this is what I got so far:

Code:
    Public Class HSL_To_RGB

        Public Shared Sub HLSToRGB(ByVal inputH As Short, ByVal inputS As Single, ByVal inputL As Single)


            Dim rR As Single = 0
            Dim rG As Single = 0
            Dim rB As Single = 0
            Dim Min As Single, Max As Single

            If inputL = 0 Then luminance = 0, means basically that brightness is 0.
                rR = 0
                rG = 0
                rB = 0
            Else
                If inputS = 0 Then When saturation is 0, color is undefined (gray,black,white)
                    rR = CByte(inputL * 255)
                    rG = CByte(inputL * 255)
                    rB = CByte(inputL * 255)
                Else
                    If inputL <= 0.5 Then
                        Min = inputL * (1 + inputS)
                    Else
                        Min = inputL + inputS - (inputS * inputL)
                    End If

                    Max = 2 * inputL - Min

                    rR = 255 * Hue_To_RGB(Max, Min, (inputH + (1 / 3)))
                    rG = 255 * Hue_To_RGB(Max, Min, inputH)
                    rB = 255 * Hue_To_RGB(Max, Min, (inputH - (1 / 3)))

                End If
            End If

            AIvision.cR = CByte(rR)
            AIvision.cG = CByte(rG)
            AIvision.cB = CByte(rB)

        End Sub

        Private Shared Function Hue_To_RGB(ByVal inputMax As Single, ByVal inputMin As Single, ByVal inputHue As Single) As Single

            If inputHue < 0 Then
                inputHue += 1
            End If

            If (inputHue > 1) Then
                inputHue -= 1
            End If

            If ((6 * inputHue) < 1) Then
                Return (inputMax + (inputMin - inputMax) * 6 * inputHue)
            End If

            If ((2 * inputHue) < 1) Then
                Return (inputMin)
            End If

            If ((3 * inputHue) < 2) Then
                Return (inputMax + (inputMin - inputMax) * ((2 / 3) - inputHue) * 6)
            End If

            Return (inputMax)

        End Function
 
Last edited by a moderator:
Another version of it I made:

Code:
        Public Shared Sub HLSToRGB(ByVal inputH As Short, ByVal inputS As Single, ByVal inputL As Single)

            Dim rR As Single = 0
            Dim rG As Single = 0
            Dim rB As Single = 0
            Dim Min As Single, Max As Single
            inputH = inputH / 360.0

            If inputL = 0 Then
                rR = 0
                rG = 0
                rB = 0
            Else
                If inputS = 0 Then
                    rR = CByte(inputL * 255)
                    rG = CByte(inputL * 255)
                    rB = CByte(inputL * 255)
                Else
                    If inputL <= 0.5 Then
                        Min = inputL * (1.0 + inputS)
                    Else
                        Min = inputL + inputS - (inputS * inputL)
                    End If

                    Max = 2 * inputL - Min

                    Dim t3() As Single = {(inputH + 1.0 / 3.0), inputH, (inputH - 1.0 / 3.0)}
                    Dim clr() As Single = {0, 0, 0}
                    Dim i As Integer

                    For i = 0 To 2

                        If (t3(i) < 0) Then
                            t3(i) += 1.0
                        End If

                        If (t3(i) > 1) Then
                            t3(i) -= 1.0
                        End If

                        If ((6.0 * t3(i)) < 1.0) Then
                            clr(i) = Max + (Min - Max) * t3(i) * 6.0
                        ElseIf (2.0 * t3(i) < 1.0) Then
                            clr(i) = Min
                        ElseIf ((3.0 * t3(i)) < 2.0) Then
                            clr(i) = (Max + (Min - Max) * ((2.0 / 3.0) - t3(i)) * 6.0)
                        Else
                            clr(i) = Max
                        End If
                    Next i

                    rR = clr(0) * 255
                    rG = clr(1) * 255
                    rB = clr(2) * 255

                End If
            End If

            AIvision.cR = CByte(rR)
            AIvision.cG = CByte(rG)
            AIvision.cB = CByte(rB)

        End Sub

im getting gray colors... what in earth is wrong with it...
 
lol now I found a new kind of conversion method:

Code:
HSL to RGB : 

First of all normalise H in the range of 0-360. Here there are 3 cases to be considered. 

0<H<120 : 

r = i*{1+[(s*CosH)/Cos(60-H)]} 

b = i*(1 - s) 

g = 3i*{i-[(r+b)/3*i]} 

120<H<240 : 

g = i*{1+[(s*Cos(H-120))/Cos(180-H)]} 

r = i*(1 - s) 

b = 3i*{i-[(r+g)/3*i]} 

240<H<360 : 

b = i*{1+[(s*Cos(H-240))/Cos(300-H)]} 

g = i*(1 - s) 

r = 3i*{i-[(g+b)/3*i]}
 
In the second one, I noticed this line:
Code:
Dim t3() As Single = {(inputH + 1.0 / 3.0), inputH, (inputH - 1.0 / 3.0)}
I dont know if you are aware but division is handled before the addition, which means you are adding 0.3 to inputH, an Integer, and the 0.3 will be rounded to a 0 for integer addition. :)
Well, a short is a short integer, so it is still an Integer.
 
What would I do without you iceplug!!!!!!!!!!!!!!!!!!!!?!?!?!?

you just saved my day, now im able to go to sleep it is 3:15am and I got school at 7am lol... it got so stressing man, I mean ffs I searched google and clicked every HSL to RGB link ...just saw same old bull there...made 3-4 diff version of the code until now... YES the input value is short 0 - 360 but in the sub proggy it doesnt work as you stated! so I had to change the input type to single...and WOOOHOO no more overflowing or other problems... I was so frustrated I even tried the bloody ColoHSLtoRGB API function....

Public Declare Function ColorHLSToRGB Lib "SHLWAPI.DLL" (ByVal wHue As Integer, ByVal wLuminance As Integer, ByVal wSaturation As Integer) As Long

that bloody function is using HUE, SATURATION and LUMINANCE values of 0-240 ...and as you can see those are damn integers..... so if i get pixelvalues with vb.net .gethue .getsaturation .getbrightness i get values 0-360, 0.0000000 - 1.0000000, 0.0000000 - 1.0000000 now to insert them to that bloody function you have to multiply sat and brightness with 240..... and convert them to int... and after that you get bloody LONG value out of the Api-funciton.... and to get r ,g and b out of it you have to do this bloody operation:

dim blaa as long

blaa = ColorHLSToRGB(240,240,240)

nRed = blaa And 255
nGreen = (blaa / 256) And 255
nBlue = (blaa / 65536) And 255

hooray now you got inaccurate R,G,B values and they correspond hue sat bri of 0-240 0-240 0-240 and you cant compare them with vb.net .gethue .getsaturation getbrightness just because you had to convert the values to non-standard BS 0-240 which is btw used in bloody ms-paint! one other funny thing in vb.net is that you can e.g. lblColor.backcolor = Color.FromArgb(cR, cG, cB) but not from hue sat bri.

P.S. ICEPLUG rox! :D
 
Last edited by a moderator:
Back
Top