%%%%%%****  Efficient definition or colour for PDF, either RGB or gray  ****%%%%%%

% 2026-sep-23: Made \ignorespaces at the end of the four \Pdf***Raw commands configurable

\let\ignorespacesaftercolor=\relax %By default, do not ignore following spaces

%% Use like this:
%
% \DefGray{gray4}{0.33}
% \DefColor{paleblue}{0.16, 0.16, 0.36}
% \hbox{\color{gray4} Text in gray}
% \hbox{\color{paleblue} Paleblue}
%
%\PdfFill and \PdfGrayFill only set rg, not RG, so they are slightly faster and shorter.
%
% You can use a colour without defiting it:
% \hbox{\PdfGrayRaw{0.2} Text in dark gray}
% \hbox{\PdfColorRaw{0, 0, .55} Text in blue}

\ifdefined\pdfextension
  \unless\ifdefined\pdfliteral \protected\def\pdfliteral{\pdfextension literal}\fi
  \unless\ifdefined\pdfcolorsktack \def\pdfcolorstack{\pdfextension colorstack}\fi
\fi

\count255=\catcode`@
\catcode`@=11

\def\DefColor #1#2{\DefColorRaw{#1}#2\relax}
\def\DefColorRaw #1#2,#3,#4\relax{\exP\def\csname pdfcolor@#1\endcsname{#2 #3 #4}}
\def\DefGray #1#2{\DefColorRaw{#1}#2,#2,#2\relax \exP\def\csname pdfgray@#1\endcsname{#2}}
\def\popcolorstack{\pdfcolorstack0 pop}

%We make this two-step definition so that \PdfFill and \PdfColor can be expanded inside an edef, for optimization
\protected\def\PdfFillRaw#1{\pdfcolorstack0 push{#1 rg}\aftergroup\popcolorstack\ignorespacesaftercolor}% Gliphs are just fill.
\def\PdfFill#1{\exP\PdfFillRaw\exP{\expanded{\csname pdfcolor@#1\endcsname}}}
%
\protected\def\PdfColorRaw#1{\pdfcolorstack0 push{#1 rg #1 RG}\aftergroup\popcolorstack\ignorespacesaftercolor}% But rules may not.
\def\PdfColor#1{\exP\PdfColorRaw\exP{\expanded{\csname pdfcolor@#1\endcsname}}}
%
\protected\def\PdfGFillRaw#1{\pdfcolorstack0 push{#1 g}\aftergroup\popcolorstack\ignorespacesaftercolor}
\def\PdfGrayFill#1{\exP\PdfGFillRaw\exP{\expanded{\csname pdgray@#1\endcsname}}}
%
\protected\def\PdfGrayRaw#1{\pdfcolorstack0 push{#1 g G}\aftergroup\popcolorstack\ignorespacesaftercolor}
\def\PdfGrayColor#1{\exP\PdfGrayRaw\exP{\expanded{\csname pdfgray@#1\endcsname}}}

\catcode`@=\count255

\let\color\PdfColor
\let\colorraw\PdfColorRaw

%Define some colours
\DefGray{white}{1}
\DefGray{black}{0}
\DefColor{blue}{0,0,1}
\DefColor{green}{0,1,0}
\DefColor{red}{1,0,0}