ooRexx logo
   1: #!/usr/bin/env rexx
   2: /**
   3:  * Function: hsl2rgb
   4:  *
   5:  * Converts an HSL color value to RGB.
   6:  * Assumes h in degrees (0-360), s and l are in percentages (0-100)
   7:  * returns r, g, and b in the set [0, 255].
   8:  *
   9:  * @param   number    h       hue (bewteen 0 and 1, not in degrees)
  10:  * @param   number    s       saturation
  11:  * @param   number    l       lightness 
  12:  *
  13:  * @return  string            red green blue
  14:  *
  15:  * @version 1.1
  16:  *
  17:  * @author  Ruurd J. Idenburg (ruurd@idenburg.net)
  18:  *
  19:  * Don't ask me to explain, I just followed the formulas i found
  20:  * here: https://www.rapidtables.com/convert/color/hsl-to-rgb.html
  21:  *
  22:  * 2024/10/21 - Changed args from (0-1) to degrees and percentage(rji)
  23:  *
  24:  **/
  25: parse arg H S L
  26:   S = S/100
  27:   L = L/100
  28:   C = (1-abs(2*L-1))*S
  29:   X =C*(1-abs(abs((H/60)//2)-1))
  30:   m = L-C/2
  31:   select 
  32:     when (H>=0 & H<60)    then do; R=C; G=X; B=0; end;
  33:     when (H>=60 & H<120)  then do; R=X; G=C; B=0; end;
  34:     when (H>=120 & H<180) then do; R=0; G=C; B=X; end;
  35:     when (H>=180 & H<240) then do; R=0; G=X; B=C; end;
  36:     when (H>=240 & H<300) then do; R=X; G=0; B=C; end;
  37:     when (H>=300 & H<360) then do; R=C; G=0; B=X; end;
  38:     otherwise nop
  39:   end
  40: --return the normalized rounded values for the colours
  41: return format((R+m)*255,,0) format((G+m)*255,,0) format((B+m)*255,,0)
  42: 
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.