% This script shows that the camera does indeed perform some
% form of color-correction.
%
% Some of the xy-chromaticity code was adapted from 
% ColorMatching.m
%
% KLY 98

load xyz;
load /local/class/psych221/tutorials/cMatch/monitor;
rgb2xyz = XYZ'*phosphors;
xyz2rgb = inv(rgb2xyz);

xySpectrum = chromaticity(xyz');
maxXYZ = xyz'*phosphors;

% Draw outer boundary of color gamut

figure
plot(xySpectrum(1,:),xySpectrum(2,:),'k'); axis equal, axis square
grid on, xlabel('x-chromaticity'), ylabel('y-chromaticity')
hold on


xyMonitor = chromaticity(maxXYZ);
plot(xyMonitor(1,1),xyMonitor(2,1),'ro');
plot(xyMonitor(1,2),xyMonitor(2,2),'go');
plot(xyMonitor(1,3),xyMonitor(2,3),'bo');
xlabel('x chromaticity'), ylabel('y chromaticity')
title('Effects of color-balancing on xy values')

p = patch(xyMonitor(1,:), xyMonitor(2,:), [.6 .6 .6]);
text(.2136,.0812,'Monitor Gamut')

whitePoint = chromaticity(sum(maxXYZ')')
plot(whitePoint(1),whitePoint(2),'cs');

% Now display the MEASURED xy values of the white patch
% under various illuminants.

load macbethXYZ_filters

% Altogether, we used 18 different light sources.

whites = macbethXYZ_filters(:,19,:);
size(whites)
whites = reshape(whites, 3,18);
whitesxy = chromaticity(whites);
plot(whitesxy(1,:),whitesxy(2,:),'wo')

% Calculate the chromaticity coordinates of the 'photographed'
% xyz values.

load olympusColors

cameraWhites = (olympusColors(:,:,19))';
cameraWhitesXYZ = rgb2xyz * cameraWhites;
cameraWhitesxy = chromaticity(cameraWhitesXYZ);
plot(cameraWhitesxy(1,:),cameraWhitesxy(2,:),'+y')
text(.32,.82,'o - values reported by photometer')
text(.32,.78,'+ - values reported by camera')
text(.32,.74,'square indicates display white point')

% Make a few labels

line([.454,.531],[.4356,.5609])
line([.5126,.6393],[.413,.47])
line([.4023,.2217],[.4078,.4632])
line([.4791,.5525],[.3603,.26])

text(.54,.56,'Green')
text(.65,.47,'Yellow')
text(.16,.4632,'Blue')
text(.56,.26,'Magenta')



