% optimals.rnw
% Time-stamp: "optimals.rnw"

\documentclass[11pt]{article}

% Set margins to be narrow
\RequirePackage[left=1in,top=0.75in,right=1in,bottom=0.75in]{geometry}


%\VignetteIndexEntry{Plotting Chromaticity Loci of Optimal Colors - the MacAdam Limits}
%\VignetteEngine{knitr::knitr}

\RequirePackage{color}
\definecolor{darkblue}{rgb}{0,0,0.5}
\definecolor{blue}{rgb}{0,0,0.8}
\definecolor{lightblue}{rgb}{0.2,0.2,0.9}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}
\definecolor{red}{rgb}{0.7,0,0}
\definecolor{darkgreen}{rgb}{0.0,0.4,0.0}
\definecolor{lightgray}{rgb}{0.7,0.7,0.7}
\definecolor{darkorange}{rgb}{0.75, 0.45, 0}
\definecolor{purple}{rgb}{0.65, 0, 0.75}
\definecolor{goldenrod}{rgb}{0.80, 0.61, 0.11}
\definecolor{lightyellow}{rgb}{0.98,0.94,0.83}


\RequirePackage{fancyvrb}
\RequirePackage[T1]{fontenc}
\RequirePackage{ae}       % ComputerModern Fonts
\RequirePackage{fancyhdr}
\RequirePackage{float}
\RequirePackage{hyperref}
\usepackage{lastpage}

\pagestyle{fancy}
\cfoot{page \thepage\ of \pageref{LastPage}}
\renewcommand{\headrulewidth}{0pt}

% \code mini environment ttfamily->texttt
\newcommand\code{\bgroup\@codex}
\def\@codex#1{{\color{darkred} \normalfont\ttfamily\hyphenchar\font=-1 #1}\egroup}

% This environment defines the look of R ouput
\DefineVerbatimEnvironment{Soutput}{Verbatim}{
  fontsize=\small,
  formatcom=\color{darkblue}
}

\begin{document}
% \SweaveOpts{concordance=TRUE}

\title{ {\Huge Plotting Chromaticity Loci of Optimal Colors} \\ {\Large Reproducing plots of MacAdam Limits}}
\author{Glenn Davis     \url{    <gdavis@gluonics.com>}}
\maketitle
% \thispagestyle{fancy}

% Setup stuff.
<<setup, echo=FALSE, results="hide">>=
require("knitr",quietly=TRUE)
opts_chunk$set(fig.path="figs/ag2-", fig.align="center",
  fig.width=7, fig.height=7, comment="")
knit_hooks$set(output = function(x, options) {
  paste('\\begin{Soutput}\n', x, '\\end{Soutput}\n', sep = '')
})
options(width=90)
if(!file.exists("figs")) dir.create("figs")
@
% ----------------------------------------------------------------------------
\section*{Introduction}

The goal of this vignette is to reproduce 2 figures in \cite{W&S},
and to make similar ones.
The featured function from \textbf{colorSpec} used in this vignette is 
\code{sectionOptimalColors()}.
But it requires some help from
the function \code{plotOptimals()} and others in the file \code{optimal-help.R}.

<<packs, echo=TRUE, message=FALSE>>=
library( colorSpec )
source( "optimal-help.R" )

# make vctor of levels to be used for all the plots
Ylevel=c( seq( 0.10, 0.90, by=0.1 ), 0.95 ) 
@


% ----------------------------------------------------------------------------

\section*{Illuminant A}

First, build the "material responder" from Illuminant A and standard CMFs:

\setcounter{figure}{0}  
<<lee10, echo=TRUE, message=FALSE >>=
wave  = seq(380,800,by=5)
A.eye = product( A.1nm, "material", xyz1931.1nm, wavelength=wave )
white = product( neutralMaterial(1,wave=wave), A.eye )
seclist = sectionOptimalColors( A.eye, normal=c(0,1,0), beta=white[2]*Ylevel )
@
Now, make the plot:
<<lee12, echo=TRUE, fig.pos="H", fig.height=6.5, out.width='1.0\\linewidth', fig.cap='MacAdam Limits for Illuminant A' >>=
par( omi=rep(0,4), mai=c(0.5,0.6,0,0) )
plotOptimals( seclist, Ylevel, xyz1931.1nm, white )
@
Compare this with Figure 3(3.7) from \cite{W&S}.



\section*{Illuminant D65}

First, build the "material responder" from Illuminant D65 and standard CMFs:

<<lee20, echo=TRUE, message=FALSE >>=
wave  = seq(380,800,by=5)
D65.eye = product( D65.1nm, "material", xyz1931.1nm, wavelength=wave )
white = product( neutralMaterial(1,wave=wave), D65.eye )
seclist = sectionOptimalColors( D65.eye, normal=c(0,1,0), beta=white[2]*Ylevel )
@
Make the plot:
<<lee22, echo=TRUE, fig.pos="H", fig.height=6.5, out.width='1.0\\linewidth', fig.cap='MacAdam Limits for Illuminant D65' >>=
par( omi=rep(0,4), mai=c(0.5,0.6,0,0) )
plotOptimals( seclist, Ylevel, xyz1931.1nm, white )
@
Compare this with Figure 4(3.7) from \cite{W&S}.






\section*{Illuminant C}

First, build the "material responder" from Illuminant C and standard CMFs:

<<lee40, echo=TRUE, message=FALSE >>=
wave  = seq(380,780,by=5)
C.eye = product( C.5nm, "material", xyz1931.1nm, wavelength=wave )
white = product( neutralMaterial(1,wave=wave), C.eye )
seclist = sectionOptimalColors( C.eye, normal=c(0,1,0), beta=white[2]*Ylevel )
@
Now, make the plot:
<<lee42, echo=TRUE, fig.pos="H", fig.height=6.5, out.width='1.0\\linewidth', fig.cap='MacAdam Limits for Illuminant C' >>=
par( omi=rep(0,4), mai=c(0.5,0.6,0,0) )
plotOptimals( seclist, Ylevel, xyz1931.1nm, white )
@






\section*{An RGB Scanner}

This also works with object color from an electrical RGB scanner.
The chromaticities in this case are:

$$ r = R/(R + G + B) ~~~~~~   g = G/(R + G + B)$$

Make a scanner from a tungsten source and a Flea2 camera:
<<lee30, echo=TRUE, message=FALSE >>=
wave = seq(420,680,by=5)
Flea2.scanner = product( A.1nm, "material", Flea2.RGB, wavelength=wave )
white = product( neutralMaterial(1,wave=wave), Flea2.scanner )
seclist = sectionOptimalColors( Flea2.scanner, normal=c(0,1,0), beta=white[2]*Ylevel )
@
Now, make the plot:
<<lee32, echo=TRUE, fig.pos="H", fig.height=6.5, out.width='1.0\\linewidth', fig.cap='Approximate Output Limits for an RGB scanner' >>=
par( omi=rep(0,4), mai=c(0.5,0.6,0,0) )
plotOptimals( seclist, Ylevel, Flea2.scanner, white )
@
The wavelengths have been trimmed at each end to avoid weak
responsivities that wander around too much.
Even after trimming, the spectrum locus is not convex in the inverval
from about 500 to 560 nm.
This shows that the color solid does \emph{not} satisfy the 2-transition property.
The optimal color chromaticity locii are convex, as they must be;
and some optimal colors are outside the spectrum locus.
See Figure 3 and more discussion in \cite{West}.

% \Sexpr{knitr::knit_exit()}


% \pagebreak

\bibliographystyle{alpha}
\bibliography{optimals}


% ----------------------------------------------------------------------------


\section*{Appendix}
This document was prepared \today \quad with the following configuration:
<<finish, echo=FALSE, results="asis">>=
knit_hooks$set(output = function(x, options) { x })
toLatex(sessionInfo(), locale=FALSE)
@


\end{document}
