Skip to content

How to calculate RP-177 3×3 matrices in Matlab and Python

How to calculate RP-177 3×3 matrices in Matlab.

Update: For part 2 with the RP-177 TRA Matlab code, see this post

The Matlab code was made by Tinna Lif Gunnarsdottir.

To make DCPs in DCI-P3 D6O, D61, D65 or Rec 709 D60, D61, D65, you can use SMPTE RP-177:1993 3×3 NPMs to transfer from RGB to XYZ.

NPM = normalized primary matrix

For more info on the math see SMPTE EG 432-1-10 and How to make 3D LUTs in Nuke (R´B´G´to X´Y´Z´)

update:

You can get some of the 3D LUTS made with these 3×3 matrices free here

Example: DCI-P3 D6O

Here is the Matlab code to calculate the correct RP-177 RGB-XYZ matrices from DCI-P3 D6O to XYZ.

Matlab Code

% COLOR DCI P3
R = [0.68   ; 0.320 ; 0];
G = [0.265  ; 0.690 ; 0.045]; 
B = [0.150  ; 0.060 ; 0.79];
P = [R G B];

% White Point D60
X = 0.3217; Y = 0.3378; Z = 0.3405;

W = [X/Y ; 1 ; Z/Y]; CC = P \ W;

C = [ CC(1,1) 0 0 ; 0 CC(2,1) 0 ; 0 0 CC(3,1)];

NPM = P * C
NPM =
0.504739646629896 0.264744009223770 0.182855006076470
0.237524539590539 0.689333457978873 0.073142002430588
0                 0.044956529868187 0.963036365336076

Pasted into a Nuke color matrix node:

0.48657095  0.26566769  0.19821729
0.22897457  0.69173852  0.07928691
0           0.04511338  1.04394437

The middle row sums to 1

0.22897457+0.69173852+0.07928691 = 1

Python Code

import numpy as np

# COLOR DCI P3
R = np.array([0.68, 0.320, 0])
G = np.array([0.265, 0.690, 0.045])
B = np.array([0.150, 0.060, 0.79])
color_matrix = np.column_stack((R, G, B))

# White Point D60
X = 0.3217
Y = 0.3378
Z = 0.3405
white_point = np.array([X/Y, 1, Z/Y])
scaling_factors = np.linalg.solve(color_matrix, white_point)
scaled_diag_matrix = np.diag(scaling_factors)

# Calculate NPM
NPM = color_matrix @ scaled_diag_matrix

# Display NPM
np.set_printoptions(precision=15)
print("NPM:")
print(NPM)

Download and install Python

Run cmd on windows or terminal on mac.


Install NumPy

pip3 install numpy


Copy and paste code into a text editor like nano, notepad++(win), bbedit(mac) and save it as P3D60NPM.py

Run the code

python3 P3D60NPM.py

The result is the same as in Matlab

NPM:
[[0.504739646629896 0.26474400922377  0.18285500607647 ]
[0.237524539590539  0.689333457978873 0.073142002430588]
[0.                 0.044956529868187 0.963036365336076]]

Other transforms


DCI-P3 D61

WHITE POINT
X = 0.3198;
Y = 0.3360;
Z = 0.3442;

NPM from Matlab:

0.500852913533834 0.264968867481203  0.185963933270677
0.235695488721804 0.689918937969925  0.0743855733082713
0                 0.044994713345865  0.979410048558897

Pasted into a Nuke color matrix node:

0.50085291 0.26496887 0.18596393
0.23569549 0.68991894 0.07438557
0          0.04499471 0.97941005

The middle row sums to 1


0.23569549+0.68991894+0.07438557=1

DCI-P3 DCI-WHITE

WHITE POINT
X = 0.314;
Y = 0.351;
Z = 0.335;

NPM from Matlab

0.445169815564552 0.277134409206778   0.172282669815565
0.209491677912731 0.721595254161044   0.068913067926226
0                 0.047060560053981   0.907355394361973

Pasted into a Nuke color matrix node:

0.44516982 0.27713441    0.17228267
0.20949168 0.72159525    0.06891307
0          0.04706056    0.90735539

The middle row sums to 1

0.20949168+0.72159525+0.06891307=1

REC 709
REC 709 D65, D60, and D61 fit inside DCI-P3 and can be used in DCPs.

REC709 Primaries
R = [0.640 ; 0.330 ; 0.030];
G = [0.300 ; 0.600 ; 0.100];
B = [0.150 ; 0.060 ; 0.790];

REC 709 D65

D65 WHITE POINT
X = 0.3127;
Y = 0.3290;
Z = 0.3583;

NPM from Matlab:

0.412390799265959   0.357584339383878   0.180480788401834
0.212639005871510   0.715168678767756   0.072192315360734
0.019330818715592   0.119194779794626   0.950532152249661

Pasted into a Nuke color matrix node:

0.4123908  0.3575843   0.18048079
0.21263901 0.71516867  0.07219232
0.01933082 0.1191948   0.95053215

The middle row should sum to 1 according to RP-177, so I changed 0.71516868 to 0.71516867.


REC 709 D60

D60 WHITE POINT
X = 0.3217;
Y = 0.3378;
Z = 0.3405;

NPM from Matlab:

0.431575918832227 0.355727231008106   0.165035512089804
0.222531333147867 0.711454462016212   0.066014204835922
0.020230121195261 0.118575743669369   0.869187030339634

Pasted into a Nuke color matrix node:

0.43157592 0.35572723  0.16503551
0.22253133 0.71145447  0.0660142
0.02023012 0.11857574  0.86918703

Changed 0.71145446 to 0.71145447 so the middle row sums to 1

0.22253133+0.71145447+0.0660142= 1

REC 709 D61

D61 WHITE POINT

X = 0.3198;
Y = 0.3360;
Z = 0.3442;

NPM from Matlab:

0.427462229871868 0.356162746222987 0.168160738190859
0.220410212277682 0.712325492445975 0.067264295276343
0.020037292025244 0.118720915407662 0.885646554471856

Pasted into a Nuke color matrix node:

0.42746223 0.35616275 0.16816074
0.22041021 0.71232549 0.0672643
0.02003729 0.11872092 0.88564655


The middle row sums to 1

0.22041021+0.71232549+0.0672643=1

2 thoughts on “How to calculate RP-177 3×3 matrices in Matlab and Python”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.