rgb_to_hsv Subroutine

private pure elemental subroutine rgb_to_hsv(r, g, b, h, s, v)

Arguments

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

Called by

proc~~rgb_to_hsv~~CalledByGraph proc~rgb_to_hsv rgb_to_hsv proc~convert color%convert proc~convert->proc~rgb_to_hsv 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 rgb_to_hsv(r, g, b, h, s, v)
      integer(ik), intent(in)  :: r, g, b
      real(rk),    intent(out) :: h, s, v
      real(rk) :: rn, gn, bn, cmax, cmin, delta

      rn = real(r, kind=rk) / 255.0_rk
      gn = real(g, kind=rk) / 255.0_rk
      bn = real(b, kind=rk) / 255.0_rk

      cmax = max(rn, max(gn, bn))
      cmin = min(rn, min(gn, bn))
      delta = cmax - cmin

      v = cmax

      if (delta < 1e-6_rk) then
         h = 0.0_rk
         s = 0.0_rk
      else
         if (cmax > 0.0_rk) then
            s = delta / cmax
            if (abs(cmax - rn) < 1.0e-6_rk) then
               h = 60.0_rk * mod(((gn - bn) / delta), 6.0_rk)
            elseif (abs(cmax - gn) < 1.0e-6_rk) then
               h = 60.0_rk * (((bn - rn) / delta) + 2.0_rk)
            else
               h = 60.0_rk * (((rn - gn) / delta) + 4.0_rk)
            end if
         end if

         if (h < 0.0_rk) h = h + 360.0_rk
      end if

      s = s * 100.0_rk
      v = v * 100.0_rk
   end subroutine rgb_to_hsv