%Chapter 7,  Fig 7.4
%Eric Dubois, updated 2018-12-12
% Plot the CIE 1931 RGB color matching functions 
% data files obtained from CVRL website
%Data files should be placed in a data directory on the MATLAB path.
clear all, close all;
% read the CIE 1931 XYZ color matching functions.
% they are defined from 360 nm to 830 nm in steps of 1 nm.
xyz = dlmread('ciexyz31_1.txt',',');
% transform them to the CIE 1931 RGB color matching functions
%1931 CIE RGB to XYZ -- matrix from Schanda
A_RGB31toXYZ = [2.768892 1.751748  1.130160; ...
                1.0      4.590700  0.060100;  ...
                0.0      0.056508  5.594292];
A_XYZtoRGB31 = inv(A_RGB31toXYZ);
rgb31cmf = xyz(:,2:4)*A_XYZtoRGB31';
% plot the rgb color matching functions
figure;
lamxyz = xyz(:,1);
plot(lamxyz,rgb31cmf(:,1),'r',lamxyz,rgb31cmf(:,2),'g',lamxyz,rgb31cmf(:,3),'b');
xlabel('Wavelength (nm)');
ylabel('Tristimulus values');
axis([350 850 -.1 0.4]);
text(600,.36,'r'),text(545,.23,'g'),text(465,.3,'b');
set(gcf,'Color',[1 1 1]);
set(gca,'fontname','times')          %set the plot font to Times