hsl_to_rgb Subroutine

private pure elemental subroutine hsl_to_rgb(h, s, l, r, g, b)

Arguments

Type IntentOptional Attributes Name
real(kind=rk), intent(in) :: h
real(kind=rk), intent(in) :: s
real(kind=rk), intent(in) :: l
integer(kind=ik), intent(out) :: r
integer(kind=ik), intent(out) :: g
integer(kind=ik), intent(out) :: b

Called by

proc~~hsl_to_rgb~~CalledByGraph proc~hsl_to_rgb hsl_to_rgb proc~convert color%convert proc~convert->proc~hsl_to_rgb proc~initialize_colors initialize_colors proc~initialize_colors->proc~convert proc~set color%set proc~initialize_colors->proc~set program~demo_color demo_color program~demo_color->proc~convert proc~find_nearest color%find_nearest program~demo_color->proc~find_nearest proc~print_available_colors color%print_available_colors program~demo_color->proc~print_available_colors proc~save_available_colors color%save_available_colors program~demo_color->proc~save_available_colors program~demo_color->proc~set program~example26 example26 program~example26->proc~convert program~example26->proc~set program~example27 example27 program~example27->proc~convert program~example27->proc~find_nearest program~example27->proc~set proc~find_nearest->proc~initialize_colors proc~print_available_colors->proc~initialize_colors proc~save_available_colors->proc~initialize_colors proc~set_by_name color%set_by_name proc~set_by_name->proc~initialize_colors proc~set->proc~set_by_name program~example29 example29 program~example29->proc~print_available_colors program~example29->proc~save_available_colors program~example28 example28 program~example28->proc~set

Source Code

   elemental pure subroutine hsl_to_rgb(h, s, l, r, g, b)
      real(rk), intent(in) :: h, s, l
      integer(ik), intent(out) :: r, g, b
      real(rk) :: hn, sn, ln
      real(rk) :: c, h_prime, x, m
      real(rk) :: r1, g1, b1

      hn = h
      sn = s / 100.0_rk
      ln = l / 100.0_rk

      c = (1.0_rk - abs(2.0_rk * ln - 1.0_rk)) * sn

      h_prime = mod(hn, 360.0_rk) / 60.0_rk

      x = c * (1.0_rk - abs(mod(h_prime, 2.0_rk) - 1.0_rk))

      select case (int(h_prime))
       case (0)
         r1 = c
         g1 = x
         b1 = 0.0_rk
       case (1)
         r1 = x
         g1 = c
         b1 = 0.0_rk
       case (2)
         r1 = 0.0_rk
         g1 = c
         b1 = x
       case (3)
         r1 = 0.0_rk
         g1 = x
         b1 = c
       case (4)
         r1 = x
         g1 = 0.0_rk
         b1 = c
       case (5)
         r1 = c
         g1 = 0.0_rk
         b1 = x
      end select

      m = ln - c / 2.0_rk

      r = nint(r1 * 255.0_rk + m * 255.0_rk, kind=ik)
      g = nint(g1 * 255.0_rk + m * 255.0_rk, kind=ik)
      b = nint(b1 * 255.0_rk + m * 255.0_rk, kind=ik)
   end subroutine hsl_to_rgb