%%
%% This is file `pst-cie.tex',
%%
%% IMPORTANT NOTICE:
%%
%% Package `pst-cie.tex'
%%
%% Manuel Luque <Manuel.Luque27 _at_ gmail.com
%% Herbert Voss <hvoss _at_ tug.org>
%%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
%%
%% DESCRIPTION:
%%   `pst-cie' is a PSTricks package to draw CIE color space
%%   with a Planck' locus
%%
\csname PSTcieLoaded\endcsname
\let\PSTcieLoaded\endinput
\ifx\PSTricksLoaded\endinput\else \input pstricks.tex\fi
\ifx\PSTplotLoaded \endinput\else \input pst-plot.tex\fi
\ifx\PSTnodesLoaded\endinput\else \input pst-node \fi
%\ifx\PSTXKeyLoaded\endinput\else    \input pst-xkey \fi
%
\def\fileversion{1.06a}
\def\filedate{2018/12/16}
\message{`PST-cie' v\fileversion, \filedate\space (hv)}
%
\edef\PstAtCode{\the\catcode`\@} \catcode`\@=11\relax
\pst@addfams{pst-cie}
\pstheader{pst-cie.pro}
%
\define@key[psset]{pst-cie}{gamma}[2.2]{\def\psk@psCIE@GAM{#1 }}
\define@key[psset]{pst-cie}{contrast}[1]{\def\psk@psCIE@contrast{#1 }}
\define@key[psset]{pst-cie}{chromaticityCoordinates}[xy]{\def\psk@psCIE@chromaticityCoordinates{#1}}
\define@key[psset]{pst-cie}{bgcolor}[black]{\pst@getcolor{#1}\pst@psCIE@bgcolor}
\define@key[psset]{pst-cie}{textcolor}[white]{\pst@getcolor{#1}\pst@psCIE@textcolor}
\define@key[psset]{pst-cie}{trianglecolor}[black]{\pst@getcolor{#1}\pst@psCIE@trianglecolor}
\psset[pst-cie]{gamma=2.2,contrast=1,bgcolor=black,textcolor=white,trianglecolor=black}
\define@boolkey[psset]{pst-cie}[Pst@]{primaries}[true]{}
\define@key[psset]{pst-cie}{Tfontsize}{\pst@getlength{#1}\psk@Tfontsize}
\define@boolkey[psset]{pst-cie}[Pst@]{Planck}[true]{}
\define@boolkey[psset]{pst-cie}[Pst@]{showcontour}[true]{}
\psset[pst-cie]{primaries,Planck=false,showcontour=false,Tfontsize=4pt,chromaticityCoordinates={xy}}
% primary illuminants
% systems = Adobe, CIE, SMPTE, sRGB,Pal-Secam, NTSC
% #1=type de reservation (action, object...) associee a une liste qui
% doit forcement etre du type pst@cie@list@#1
\def\pst@cie@reservednames#1{%
  \edef\@tempa{\csname pst@cie@list@#1\endcsname}
  \@for \name:=\@tempa\do{\expandafter\def\csname pst@cie@#1@\name\endcsname{}}}
% système
\def\txCIEdictBegin{ tx@CIEdict begin }
%
\def\pst@cie@list@ColorSpace{Adobe,CIE,ColorMatch,NTSC,Pal-Secam,ProPhoto,SMPTE,sRGB}
%% Reservation de macros correspondants aux systemes
\pst@cie@reservednames{ColorSpace}
\define@key[psset]{pst-cie}{ColorSpace}{%
  \ifcsname pst@cie@ColorSpace@#1\endcsname
    \def\pst@cie@ColorSpace{#1}%
  \else
    \@pstrickserr{This color space is not authorized, sRGB is
    chosen by default}{}%
  \fi}
\psset[pst-cie]{ColorSpace=sRGB}

% datas
\def\pst@cie@list@datas{CIE1964,CIE1931}
%% Reservation de macros correspondants aux datas
\pst@cie@reservednames{datas}
\define@key[psset]{pst-cie}{datas}{%
  \ifcsname pst@cie@datas@#1\endcsname
    \def\pst@cie@datas{#1}%
  \else
    \@pstrickserr{This datas is not authorized, CIE1931 is chosen by default}{}%
  \fi}
\psset[pst-cie]{datas=CIE1931}
\def\CIEdefaultYear{CIE1931}
%
\def\psChromaticityDiagram{\def\pst@par{}\pst@object{psChromaticityDiagram}}
\def\psChromaticityDiagram@i{%
  \begin@SpecialObj
%\pstVerb{\ifPst@primaries /primaries {true} def \else /primaries {false} def \fi}%
  \addto@pscode{
    \ifPst@primaries /primaries {true} def \else /primaries {false} def \fi
    /gamma \psk@psCIE@GAM def
    /contrast \psk@psCIE@contrast def
    /ColorSpace (\pst@cie@ColorSpace) def
    /datas (\pst@cie@datas) def
    /chromaticityCoordinates (\psk@psCIE@chromaticityCoordinates) def
    /cie-bgcolor { \pst@usecolor\pst@psCIE@bgcolor } def
    /cie-textcolor { \pst@usecolor\pst@psCIE@textcolor } def
    /cie-unit \pst@number\psunit 10 mul def
    /cie-U \pst@number\psunit 28.45274 div def
    \txCIEdictBegin SCREEN {datas cvx exec}
     {ColorSpace cvx exec} gamma contrast traceGamut end
  }%
  \ifPst@Planck\gdef\ps@next{\pstPlanck}\else\gdef\ps@next{}\fi%  options are local
  \end@SpecialObj
  \ps@next
}%
%
\def\pstCIEcontour{\pst@object{pstCIEcontour}}%
\def\pstCIEcontour@i{%
 \begin@ClosedObj
  \addto@pscode{
    \ifPst@primaries /primaries {true} def \else /primaries {false} def \fi
    /gamma \psk@psCIE@GAM def
    /contrast \psk@psCIE@contrast def
    /ColorSpace (\pst@cie@ColorSpace) def
    /datas (\pst@cie@datas) def
    /chromaticityCoordinates (\psk@psCIE@chromaticityCoordinates) def
    /cie-bgcolor { \pst@usecolor\pst@psCIE@bgcolor } def
    /cie-textcolor { \pst@usecolor\pst@psCIE@textcolor } def
    /cie-trianglecolor { \pst@usecolor\pst@psCIE@trianglecolor } def
    /cie-unit \pst@number\psunit 10 mul def
    /cie-U cie-unit 28.45274 div def
    \txCIEdictBegin {datas cvx exec} /TAB exch def ColorSpace cvx exec %/system exch def
     chromaticityCoordinates (xy) ne {/Tabuv [
    0 1 TAB length 1 sub {/i exch def
     TAB i get /subarray exch def
     /X subarray 0 get def /Y subarray 1 get def /Z 1 X sub Y sub def
%     [4 X mul X 15 Y mul add 3 Z mul add div   % u
%      6 Y mul X 15 Y mul add 3 Z mul add div]  % v
     [4 X mul X 15 Y mul add 3 Z mul add div    % u'
      9 Y mul X 15 Y mul add 3 Z mul add div]   % v'
      } for
     ] def
     /TAB Tabuv def
     } if
%    system
    gsave primaries {CouleursPrimaires} if grestore
    TAB contour stroke
    contourTriangle cie-trianglecolor stroke
   end
  }%
 \end@ClosedObj
}
%    \txCIEdictBegin \ifx\pst@cie@datas\CIEdefaultYear CIEXYZ_1931_table \else CIEXYZ_1964_table \fi
%
\def\pstPlanck{\pst@object{pstPlanck}}%
\def\pstPlanck@i{%
  \begingroup
  \use@par
  \font\CMSS=cmss10 at \psk@Tfontsize pt
  \ifPst@showcontour\pstCIEcontour\fi
  % lieu des points blancs (Planckian locus)
  \parametricplot[plotpoints=3000]{1000}{500000}{
    \txCIEdictBegin t \ifx\pst@cie@datas\CIEdefaultYear CIEXYZ_1931_table \else CIEXYZ_1964_table \fi
    BLKBOD Y X X Y Z end
    add add /S ED S div 10 mul exch S div 10 mul }%
  \parametricplot[plotpoints=2,arrows=-|]{500000}{502000}{
    \txCIEdictBegin t \ifx\pst@cie@datas\CIEdefaultYear CIEXYZ_1931_table \else CIEXYZ_1964_table \fi
    BLKBOD Y X X Y Z end
    add add /S ED S div 10 mul exch S div 10 mul }%
  % point de concours des normales : R.SÃ¨ve : physique de la couleur (.332,.186)
  %\psdot[linecolor=red](3.32,1.86)
  \pnode(3.320,1.858){O}%
  \psforeach{\iA}{5000,7000,8000,9000,11000,12000,13000}{%
    \pnode(!
      \txCIEdictBegin \iA\space \ifx\pst@cie@datas\CIEdefaultYear CIEXYZ_1931_table \else CIEXYZ_1964_table \fi
      BLKBOD Y X X Y Z end
      add add /S ED
      S div 10 mul exch S div 10 mul){D\iA}%
    \psRelNode(O)(D\iA){0.96}{d0}\psRelNode(O)(D\iA){1.04}{d1}%
    \psline(d0)(d1)%
  }%
  \psforeach{\iA}{1500,2000,2500,3000,4000,6000,10000}{%
    \pnode(!
      \txCIEdictBegin \iA\space
      \ifx\pst@cie@datas\CIEdefaultYear CIEXYZ_1931_table \else CIEXYZ_1964_table \fi
      BLKBOD Y X X Y Z end
      add add /S ED
      S div 10 mul exch S div 10 mul){D\iA}%
    \psRelNode(O)(D\iA){1 3.2e-5 \iA\space mul sub}{d0}\psRelNode(O)(D\iA){1 3.2e-5 \iA\space mul add}{d1}%
    \pcline(d0)(d1)\ncput[npos=1.15]{\CMSS\iA}%
  }%
  \uput[-125](!
    1e5
    \txCIEdictBegin \ifx\pst@cie@datas\CIEdefaultYear CIEXYZ_1931_table \else CIEXYZ_1964_table \fi
    BLKBOD Y X X Y Z end
    add add /S ED
    S div 10 mul exch S div 10 mul){$\scriptstyle\infty$}%
  \endgroup
}
%
\catcode`\@=\PstAtCode\relax
\endinput
%% END pst-cie.tex \def\pstCIEcontour{\pst@object{pstCIEcontour}}%
