Liberty BASIC Community Forum
« RGB to HSL converter »

Welcome Guest. Please Login or Register.
Nov 17th, 2017, 1:15pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: RGB to HSL converter  (Read 423 times)
Ryan Mckeown
New Member
Image


member is offline

Avatar

Judges 6:16 - 'The Lord answered, "I will be with you..."'

YIM YIM
Homepage PM

Gender: Male
Posts: 36
question RGB to HSL converter
« Thread started on: Nov 30th, 2009, 12:52pm »

Hey.

Can anyone tell me where I have went wrong here. It is probabily something stupid. I already have a HSl to RGB converter sorted but am struggling to set the reverse up. Here is the code:
Code:
'Substitute any RGB value in below. Note the output is in windows format of H = 0-240, S = 0-240 and L = 0-240
red = 160
green = 210
blue = 75

red = red / 255
green = green / 255
blue = blue / 255

if red < green and red < blue then min = red
if green < red and green < blue then min = green
if blue < green and blue < red then min = blue

if red > green and red > blue then max = red
if green > red and green > blue then max = green
if blue > green and blue > red then max = blue

del = max - min

lum = (max + min ) / 2

if max = 0 then
   hue = 0
   sat = 0
else
   if lum < 0.5 then
        sat = max / (max + min)
   else
        sat = max / (2 - max - min)
   end if

   delR = (((max - red) / 6 ) + (max / 2)) / max
   delG = (((max - green) / 6 ) + (max / 2)) / max
   delB = (((max - blue) / 6 ) + (max / 2)) / max

   if red = max then
        hue = delB - delG
   else
        if green = max then
            hue = (1 / 3) + delR - delB
        else
            hue = (2 / 3) + delG - delR
        end if
    end if

   if hue < 0 then hue = hue + 1
   if hue > 1 then hue = hue - 1
end if

hue = rounder(hue * 240)
sat = rounder(sat * 240)
lum = rounder(lum * 240)

print hue
print sat
print lum


end



function rounder(num)
    num$ = str$(num)
    if instr(num$, ".") then
        digit = val(mid$(num$, instr(num$, ".") + 1, 1))
        if digit >= 5 then
            rounder = int(num + 1)
        else
            rounder = int(num)
        end if
    else
        rounder = int(num)
    end if
end function


 


The program takes the rgb values and coverts them into the respective HSL values.

I got the formula for that here:
http://www.easyrgb.com/index.php?X=MATH&H=18#text18

It should be pointed out that you can adjust the top three variables of red, green and blue to test the program. Also, the output is the standard Windows HSL of hue = 0-240, sat = 0-240 and lum = 0-240 like you see in MS paint.

Any ideas?
« Last Edit: Nov 30th, 2009, 1:11pm by Ryan Mckeown » User IP Logged

Regards,
Ryan Mckeown
N.I., U.K.


My websites:

www.thechristiancorner.co.uk
Interested in Christianity? Ever wonder what a Christian view is on a certain contemporary topics? Go here and find answers.

www.stand-by.co.uk
A collection of all the code I have written for Just BASIC and Liberty BASIC.
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5560
question Re: RGB to HSL converter
« Reply #1 on: Dec 1st, 2009, 07:58am »

I tried following the formula on Wiki. I can get the H and the L but the S eludes me right now. Out of time but this alternative code might give you a bit more to work on.




Code:
'rgb 0-255
red = 160
green = 210
blue = 75
'find max
if max<red then max=red
if max<green then max=green
if max<blue then max=blue
'find min
min=max
if min>red then min=red
if min>green then min=green
if min>blue then min=blue
'find hue 0-360
select case
    case max=min
    H=0
    case max=red
    H=60*(green-blue)/(max-min)+360 mod 360
    case max=green
    H=60*(blue-red)/(max-min)+120
    case max=blue
    H=60*(red-green)/(max-min)+240
end select
'normalise to 0-240
H=H/360*240
'round
R=H-int(H)
Hn=int(H+(R>.5))
'find luminosity
L=(max+min)/2
'normalise to 0-240
Ln=L/255*240
'round
R=Ln-int(Ln)
Ln=int(Ln+(R>.5))
'sind saturation ----- not working yet----
if max=min then
    S=0
else
    if L <= 0.5 then
        S = (max-min) / (max+min)
    else
        S = (max-min) / (2-max-min)
    end if
end if

print red
print green
print blue
print

print Hn
print S
Print Ln

 
User IP Logged

JohnDavidson
Board Moderator

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 2480
question Re: RGB to HSL converter
« Reply #2 on: Dec 1st, 2009, 1:04pm »

I haven't had time to play with the code but this link might help.

John

edit to add:
And now I read Rod's post!

« Last Edit: Dec 1st, 2009, 1:09pm by JohnDavidson » User IP Logged

John Davidson
e-me: johnshomeport@yahoo.com
Comming Soon: http://johnshomeport.com
Did ya Libby yet? http://lblibby.com/



Ryan Mckeown
New Member
Image


member is offline

Avatar

Judges 6:16 - 'The Lord answered, "I will be with you..."'

YIM YIM
Homepage PM

Gender: Male
Posts: 36
question Re: RGB to HSL converter
« Reply #3 on: Dec 1st, 2009, 1:42pm »

Thanks all.

I didn't know there was an API for this, thats made my life just that little bit easier.

The code I used to convert HSL to RGB (just in case anyone is curious) is:
Code:
hue = 196
sat = 23
lum = 160

hue = hue / 240
sat = sat / 240
lum = lum / 240

if sat = 0 then
    red = lum * 255
    green = lum * 255
    blue = lum * 255
else
    if lum < 0.5 then
        dummy = lum * (1 + sat)
    else
        dummy = (lum + sat) - (sat * lum)
    end if

    var = (2 * lum) - dummy

    red = 255 * hue2RGB(var, dummy, hue + (1 / 3))
    green = 255 * hue2RGB(var, dummy, hue)
    blue = 255 * hue2RGB(var, dummy, hue - (1 / 3))
end if

print rounder(red)
print rounder(green)
print rounder(blue)

end

function hue2RGB(v1, v2, vH)
    if vH < 0 then vH = vH + 1
    if vH > 1 then vH = vH - 1
    if (6 * vH) < 1 then
        hue2RGB = v1 + (v2 - v1) * 6 * vH
    else
        if (2 * vH) < 1 then
            hue2RGB = v2
        else
            if (3 * vH) < 2 then
                hue2RGB = v1 + (v2 - v1) * ((2 / 3) - vH) * 6
            else
                hue2RGB = v1
            end if
        end if
    end if
end function

function rounder(num)
    num$ = str$(num)
    if instr(num$, ".") then
        digit = val(mid$(num$, instr(num$, ".") + 1, 1))
        if digit >= 5 then
            rounder = int(num + 1)
        else
            rounder = int(num)
        end if
    else
        rounder = int(num)
    end if
end function


 


But it seems MSDN may have a solution to that too. I may just scrap that code above and use the API, it's probally more acurate and much faster. Ah well...

Thanks again.
User IP Logged

Regards,
Ryan Mckeown
N.I., U.K.


My websites:

www.thechristiancorner.co.uk
Interested in Christianity? Ever wonder what a Christian view is on a certain contemporary topics? Go here and find answers.

www.stand-by.co.uk
A collection of all the code I have written for Just BASIC and Liberty BASIC.
Stefan Pendl
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Computers are like babies, you must teach them what you like them to do ...


Homepage PM

Gender: Male
Posts: 5297
question Re: RGB to HSL converter
« Reply #4 on: Dec 1st, 2009, 1:57pm »

on Dec 1st, 2009, 1:42pm, Ryan Mckeown wrote:
Thanks all.

I didn't know there was an API for this, thats made my life just that little bit easier.

---snip

But it seems MSDN may have a solution to that too. I may just scrap that code above and use the API, it's probally more acurate and much faster. Ah well...

Thanks again.


Please remember that a COLORREF is a BGR value and not a RGB value, which means the values are in the reverse order.

See the Remarks section of the COLORREF Macro definition.
User IP Logged

Stefan

Make sure to read and follow the Forum Guidelines

Liberty BASIC Pro 4.04, Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
Ryan Mckeown
New Member
Image


member is offline

Avatar

Judges 6:16 - 'The Lord answered, "I will be with you..."'

YIM YIM
Homepage PM

Gender: Male
Posts: 36
question Re: RGB to HSL converter
« Reply #5 on: Dec 2nd, 2009, 10:43am »

Ok

I will keep that in mind Stefan.

Does anyone know of an example piece of code demonstrationg the API for HSL to BGR conversion. Its just APIs and DLLs aren't my strongest subject...
User IP Logged

Regards,
Ryan Mckeown
N.I., U.K.


My websites:

www.thechristiancorner.co.uk
Interested in Christianity? Ever wonder what a Christian view is on a certain contemporary topics? Go here and find answers.

www.stand-by.co.uk
A collection of all the code I have written for Just BASIC and Liberty BASIC.
Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5560
question Re: RGB to HSL converter
« Reply #6 on: Dec 2nd, 2009, 1:23pm »

See the API thread.
User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls