ooRexx logo
   1: #!/usr/bin/env rexx
   2: /**
   3:  * Converts an RGB color value to HSL.
   4:  * Assumes r, g, and b are contained in the set [0, 255] and
   5:  * returns h, s, and l hue in degrees (0-360), saturaion and 
   6:  *                     lightness in percentage (0-100)
   7:  *                              
   8:  * @param   integer   red       The red color value
   9:  * @param   integer   green     The green color value
  10:  * @param   integer   blue      The blue color value
  11:  *
  12:  * @return  string              hue saturation lightness
  13:  *
  14:  * @version 1.1
  15:  *
  16:  * @author  Ruurd J. Idenburg (ruurd@idenburg.net)
  17:  *
  18:  * Don't ask me to explain, I just followed the formulas i found 
  19:  * here: https://www.rapidtables.com/convert/color/rgb-to-hsl.html
  20:  *
  21:  * 2024/10/21 - Changed return values form, now in degrees and percentage (rji)   
  22:  *
  23:  **/
  24: parse arg red green blue
  25:   R = red/255
  26:   G = green/255
  27:   B = blue/255
  28:   Cmax = max(r,max(g,b))
  29:   Cmin = min(r,min(g,b))
  30:   L = (Cmax+Cmin)/2
  31:   if (Cmax==Cmin) 
  32:     then return 0 0 L          -- achromatic
  33:   Chroma = Cmax-Cmin
  34:   if L>0.5
  35:     then S = Chroma/(2-Cmax-Cmin)
  36:     else S = Chroma/(Cmax+Cmin)
  37:   select
  38:     when (Cmax==R) then do
  39:       H = ((G-B)/Chroma)
  40:       if (G < B) then H = H+6
  41:     end
  42:     when (Cmax==G) then H = ((B-R)/Chroma+2)
  43:     when (Cmax==B) then H = ((r-g)/Chroma+4)
  44:     otherwise nop
  45:   end
  46: -- More digits after decimal point might yield better results for hsl2rgb
  47: return format(H/6*360,,0) format(S*100,,1) format(L*100,,1)
  48: 
If you feel inclined to make corrections, suggestions etc., please mail me any.
All content © Ruurd Idenburg, 2007–, except where marked otherwise. All rights reserved. This page is primarily for non-commercial use only. The Idenburg website records no personal information and sets no ‘cookies’. This site is hosted on a VPS(Virtual Private System) rented from Transip.nl, a Dutch company, falling under Dutch (privacy) laws (I think).

This page updated on by Ruurd Idenburg.