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
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
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
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
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
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
Is it possible to get all these LUTs as download?
Yes. https://www.knuterikevensen.com/?p=2813