Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIAi2 WildChat-4.8M (AI2)

Comvert W* U* V* (CIE 1964) Color (under illuminant C to XYZ Under D65 Illuminant, and…

  1. User

    Comvert W* U* V* (CIE 1964) Color (under illuminant C to XYZ Under D65 Illuminant, and then to SRGB triplet.. Output code should be BASIC or FORTRAN IV..

  2. Assistant

    Certainly! Below is a step-by-step explanation and then a FORTRAN IV code snippet that converts a color from W* U* V* (CIE 1964 UCS) under Illuminant C to XYZ under Illuminant D65, and then from XYZ (D65) to sRGB triplet.


    Overview of the Conversion Steps:

    1. W* U* V* (CIE 1964) to XYZ (Illuminant C):

      The CIE 1964 UCS color space is defined by:

      [ U^* = 13 W^* (u - u_n) \ V^* = 13 W^* (v - v_n) ]

      where ( u, v ) are the chromaticity coordinates of the color, and ( u_n, v_n ) are the chromaticities of the white point (Illuminant C here). Also:

      [ W^* = 25 Y^{1/3} - 17 ]

      Rearranged to get (Y):

      [ Y = \left(\frac{W^* + 17}{25}\right)^3 ]

      From (U^), (V^), and (W^*), recover (u) and (v):

      [ u = \frac{U^}{13 W^} + u_n \ v = \frac{V^}{13 W^} + v_n ]

      Once (u, v, Y) are known, recover (X, Z):

      [ X = Y \frac{9u}{4v} \ Z = Y \frac{12 - 3u - 20v}{4v} ]


    1. Adapt XYZ from Illuminant C to Illuminant D65:

      The adaptation matrix from C to D65 (using Bradford adapted chromatic adaptation matrix) can be applied:

      [ XYZ_{D65} = M_{adapt} \times XYZ_C ]

      We'll use Von Kries chromatic adaptation via cone response domain.

      Steps:

      • Convert (XYZ_C) to LMS cone responses:

        [ LMS = M_{B} \times XYZ_C ]

        where

        [ M_B = \begin{bmatrix} 0.8951 & 0.2664 & -0.1614 \ -0.7502 & 1.7135 & 0.0367 \ 0.0389 & -0.0685 & 1.0296 \end{bmatrix} ]

      • Scale LMS channels by the ratio of source and target white points' LMS values.

        First calculate (L_{n}, M_{n}, S_{n}) for source and target illuminants from their (XY) chromaticities.

      • Convert back LMS to (XYZ_{D65}) by inverse of (M_B):

        [ XYZ_{D65} = M_B^{-1} \times LMS_{adapted} ]


    1. Convert XYZ D65 to linear sRGB:

      [ \begin{bmatrix} R_{lin} \ G_{lin} \ B_{lin} \end{bmatrix}

      \begin{bmatrix} +3.2406 & -1.5372 & -0.4986 \ -0.9689 & 1.8758 & 0.0415 \ 0.0557 & -0.2040 & 1.0570 \end{bmatrix} \times \begin{bmatrix} X_{D65} \ Y_{D65} \ Z_{D65} \end{bmatrix} ]


    1. Gamma correction to sRGB:

      For each channel (C_{lin}):

      [ C_{sRGB} = \begin{cases} 12.92 \times C_{lin}, & C_{lin} \leq 0.0031308 \ 1.055 \times C_{lin}^{1/2.4} - 0.055, & C_{lin} > 0.0031308 \end{cases} ]

      Clamp to [0,1].


    Reference Illuminant Chromaticities:

    • Illuminant C (1950):

      [ x_n = 0.3101, \quad y_n = 0.3162 ]

    • Illuminant D65:

      [ x_{D65} = 0.3127, \quad y_{D65} = 0.3290 ]


    Fortran IV Code:

          PROGRAM WUV_TO_SRGB
    C     Converts W*U*V* (CIE 1964) under Illuminant C
    C     to sRGB triplet under Illuminant D65
    
    C     Input: Wstar, Ustar, Vstar
          REAL Wstar, Ustar, Vstar
    C     Output: sRGB triplet in 0..1 range: R,G,B
    
          REAL U_n, V_n
          REAL u, v, Y, X, Z
          REAL X_D65, Y_D65, Z_D65
          REAL LMS(3), LMS_white_src(3), LMS_white_dst(3)
          REAL R_lin, G_lin, B_lin
          REAL R, G, B
          REAL T(3,3), T_inv(3,3)
          REAL M_RGB(3,3)
          REAL Xn_src, Yn_src, Zn_src
          REAL Xn_dst, Yn_dst, Zn_dst
          REAL Ln_src, Mn_src, Sn_src
          REAL Ln_dst, Mn_dst, Sn_dst
          REAL u_white_src, v_white_src
          REAL u_white_dst, v_white_dst
          REAL temp
    
    C     Illuminant C white point chromaticities
          U_n = 0.2009     ! u for Illuminant C, approx
          V_n = 0.4610     ! v for Illuminant C, approx
    
    C     Illuminant C chromaticities from x,y
          Xn_src = 0.3101
          Yn_src = 1.0
          Zn_src = (1.0 - 0.3101 - 0.3162)/0.3162  ! Y=1, Z from x,y
    
    C     Calculate v for Illuminant C from x,y
          u_white_src = (4.0 * Xn_src) / (Xn_src + 15.0*Yn_src + 3.0 * Zn_src)
          v_white_src = (6.0 * Yn_src) / (Xn_src + 15.0*Yn_src + 3.0 * Zn_src)
    
    C     Illuminant D65 white (standard)
          Xn_dst = 0.3127
          Yn_dst = 1.0
          Zn_dst = (1.0 - 0.3127 - 0.3290)/0.3290
    
    C     Calculate v for Illuminant D65
          u_white_dst = (4.0 * Xn_dst) / (Xn_dst + 15.0*Yn_dst + 3.0 * Zn_dst)
          v_white_dst = (6.0 * Yn_dst) / (Xn_dst + 15.0*Yn_dst + 3.0 * Zn_dst)
    
    C     Read input Wstar, Ustar, Vstar
          PRINT *, 'Enter W*, U*, V* values:'
          READ(*,*) Wstar, Ustar, Vstar
    
    C     Handle W* = 0 carefully to avoid division by zero
          IF (Wstar .EQ. 0.0) THEN
              PRINT *, 'W* cannot be zero.'
              STOP
          END IF
    
    C     Recover Y from W*
          Y = ((Wstar + 17.0) / 25.0)**3
    
    C     Calculate u and v from U*, V*, W*
          u = Ustar / (13.0 * Wstar) + u_white_src
          v = Vstar / (13.0 * Wstar) + v_white_src
    
    C     Recover X and Z from u,v,Y
          X = Y * (9.0*u) / (4.0*v)
          Z = Y * (12.0 - 3.0*u - 20.0*v) / (4.0*v)
    
    C     Define Bradford transform matrix M_B and inverse
    
          DATA T / 0.8951,  0.2664, -0.1614,
         &       -0.7502,  1.7135,  0.0367,
         &        0.0389, -0.0685,  1.0296 /
    C
          DATA T_inv / 0.986993, -0.147054,  0.159963,
         &           0.432305,  0.518360,  0.049291,
         &          -0.008529,  0.040043,  0.968487 /
    
    C     Convert white points to LMS
          CALL MATVEC(T, (/Xn_src, Yn_src, Zn_src/), LMS_white_src)
          CALL MATVEC(T, (/Xn_dst, Yn_dst, Zn_dst/), LMS_white_dst)
    
    C     Convert color XYZ_C to LMS
          CALL MATVEC(T, (/X, Y, Z/), LMS)
    
    C     Adapt LMS by white scaling factors
          LMS(1) = LMS(1) * LMS_white_dst(1) / LMS_white_src(1)
          LMS(2) = LMS(2) * LMS_white_dst(2) / LMS_white_src(2)
          LMS(3) = LMS(3) * LMS_white_dst(3) / LMS_white_src(3)
    
    C     Convert adapted LMS back to XYZ D65
          CALL MATVEC(T_inv, LMS, (/X_D65, Y_D65, Z_D65/))
    
    C     Convert XYZ to linear RGB (sRGB)
    C     sRGB RGB matrix
          DATA M_RGB / 3.2406, -1.5372, -0.4986,
         &             -0.9689,  1.8758,  0.0415,
         &              0.0557, -0.2040,  1.0570 /
    
          REAL tempRGB(3)
          CALL MATVEC(M_RGB, (/X_D65, Y_D65, Z_D65/), tempRGB)
    
          R_lin = tempRGB(1)
          G_lin = tempRGB(2)
          B_lin = tempRGB(3)
    
    C     Apply sRGB gamma correction and clamp function
          R = SRGB_GAMMA(R_lin)
          G = SRGB_GAMMA(G_lin)
          B = SRGB_GAMMA(B_lin)
    
    C     Clamp between 0 and 1
          IF (R .LT. 0.0) R = 0.0
          IF (R .GT. 1.0) R = 1.0
          IF (G .LT. 0.0) G = 0.0
          IF (G .GT. 1.0) G = 1.0
          IF (B .LT. 0.0) B = 0.0
          IF (B .GT. 1.0) B = 1.0
    
          PRINT *, 'sRGB values (0-1):'
          PRINT *, R, G, B
    
          END
    
    C-----------------------------
          SUBROUTINE MATVEC(M, V, R)
    C     Multiply 3x3 matrix M by 3-vector V to give R
          REAL M(3,3), V(3), R(3)
          INTEGER I, J
          DO 10 I = 1,3
             R(I) = 0.0
             DO 20 J=1,3
                R(I) = R(I) + M(I,J)*V(J)
    20       CONTINUE
    10    CONTINUE
          RETURN
          END
    
    C-----------------------------
          REAL FUNCTION SRGB_GAMMA(C)
    C     Apply sRGB gamma correction
          REAL C, c_abs
          c_abs = C
          IF (C .LT. 0.0) THEN
            c_abs = 0.0
          END IF
    
          IF (c_abs .LE. 0.0031308) THEN
            SRGB_GAMMA = 12.92 * c_abs
          ELSE
            SRGB_GAMMA = 1.055 * (c_abs)**(1.0/2.4) - 0.055
          END IF
    
          IF(SRGB_GAMMA .LT. 0.0) SRGB_GAMMA = 0.0
          IF(SRGB_GAMMA .GT. 1.0) SRGB_GAMMA = 1.0
    
          RETURN
          END
    

    Notes:

    • The input to this program is the W*, U*, and V* color values under Illuminant C.

    • The program calculates the corresponding XYZ values under Illuminant C, adapts them to Illuminant D65, converts to linear RGB, applies gamma correction, and finally clamps the sRGB values between 0 and 1.

    • The white point's (u_n, v_n) for Illuminant C is approximated as 0.2009 and 0.4610 — you should verify or adjust these if higher accuracy is needed.

    • Floating point accuracy and interpolation issues with cube roots and fractional powers can differ between FORTRAN IV implementations.


    If you want the code in BASIC or have any further questions or adaptations, please ask!