
## Author: Moxon, M.T.D.
## Created: 2014-10-19

%This octave / matlab file was used to generate the figures for an investigation of pumpkin oil.
%(You can see the instructable on pumpkin oil and dichromatism at: http://www.instructables.com/id/Pumpkin-Seed-Oil-Trick-and-Treat/ )
%
%
%The nice graphs were created with help from this wonderful reference
%https://www.che.utah.edu/department_documents/Projects_Lab/Projects_Lab_Handbook/MatlabPlots.pdf
%



function pumpkin_oil_analysis()

%Set to true if you want the function to save to file
savegraphs = false;

%Plot Color helpers
  myGr = [34 139 34]/255;
  %Make my own colormap
  h = linspace(0,1,256);
  s = 0.9*ones(size(h));
  v = 0.9*ones(size(h));
  RGB = hsv2rgb([h;s;v]');

%Oil properties
  c = 0.1;
  e_gr = 1;
  e_red = 0.2;

%Optical path length of sample
  N = 1000;
  l = linspace(0,20,N);

%Find absorbances
  A_gr = e_gr*c*l;
  A_red = e_red*c*l;

%Find transmittances
  T_gr = power(10,-A_gr);
  T_red = power(10,-A_red);

%Simple transmittance comparison
  myFigure();
  handle = plot(l,T_red,'r',...
                l,T_gr,'Color',myGr);
  xlabel("Depth");
  ylabel("Output Light Intensity");
  title("'True' Transmittance Comparison");
  format_graph(handle);
  legend('Red T','Green T','Location','NorthEast');
  
  if(savegraphs)
    exportFigure('simple_transmittance');
  end
  
%Graph True Hue vs depth
  B = zeros(size(T_gr));
  HSV = rgb2hsv([T_red; T_gr; B]');
  H = HSV(:,1);
  myFigure();
  H1 = uint8(255*H);
  cmap = RGB(H1,:);
  handle = scatter(l,H,6,cmap,'filled');
  xlabel("Depth");
  ylabel("Hue");
  title("'True' Hue vs Sample Depth");
  format_graph(handle);
  if(savegraphs)
    exportFigure('true_hue');
  end
  
  

%Human vision is weighted
  wt_green = 0.7152;
  wt_red = 0.2126;
  wt_blue = 0.0722;
  relative_scaling = wt_green/wt_red;

%Transmittance comparison accounting for human vision
  T_gr_human = T_gr*relative_scaling;
  myFigure();
  handle = plot(l,T_red,'r',...
                l,T_gr_human,'Color',myGr);
  xlabel("Depth");
  ylabel("Output Light Intensity");
  title("'Human' Transmittance Comparison");
  format_graph(handle);
  legend('Red T','Green T','Location','NorthEast');
  
  if(savegraphs)
    exportFigure('perceived_transmittance');
  end

%Graph Human perceived Hue vs Depth
  B = zeros(size(T_gr_human));
  HSV = rgb2hsv([T_red; T_gr_human; B]'/relative_scaling);
  H = HSV(:,1);
  myFigure();
  H1 = uint8(255*H);
  cmap = RGB(H1,:);
  handle = scatter(l,H,6,cmap,'filled');
  xlabel("Depth");
  ylabel("Hue");
  title("Perceived Hue vs Sample Depth");
  format_graph(handle);
  
  if(savegraphs)
    exportFigure('perceived_hue');
  end
  
endfunction



##Subfunctions


%Improves on the formatting of default matlab figures
function myFigure()
  %Export graphics and figure configurations
  figure;
  set(gcf,'PaperUnits','inches');
  set(gcf,'PaperSize',[8 8]);
  set(gcf,'PaperPosition',[1 1 6 6]);
  set(gcf,'PaperPositionMode','Manual');  
endfunction


%Handles common formatting for many of the graphs
function format_graph(handle)
  set(gcf,'color','w');
  
  set(handle,'LineWidth',6);

  set(get(gca,'xlabel'),'FontSize',18,'FontWeight','Bold');
  set(get(gca,'ylabel'),'FontSize',18,'FontWeight','Bold');
  set(get(gca,'title'),'FontSize',18,'FontWeight','Bold');
  
  set(gca,'FontSize',13);
  set(gca,'FontWeight','Bold');
  set(gca,'LineWidth',2);
  
  box off;
  axis square;  
endfunction



%Improves on default saving of generated figures
function exportFigure(filename)
    
  print(gcf,'-dpdf','-r150',strcat(filename,'.pdf'));
  print(gcf,'-dpng',strcat(filename,'.png'));
  
endfunction


