%Chapter 7,  Fig 7.13
%Eric Dubois, updated 2018-12-13
%make a step test pattern to step from a starting color C1 to an ending
%color C2. C1 and C2 are specified in sRGB gamma-corrected coordinates.
%Required functions: sRGB_gamma, sRGB_gamma_correction
%Several images are generated.
%1. the variation is linear in tristimulus coordinates, 
%2. the variation is linear in gamma-corrected sRGB
%3. the variation is linear in CIELAB
%1 and 3 are used in the book
% Black and White is used in the book for (a) and (b).
% Blue at mid luminance to green at max luminance is used in the book for (c) and (d).
% Blue to green at luminance of blue (.0722) is used in the book for (e) and (f).
%Uncomment the ones desired
clear all, close all
%
%Parameters
K = 16;          %number of steps, a power of 2 less than or equal to 32
NW = 480;        %image width at 300 dpi, a multiple of K
NH = 320;        %image height at 300 dpi, a multiple of K
ST = NW/K;       %step width
pwidth = 2.54*NW/300;      %plot width in centimeters
pheight = 2.54*NH/300;     %plot height in centimeters
%SetPlotSize ([pwidth, pheight], 'centimeters');
%Start and final colors in sRGB coordinates
%Black and White
%  C1 = [0; 0; 0];
%  C2 = [1; 1; 1];
%Green and Blue at maximum luminance
% C1 = [0; 1; 0];
% C2 = [0; 0; 1];
%Green and Red at maximum luminance
% C1 = [0; 1; 0];
% C2 = [1; 0; 0];
% Blue to green at luminance of blue (.0722) 
C1 = [0; 0; 1];
C2 = [0; .3509; 0];
% Green and red at luminance of red (.2126)
% C1 = [0; .5814; 0];
% C2 = [1; 0; 0];
%Blue at mid luminance to green at max luminance
% C1 = [0; 0; .3];
% C2 = [0; 1; 0];
%Compute colors of step pattern in linear tristimulus coordinates. Store in
%a K x 3 matrix. Create the linear step pattern
%Apply gamma to C1 and C2
C1L = sRGB_gamma(C1);
C2L = sRGB_gamma(C2);
lin_img = zeros(NH,NW,3);
for k = 1:K
   DL(k,:) =  ((K-k)*C1L + (k-1)*C2L)'/(K-1);
   for c=1:3
       lin_img(1:NH,(k-1)*ST+1:k*ST,c) = DL(k,c);
   end
end
%apply sRGB gamma correction
lin_img_gc = sRGB_gamma_correction(lin_img);
figure;
image(1:NW,1:NH,lin_img_gc);
set(gcf,'Color',[1 1 1]);
%Compute colors of step pattern in sRGB coordinates. Store in
%a K x 3 matrix. Create the linear step pattern
lin_img_gc = zeros(NH,NW,3);
for k = 1:K
   D(k,:) =  ((K-k)*C1 + (k-1)*C2)'/(K-1);
   for c=1:3
       lin_img(1:NH,(k-1)*ST+1:k*ST,c) = D(k,c);
   end
end
figure;
image(1:NW,1:NH,lin_img);
set(gcf,'Color',[1 1 1]);
%Compute colors of the step pattern linear in LAB coordinates. 
%Convert C1 and C2 to LAB
A_XYZtosRGB = [3.2406 -1.5372 -0.4986; ...
              -0.9689  1.8758  0.0415; ...
               0.0557 -0.2040  1.0570];
C1_XYZ = A_XYZtosRGB\C1L;
C2_XYZ = A_XYZtosRGB\C2L;
C1_LAB = XYZ2LAB(C1_XYZ')';
C2_LAB = XYZ2LAB(C2_XYZ')';
%Compute colors of step pattern linear in LAB
for k = 1:K
   D_LAB(k,:) =  ((K-k)*C1_LAB + (k-1)*C2_LAB)'/(K-1); 
end
%Convert from LAB to gamma corrected sRGB
D_LAB2XYZ = LAB2XYZ(D_LAB);
D_LAB2sRGB = (A_XYZtosRGB*D_LAB2XYZ')';
%apply sRGB gamma correction
D_LAB2sRGB_gc = sRGB_gamma_correction(D_LAB2sRGB);
%Form the step pattern image
lin_img_LAB = zeros(NH,NW,3);
for k = 1:K
     for c=1:3
       lin_img_LAB(1:NH,(k-1)*ST+1:k*ST,c) = D_LAB2sRGB_gc(k,c);
   end
end
figure;
image(1:NW,1:NH,lin_img_LAB);
set(gcf,'Color',[1 1 1]);
