C Formula That Converts HSL to RGB()

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

C Formula That Converts HSL to RGB()

Post by blueb »

I found a C programming formula that converts HSL to RGB

Unfortunately I need a C programmer to decipher this..

Code: Select all

//H, S and L input range = 0 ÷ 1.0
//R, G and B output range = 0 ÷ 255

if ( S == 0 )
{

   R = L * 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 )
}

Any help will be appreciated. :)
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
Fig
Enthusiast
Enthusiast
Posts: 351
Joined: Thu Apr 30, 2009 5:23 pm
Location: Côtes d'Azur, France

Re: C Formula That Converts HSL to RGB()

Post by Fig »

There are 2 methods to program bugless.
But only the third works fine.

Win10, Pb x64 5.71 LTS
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 536
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: C Formula That Converts HSL to RGB()

Post by BasicallyPure »

Code: Select all

EnableExplicit

Declare.l HSL_to_RGB(H.f, S.f, L.f)
Declare.f Hue_2_RGB(v1.f, v2.f, vH.f)

Procedure.l HSL_to_RGB(H.f, S.f, L.f)
   ; input variables
      ; (H)ue        0 --> 1.0
      ; (S)aturation 0 --> 1.0
      ; (L)ightness  0 --> 1.0
   
   ; returns standard PureBasic BGR value
   
   Protected.i R, G, B
   Protected.f var_1, var_2
   
   If S = 0
      R = L * 255
      G = L * 255
      B = L * 255
   Else
      If L < 0.5
         var_2 = L * (1 + S)
      Else
         var_2 = (L + S) - (S * L)
      EndIf
         
      var_1 = 2 * L - var_2
      
      R = 255 * Hue_2_RGB(var_1, var_2, H + (1.0 / 3.0))
      G = 255 * Hue_2_RGB(var_1, var_2, H)
      B = 255 * Hue_2_RGB(var_1, var_2, H - (1.0 / 3.0))
   EndIf
   
   ProcedureReturn (R | G<<8 | B<<16)
EndProcedure

Procedure.f Hue_2_RGB(v1.f, v2.f, vH.f)
   If vH < 0 : vH + 1 : EndIf
   If vH > 1 : vH - 1 : EndIf
   If (6 * vH) < 1 : ProcedureReturn (v1 + (v2 - v1) * 6 * vH) : EndIf
   If (2 * vH) < 1 : ProcedureReturn v2 : EndIf
   If (3 * vh) < 2 : ProcedureReturn (v1 + (v2 - v1) * ((2.0 / 3.0) - vH) * 6) : EndIf
   
   ProcedureReturn v1
EndProcedure


; example
; set arbitrary values for H, S, and L.
Define.f H = 0.097
Define.f S = 0.16
Define.f L = 0.87

Define color.i = HSL_to_RGB(H, S, L)

Debug "red   = " + Red(color)
Debug "green = " + Green(color)
Debug "blue  = " + Blue(color)
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
blueb
Addict
Addict
Posts: 1044
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Re: C Formula That Converts HSL to RGB()

Post by blueb »

Thanks fellows,

Fig.. Looks like an excellent example by Froggerprogger (funny it didn't come up in my search)

BasicallyPure... this helps understand C a little.
I wan't sure how C handled the 'Return' command.
- It was too lonely at the top.

System : PB 6.10 LTS (x64) and Win Pro 11 (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: C Formula That Converts HSL to RGB()

Post by Dude »

What about RGB to HSL now? :) For example, here's Paint Shop Pro's color picker. If I change any of the R,G,B values, then the corresponding H,S,L values are shown. How can I do that in PureBasic?

Image
JHPJHP
Addict
Addict
Posts: 2129
Joined: Sat Oct 09, 2010 3:47 am
Contact:

Re: C Formula That Converts HSL to RGB()

Post by JHPJHP »

Hi Dude,

For some color conversion algorithms (RGB to ...), see Spider Web Drawing.
- ConvertYUV
- ConvertYIQ
- ConvertXYZ
- ConvertHunterLAB
- ConvertCIELAB
- ConvertCIELUV
- ConvertHSV
- ConvertHLS
- ConvertGrayscale
- ConvertSepia

NB*: Use the context menu [ Set Alternate Color Space ] to execute the algorithms.
Last edited by JHPJHP on Fri Mar 23, 2018 8:58 pm, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 6874
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: C Formula That Converts HSL to RGB()

Post by infratec »

As a first start:

Code: Select all

Enumeration
  #RedRGBText
  #RedRGBSpin
  #RedRGBCanvas
  #GreenRGBText
  #GreenRGBSpin
  #GreenRGBCanvas
  #BlueRGBText
  #BlueRGBSpin
  #BlueRGBCanvas
  
  #LinkCheck
  
  
  #HueText
  #HueSpin
  #HueCanvas
  #SatText
  #SatSpin
  #SatCanvas
  #LightText
  #LightSpin
  #LightCanvas
  
  
  #HTMLText
  #HTMLString
EndEnumeration



Structure HSLStructure
  Hue.i
  Saturation.i
  Light.i
EndStructure


Procedure.f Max3F(a.f, b.f, c.f)
  If a > b
    If a > c
      ProcedureReturn a
    Else
      ProcedureReturn c
    EndIf
  Else
    If b > c
      ProcedureReturn b
    Else
      ProcedureReturn c
    EndIf
  EndIf
EndProcedure


Procedure.f Min3F(a.f, b.f, c.f)
  If a < b
    If a < c
      ProcedureReturn a
    Else
      ProcedureReturn c
    EndIf
  Else
    If b < c
      ProcedureReturn b
    Else
      ProcedureReturn c
    EndIf
  EndIf
EndProcedure


Procedure RGB2HSL(RedRGB, GreenRGB, BlueRGB, *HSL.HSLStructure) ; converts RGB-color *c to HSL and returns *c. No check if RGB is made!
  
  Protected r.f, g.f, b.f, max.f, min.f, delta.f, hue.f, saturation.f, light.f
  
  r = RedRGB / 255
  g = GreenRGB / 255
  b = BlueRGB / 255
 
  max = Max3F(r,g,b)
  min = Min3F(r,g,b)
  delta = max - min
  
  ; get lightness
  light = (max + min) / 2.0
  
  If delta <> 0.0
    
    ; get saturation
    If light <= 0.5
      saturation = delta/(max+min)
    Else
      saturation = delta/(2-max-min)
    EndIf
    
   
    ; get hue
    If r = max
      hue = (g-b)/delta
      If g < b
        hue + 6.0
      EndIf
    ElseIf g = max
      hue = (b-r)/delta + 2.0
    ElseIf b = max
      hue = (r-g)/delta + 4.0
    EndIf
   
    *HSL\Hue = hue * 60.0
   
    If *HSL\Hue < 0.0
      *HSL\Hue + 360.0
    EndIf
    
    
  Else
    ; all colors have the same value
    *HSL\Saturation = 0
    *HSL\Hue = 0 ; *c\h is even undefined
  EndIf
  
  *HSL\Saturation = saturation * 100
  *HSL\Light = light * 100
  
EndProcedure





Procedure CalcColors()
  
  Protected.i RedRGB, GreenRGB, BlueRGB
  Protected HSL.HSLStructure
  
  
  RedRGB = GetGadgetState(#RedRGBSpin)
  SetGadgetColor(#RedRGBCanvas, #PB_Gadget_BackColor, RGB(RedRGB, 0, 0))
  
  GreenRGB = GetGadgetState(#GreenRGBSpin)
  SetGadgetColor(#GreenRGBCanvas, #PB_Gadget_BackColor, RGB(0, GreenRGB, 0))
  
  BlueRGB = GetGadgetState(#BlueRGBSpin)
  SetGadgetColor(#BlueRGBCanvas, #PB_Gadget_BackColor, RGB(0, 0, BlueRGB))
  
  
  RGB2HSL(RedRGB, GreenRGB, BlueRGB, @HSL)
  
  SetGadgetState(#HueSpin, HSL\Hue)
  SetGadgetState(#SatSpin, HSL\Saturation)
  SetGadgetState(#LightSpin, HSL\Light)
  
  Debug HSL\Hue
  Debug HSL\Saturation
  Debug HSL\Light
  
  
  SetGadgetText(#HTMLString, "#" + RSet(Hex(RedRGB), 2, "0") + RSet(Hex(GreenRGB), 2, "0") + RSet(Hex(BlueRGB), 2, "0"))
  
EndProcedure



OpenWindow(0, 0, 0, 300, 200, "Current color", #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)


TextGadget(#RedRGBText, 10, 10, 40, 20, "Red:", #PB_Text_Right)
SpinGadget(#RedRGBSpin, 60, 10, 50, 20, 0, 255, #PB_Spin_Numeric)
SetGadgetState(#RedRGBSpin, 0)
TextGadget(#RedRGBCanvas, 60, 30, 50, 4, "")
DisableGadget(#RedRGBCanvas, #True)

TextGadget(#GreenRGBText, 10, 50, 40, 20, "Green:", #PB_Text_Right)
SpinGadget(#GreenRGBSpin, 60, 50, 50, 20, 0, 255, #PB_Spin_Numeric)
SetGadgetState(#GreenRGBSpin, 0)
TextGadget(#GreenRGBCanvas, 60, 70, 50, 4, "")
DisableGadget(#GreenRGBCanvas, #True)

TextGadget(#BlueRGBText, 10, 90, 40, 20, "Blue:", #PB_Text_Right)
SpinGadget(#BlueRGBSpin, 60, 90, 50, 20, 0, 255, #PB_Spin_Numeric)
SetGadgetState(#BlueRGBSpin, 0)
TextGadget(#BlueRGBCanvas, 60, 110, 50, 4, "")
DisableGadget(#BlueRGBCanvas, #True)

CheckBoxGadget(#LinkCheck, 10, 130, 70, 20, "Link colors")


TextGadget(#HueText, 130, 10, 40, 20, "Hue:", #PB_Text_Right)
SpinGadget(#HueSpin, 180, 10, 50, 20, 0, 360, #PB_Spin_Numeric)
SetGadgetState(#HueSpin, 0)
TextGadget(#HueCanvas, 180, 30, 50, 4, "")
DisableGadget(#HueCanvas, #True)

TextGadget(#SatText, 130, 50, 40, 20, "Sat:", #PB_Text_Right)
SpinGadget(#SatSpin, 180, 50, 50, 20, 0, 100, #PB_Spin_Numeric)
SetGadgetState(#SatSpin, 0)
TextGadget(#SatCanvas, 180, 70, 50, 4, "")
DisableGadget(#SatCanvas, #True)

TextGadget(#LightText, 130, 90, 40, 20, "Light:", #PB_Text_Right)
SpinGadget(#LightSpin, 180, 90, 50, 20, 0, 100, #PB_Spin_Numeric)
SetGadgetState(#LightSpin, 0)
TextGadget(#LightCanvas, 180, 110, 50, 4, "")
DisableGadget(#LightCanvas, #True)

TextGadget(#HTMLText, 100, 130, 70, 20, "HTML code:", #PB_Text_Right)
StringGadget(#HTMLString, 180, 130, 50, 20, "")

CalcColors()

Repeat
  
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      CalcColors()
      
    Case #PB_Event_CloseWindow
      Exit = #True
      
  EndSelect
  
Until Exit
Bernd
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: C Formula That Converts HSL to RGB()

Post by RASHAD »

Hi Dude
As simple as I can :)

Code: Select all

Global hue.f,sat.f,lum.f

Procedure  RGB2HSL(color)
  Protected r.f,g.f,b.f,cmax.f,cmin.f,c.f
  r = Red(color) / 255
  g = Green(color) / 255
  b = Blue(color) / 255
  Dim a.f(2)
  a(0) = r : a(1) = g : a(2) = b
  SortArray(a.f(),#PB_Sort_Ascending )
  cmax = a(2)
  cmin = a(0)
  lum = (cmax + cmin) / 2
  If cmax = cmin
      hue = 0
      sat = 0
  Else
    c = cmax - cmin
    sat = c / (1 - Abs(2 * lum - 1))
    If r = cmax
      hue = (g - b) / c 
    ElseIf g = cmax
      hue = (b - r) / c + 2              
    ElseIf b = cmax
      hue = (r - g) / c + 4              
    EndIf
    hue = Round(hue * 60,#PB_Round_Nearest)   ;  °
    If hue < 0
      hue = 360 + hue
    EndIf
    sat = Round(sat * 100,#PB_Round_Nearest)  ;  %
    lum = Round(lum * 100,#PB_Round_Nearest)  ;  %
  EndIf
EndProcedure

RGB2HSL($8B6EE6)
Debug hue
Debug sat
Debug lum
Egypt my love
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: C Formula That Converts HSL to RGB()

Post by RASHAD »

Advanced one

Code: Select all

Global hue.f,sat.f,lum.f

Procedure  RGB2HSL(color)
  Protected r.f,g.f,b.f,cmax.f,cmin.f,c.f
  r = Red(color) / 255
  g = Green(color) / 255
  b = Blue(color) / 255
  Dim a.f(2)
  a(0) = r : a(1) = g : a(2) = b
  SortArray(a.f(),#PB_Sort_Ascending )
  cmax = a(2)
  cmin = a(0)
  lum = (cmax + cmin) / 2
  If cmax = cmin
    hue = 0
    sat = 0
  Else
    c = cmax - cmin
    sat = c / (1 - Abs(2 * lum - 1))
    If r = cmax
      hue = (g - b) / c 
    ElseIf g = cmax
      hue = (b - r) / c + 2              
    ElseIf b = cmax
      hue = (r - g) / c + 4              
    EndIf
    hue = Round(hue * 60,#PB_Round_Nearest)   ;  °
    If hue < 0
      hue = 360 + hue
    EndIf
  EndIf
  sat = Round(sat * 100,#PB_Round_Nearest)  ;  %
  lum = Round(lum * 100,#PB_Round_Nearest)  ;  %
EndProcedure

LoadFont(0,"Tahoma",12)
SetGadgetFont(#PB_Default,FontID(0))

OpenWindow(0,0,0,275,145,"RGB 2 HSL",#PB_Window_Tool|#PB_Window_ScreenCentered)

SpinGadget(0,40,10,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(0,0)
text = TextGadget(#PB_Any,10,10,24,24,"",#PB_Text_Border)
SpinGadget(1,40,40,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(1,0)
text1 = TextGadget(#PB_Any,10,40,24,24,"",#PB_Text_Border)
SpinGadget(2,40,70,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(2,0)
text2 = TextGadget(#PB_Any,10,70,24,24,"",#PB_Text_Border)
SpinGadget(3,150,10,80,24,0,360, #PB_Spin_Numeric)
SetGadgetState(3,0)
text3 = TextGadget(#PB_Any,240,10,24,24,Chr(176),#PB_Text_Center|#PB_Text_Border)
SpinGadget(4,150,40,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(4,0)
text4 = TextGadget(#PB_Any,240,40,24,24,"%",#PB_Text_Center|#PB_Text_Border)
SpinGadget(5,150,70,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(5,0)
text5 = TextGadget(#PB_Any,240,70,24,24,"%",#PB_Text_Center|#PB_Text_Border)

ButtonGadget(10,10,110,80,26,"Convert")
ButtonGadget(20,185,110,80,26,"EXIT")

Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0,1,2
          red = GetGadgetState(0)
          green = GetGadgetState(1)
          blue = GetGadgetState(2)
          SetGadgetColor(text,#PB_Gadget_BackColor,RGB(red,0,0))
          SetGadgetColor(text1,#PB_Gadget_BackColor,RGB(0,green,0))
          SetGadgetColor(text2,#PB_Gadget_BackColor,RGB(0,0,blue))
          
        Case 10
          red = GetGadgetState(0)
          green = GetGadgetState(1)
          blue = GetGadgetState(2)
          color = RGB(red,green,blue)
          RGB2HSL(color)
          SetGadgetState(3,hue)
          SetGadgetState(4,sat)
          SetGadgetState(5,lum)
          
        Case 20
          End
          
      EndSelect
  EndSelect
ForEver
Edit :Bug fixed
Last edited by RASHAD on Sat Mar 24, 2018 11:00 am, edited 1 time in total.
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: C Formula That Converts HSL to RGB()

Post by Dude »

Hi Rashad, I tried converting your simple version to a procedure that accepts R,G,B values but the results don't match Paint Shop Pro's HSL results. :(

Here's the PSP image and the results I'm expecting, and the procedure I used:

Image

Code: Select all

Global rgb2hsl_hue.f, rgb2hsl_sat.f, rgb2hsl_lum.f

Procedure rgb2hsl(re.f, gr.f, bl.f)
  r.f = re / 255
  g.f = gr / 255
  b.f = bl / 255
  Dim a.f(2)
  a(0) = r : a(1) = g : a(2) = b
  SortArray(a.f(),#PB_Sort_Ascending)
  cmax.f = a(2)
  cmin.f = a(0)
  rgb2hsl_lum = (cmax + cmin) / 2
  If cmax = cmin
    rgb2hsl_hue = 0
    rgb2hsl_sat = 0
  Else
    c.f = cmax - cmin
    rgb2hsl_sat = c / (1 - Abs(2 * rgb2hsl_lum - 1))
    If r = cmax
      rgb2hsl_hue = (g - b) / c
    ElseIf g = cmax
      rgb2hsl_hue = (b - r) / c + 2             
    ElseIf b = cmax
      rgb2hsl_hue = (r - g) / c + 4             
    EndIf
    rgb2hsl_hue = Round(rgb2hsl_hue * 60,#PB_Round_Nearest)  ; °
    If rgb2hsl_hue < 0
      rgb2hsl_hue = 360 + rgb2hsl_hue
    EndIf
    rgb2hsl_sat = Round(rgb2hsl_sat * 100,#PB_Round_Nearest) ; %
    rgb2hsl_lum = Round(rgb2hsl_lum * 100,#PB_Round_Nearest) ; %
  EndIf
EndProcedure

rgb2hsl(174,155,110)
Debug rgb2hsl_hue ; 42 is correct (Paint Shop Pro incorrectly shows 30)
Debug rgb2hsl_sat ; 28 is correct (Paint Shop Pro incorrectly shows 72)
Debug rgb2hsl_lum ; 56 is correct (Paint Shop Pro incorrectly shows 142)
Last edited by Dude on Sat Mar 24, 2018 5:43 am, edited 2 times in total.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: C Formula That Converts HSL to RGB()

Post by RASHAD »

Hi Dude
It is OK
lum = 56% of 255 (0.56*255 = 142)
sat = 28% of 255(0.28*255 = 72)
hue = 42 of 360 = 30 of 255(255/360*42 = 30)
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: C Formula That Converts HSL to RGB()

Post by Dude »

Hi Rashad, you're right - PureBasic's own color picker matches your code, and other online RGB to HSL calculators match it. Seems that something is up with Paint Shop Pro's calculations, then. :shock: I also updated my code above to be a little more efficient (no need to use RGB() to convert, LOL).

Here's a site that I found that confirms your formula: https://www.rapidtables.com/convert/col ... o-hsl.html

The correct results are below. Thanks for your help again! :)

Image
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: C Formula That Converts HSL to RGB()

Post by Dude »

And thanks to your teaching, Rashad, I was able to work out my own RGB to CMYK converter. :)

The results of my code match the results here: https://www.rapidtables.com/convert/col ... -cmyk.html

Thanks to everyone else for responding, too. 8)

Edited on 31 March 2018 to fix a bug when converting RGB(0,0,0).

Code: Select all

Global rgb2cmyk_c.f, rgb2cmyk_m.f, rgb2cmyk_y.f, rgb2cmyk_k.f

Procedure rgb2cmyk(re.f, gr.f, bl.f)
  If re = 0 And gr = 0 And bl = 0
    rgb2cmyk_c = 0
    rgb2cmyk_m = 0
    rgb2cmyk_y = 0
    rgb2cmyk_k = 100
  Else
    r.f = re / 255
    g.f = gr / 255
    b.f = bl / 255
    Dim a.f(2)
    a(0) = r : a(1) = g : a(2) = b
    SortArray(a.f(),#PB_Sort_Ascending)
    cmax.f = a(2)
    rgb2cmyk_k = (1 - cmax)
    rgb2cmyk_c = Round((1 - r - rgb2cmyk_k) / (1 - rgb2cmyk_k) * 100, #PB_Round_Nearest)
    rgb2cmyk_m = Round((1 - g - rgb2cmyk_k) / (1 - rgb2cmyk_k) * 100, #PB_Round_Nearest)
    rgb2cmyk_y = Round((1 - b - rgb2cmyk_k) / (1 - rgb2cmyk_k) * 100, #PB_Round_Nearest)
    rgb2cmyk_k = Round(rgb2cmyk_k * 100, #PB_Round_Nearest)
  EndIf
EndProcedure

rgb2cmyk(174,155,110)
Debug rgb2cmyk_c ; 0
Debug rgb2cmyk_m ; 11
Debug rgb2cmyk_y ; 37
Debug rgb2cmyk_k ; 32
Last edited by Dude on Sat Mar 31, 2018 8:12 am, edited 3 times in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: C Formula That Converts HSL to RGB()

Post by wilbert »

RASHAD wrote:Advanced one
There's a problem with your code.
Try R=64, G=64, B=64 .
The output shows a Lightness value of 0 :?
Windows (x64)
Raspberry Pi OS (Arm64)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: C Formula That Converts HSL to RGB()

Post by RASHAD »

@Dude
Glad that you got your aim
And thanks for CMYK converter

@wilbert
Thanks for the catch
Code updated

It is not advanced after all :)

Keeping even the fraction

Code: Select all

Global hue.f,sat.f,lum.f

Procedure  RGB2HSL(color)
  Protected r.f,g.f,b.f,cmax.f,cmin.f,c.f
  r = Red(color) / 255
  g = Green(color) / 255
  b = Blue(color) / 255
  Dim a.f(2)
  a(0) = r : a(1) = g : a(2) = b
  SortArray(a.f(),#PB_Sort_Ascending )
  cmax = a(2)
  cmin = a(0)
  lum = (cmax + cmin) / 2
  If cmax = cmin
      hue = 0
      sat = 0
  Else
    c = cmax - cmin
    sat = c / (1 - Abs(2 * lum - 1))
    If r = cmax
      hue = (g - b) / c
    ElseIf g = cmax
      hue = (b - r) / c + 2             
    ElseIf b = cmax
      hue = (r - g) / c + 4             
    EndIf
  endif
EndProcedure

LoadFont(0,"Tahoma",12)
SetGadgetFont(#PB_Default,FontID(0))

OpenWindow(0,0,0,275,145,"RGB 2 HSL",#PB_Window_Tool|#PB_Window_ScreenCentered)

SpinGadget(0,40,10,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(0,0)
text = TextGadget(#PB_Any,10,10,24,24,"",#PB_Text_Border)
SpinGadget(1,40,40,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(1,0)
text1 = TextGadget(#PB_Any,10,40,24,24,"",#PB_Text_Border)
SpinGadget(2,40,70,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(2,0)
text2 = TextGadget(#PB_Any,10,70,24,24,"",#PB_Text_Border)
SpinGadget(3,150,10,80,24,0,360, #PB_Spin_Numeric)
SetGadgetState(3,0)
text3 = TextGadget(#PB_Any,240,10,24,24,Chr(176),#PB_Text_Center|#PB_Text_Border)
SpinGadget(4,150,40,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(4,0)
text4 = TextGadget(#PB_Any,240,40,24,24,"%",#PB_Text_Center|#PB_Text_Border)
SpinGadget(5,150,70,80,24,0,255, #PB_Spin_Numeric)
SetGadgetState(5,0)
text5 = TextGadget(#PB_Any,240,70,24,24,"%",#PB_Text_Center|#PB_Text_Border)

ButtonGadget(10,10,110,80,26,"Convert")
ButtonGadget(20,185,110,80,26,"EXIT")

Repeat 
  Select WaitWindowEvent()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0,1,2
          red = GetGadgetState(0)
          green = GetGadgetState(1)
          blue = GetGadgetState(2)
          SetGadgetColor(text,#PB_Gadget_BackColor,RGB(red,0,0))
          SetGadgetColor(text1,#PB_Gadget_BackColor,RGB(0,green,0))
          SetGadgetColor(text2,#PB_Gadget_BackColor,RGB(0,0,blue))
          
        Case 10
          red = GetGadgetState(0)
          green = GetGadgetState(1)
          blue = GetGadgetState(2)
          color = RGB(red,green,blue)
          RGB2HSL(color)
          hue = hue * 60   ;  °
          If hue < 0
            hue = 360 + hue
          EndIf
          sat = sat * 100  ;  %
          lum = lum * 100  ;  %
          SetGadgetText(3,StrF(hue,3))
          SetGadgetText(4,StrF(sat,3))
          SetGadgetText(5,StrF(lum,3))
          
        Case 20
          End
          
      EndSelect
  EndSelect
ForEver
Egypt my love
Post Reply