rgb_to_xyz Subroutine

private pure elemental subroutine rgb_to_xyz(r, g, b, x, y, z)

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) :: x
real(kind=rk), intent(out) :: y
real(kind=rk), intent(out) :: z

Called by

proc~~rgb_to_xyz~~CalledByGraph proc~rgb_to_xyz rgb_to_xyz proc~convert color%convert proc~convert->proc~rgb_to_xyz 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_xyz(r, g, b, x, y, z)
      integer(ik), intent(in) :: r, g, b
      real(rk), intent(out) :: x, y, z
      real(rk) :: rn, gn, bn

      ! Normalize RGB values to the range [0, 1]
      rn = real(r, kind=rk) / 255.0_rk
      gn = real(g, kind=rk) / 255.0_rk
      bn = real(b, kind=rk) / 255.0_rk

      ! Apply gamma correction
      if (rn <= 0.04045_rk) then
         rn = rn / 12.92_rk
      else
         rn = ((rn + 0.055_rk) / 1.055_rk) ** 2.4_rk
      end if

      if (gn <= 0.04045_rk) then
         gn = gn / 12.92_rk
      else
         gn = ((gn + 0.055_rk) / 1.055_rk) ** 2.4_rk
      end if

      if (bn <= 0.04045_rk) then
         bn = bn / 12.92_rk
      else
         bn = ((bn + 0.055_rk) / 1.055_rk) ** 2.4_rk
      end if

      ! Convert RGB to XYZ using defined transformation matrix
      x = 0.4124564_rk * rn + 0.3575761_rk * gn + 0.1804375_rk * bn
      y = 0.2126729_rk * rn + 0.7151522_rk * gn + 0.0721750_rk * bn
      z = 0.0193339_rk * rn + 0.1191920_rk * gn + 0.9503041_rk * bn

      x = x*100.0_rk
      y = y*100.0_rk
      z = z*100.0_rk
   end subroutine rgb_to_xyz