close all;
clear all;
cd /local/class/psych221/tutorials
load macbethChart; 
greenChip = 11; redChip = 15; whiteChip = 19; grayChip = 20;
macbethImage = flipud(rot90(reshape(1:24,6,4)));

load D65; 
lgt = D65;

load cMatch/monitor; 			% This contains the monitor phosphors
load cMatch/monitorGam;  % This contains monitorInvGamma
load XYZ;
cd ~/proj;

% Factor used to apply color cast on images
factor=1.5;

% Monitor gamma value
myGamma=2.7;

macbethXYZ = XYZ'* diag(lgt)* macbethChart;
xyz2rgb = inv(XYZ'*phosphors);
macbethLinearRGB = xyz2rgb*macbethXYZ;

wht = macbethLinearRGB(:,whiteChip);
macbethLinearRGB = diag(1./wht)*macbethLinearRGB;
macbethLinearRGB = scale(macbethLinearRGB,0,1);

% Darken everything
macbethLinearRGB = macbethLinearRGB * 0.1;

% Red=15, green=14 or blue=15 surface.
for n=1:24
macbethLinearRGB(:,n) = macbethLinearRGB(:,13);
end

% Display the dark constant surface image
grayLinearRGB = macbethLinearRGB';

grayRGB = (grayLinearRGB).^(1/myGamma);
figure; colormap(grayRGB); 
image(macbethImage);
title('Dark Constant Surface Image')
