%D \module
%D   [       file=colo-run,
%D        version=1997.04.01,
%D          title=\CONTEXT\ Color Macros,
%D       subtitle=Runtime loaded commands,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

\registerctxluafile{colo-run}{}

\unprotect

%D Colors

% These are nicer in \LUA\ code than in \TEX\ code but not more
% efficient.

\unexpanded\gdef\showcolorbar[#1]%
  {\backgroundline[#1]{\strut\enspace\color[white]{white}\enspace\color[black]{black}\enspace}}

\unexpanded\gdef\showcolor          {\dosingleempty\colo_basics_show}
\unexpanded\gdef\showcolorset       {\dosingleempty\colo_basics_show_set}
\unexpanded\gdef\showcolorcomponents{\dosingleempty\colo_basics_show_components}

\gdef\colo_basics_show_set       [#1]{\ctxcommand{showcolorset("#1")}}
\gdef\colo_basics_show_components[#1]{\ctxcommand{showcolorcomponents("#1")}}

\gdef\colo_basics_show[#1]% % we do this at the tex end as loading happens
  {\usecolors   [#1]%  % delayed and we can only ask for the set if
   \showcolorset[#1]}  % loading has happened

% The rest might also be done in \LUA\ although there is not much
% beauty to gain here (too much typesetting specific). Also, these
% macros are pretty old and hardly used so best keep them as they
% are.

%D Palets

\unexpanded\gdef\showpalet
  {\dodoubleargument\colo_show_palet}

\gdef\colo_show_palet[#1][#2]%
  {\doifelsecolorpalet{#1}
     {\doifelseinset\v!vertical{#2} \colo_palets_show_vertical \colo_palets_show_horizontal {#1}{#2}}%
     {}}

\gdef\colo_palets_show_vertical#1#2%
  {\begingroup
   \forgetall
   \setlocalhsize
   \hsize\localhsize
   \offinterlineskip
   \setuppalet[#1]
   \tabskip\zeropoint
   \def\colo_palets_show_palet##1%
     {\doifelseinset\v!number{#2}{##1\hskip.5\emwidth}{}%
      \aligntab
      \color    [##1]{\vrule\s!width3\emwidth\s!height\strutht\s!depth\strutdp}%
      \graycolor[##1]{\vrule\s!width3\emwidth\s!height\strutht\s!depth\strutdp}%
      \aligntab
      \doifinset\v!value{#2}{\hskip.5\emwidth\colorvalue{##1}}%
      \crcr}
   \halign
     {\hss\alignmark\alignmark\aligntab\hss\alignmark\alignmark\hss\aligntab\alignmark\alignmark\cr
      \aligntab\doifinset\v!name{#2}{\strut#1}\aligntab\cr
      \colo_palets_process[#1]\colo_palets_show_palet\crcr}%
  \endgroup}

\gdef\colo_palets_show_horizontal#1#2% todo: bTABLE etc
  {\vbox
     {\forgetall
      \setlocalhsize
      \hsize\localhsize
      \offinterlineskip
      \setuppalet[#1]
      \tabskip\zeropoint
      \scratchwidth\zeropoint
      \doifinset\v!number{#2}
        {\def\colo_palets_show_palet##1%
           {\setbox\scratchbox\hbox{##1}%
            \ifdim\scratchwidth<\wd\scratchbox
              \scratchwidth\wd\scratchbox
            \fi}%
         \colo_palets_process[#1]\colo_palets_show_palet}%
      \advance\scratchwidth\emwidth
      \ifdim\scratchwidth<5\emwidth
        \scratchwidth5\emwidth
      \fi
      \halign
        {\alignmark\alignmark\aligntab\aligntab\hbox to \scratchwidth{\hss\alignmark\alignmark\hss}\cr
         \doifinset\v!number{#2}
           {\def\colo_palets_show_palet##1{\aligntab\strut##1}%
            \colo_palets_process[#1]\colo_palets_show_palet}\cr
         \doifinset\v!name{#2}{#1\hskip.5\emwidth}%
         \def\colo_palets_show_palet##1%
           {\aligntab\strut\color[##1]{\vrule\s!width\scratchwidth\s!height\strutht\s!depth\zeropoint}}%
         \colo_palets_process[#1]\colo_palets_show_palet\crcr
         \noalign{\vskip-\strutdepth}%
         \def\colo_palets_show_palet##1%
           {\aligntab\graycolor[##1]{\vrule\s!width\scratchwidth\s!height\zeropoint\s!depth\strutdp}}%
         \colo_palets_process[#1]\colo_palets_show_palet\crcr
         \doifinset\v!value{#2}
           {\def\colo_palets_show_palet##1%
             {\aligntab\vbox
                {\hsize\scratchwidth
                 \vskip.25\exheight
                 \everypar{\strut}
                 \veryraggedcenter
                 \let\colorformatseparator\endgraf
                 \colorvalue{##1}}}%
            \colo_palets_process[#1]\colo_palets_show_palet}%
         \crcr}}}

\gdef\colo_palets_process[#1]%
  {\getpaletlist[#1]%
   \normalexpanded{\globalprocesscommalist[\paletlist]}}

\unexpanded\gdef\comparepalet
  {\dosingleargument\colo_palets_compare}

\gdef\colo_palets_compare[#1]%
  {\doifelsecolorpalet{#1}%
     {\hbox
        {\colo_palets_compare_indeed\color{#1}%
         \quad
         \colo_palets_compare_indeed\graycolor{#1}}}%
     {}}

\gdef\colo_palets_compare_indeed#1#2%
  {\vbox
     {\forgetall
      \setlocalhsize
      \hsize\localhsize
      \offinterlineskip
      \setuppalet[#2]
      \getpaletsize[#2]%
      \getpaletlist[#2]%
      \scratchwidth2\emwidth\relax
      \hsize\paletsize\scratchwidth
      \def\colo_palets_compare##1%
        {\hbox
           {\setbox0\hbox
              {#1[##1]{\vrule\s!width\hsize\s!height3ex}}%
            \wd0\zeropoint
            \box0
            \hbox to \hsize
              {\def\colo_palets_compare####1%
                 {\hbox to \scratchwidth
                    {\hss#1[####1]{\vrule\s!width.5\scratchwidth\s!height2.25\exheight\s!depth-.75\exheight}\hss}}%
               \processcommacommand[\paletlist]\colo_palets_compare}}
         \endgraf}
      \processcommacommand[\paletlist]\colo_palets_compare}}

%D Groups

\unexpanded\gdef\showcolorgroup
  {\dodoubleargument\colo_groups_show}

\gdef\colo_groups_show[#1][#2]%
  {\doifcolor{#1:1}
     {\doifelseinset\v!vertical{#2}
        \colo_groups_show_vertical
        \colo_groups_show_horizontal
        [#1][#2]}}

\gdef\colo_groups_show_horizontal[#1][#2]%
  {\vbox
     {\forgetall
      \setlocalhsize
      \hsize\localhsize
      \offinterlineskip
      \setuppalet
      \tabskip\zeropoint
      \def\colorformatseparator{\strut\cr}
      \def\colo_groups_show_group##1%
        {\doifcolor{#1:##1}{\vbox
           {\halign
              {\hss\alignmark\alignmark\alignmark\alignmark\hss\cr
               \doifinset\v!number{#2}{\strut##1}\cr
               \color    [#1:##1]{\vrule\s!width4\emwidth\s!height\strutht  \s!depth\zeropoint}\cr
               \graycolor[#1:##1]{\vrule\s!width4\emwidth\s!height\zeropoint\s!depth\strutdp  }\cr
               \doifinset\v!value{#2}{\colorvalue{#1:##1}\strut}\crcr}}}}%
      \hbox
        {\doifinset\v!name{#2}
           {\strut
            \doifelseinset\v!value{#2}
              {\raise3\lineheight\hbox{#1\hskip.5\emwidth}}
              {#1}%
            \hskip.5\emwidth}%
         \colo_groups_show_group1\colo_groups_show_group2\colo_groups_show_group3\colo_groups_show_group4%
         \colo_groups_show_group5\colo_groups_show_group6\colo_groups_show_group7\colo_groups_show_group8}}}

\gdef\colo_groups_show_vertical[#1][#2]%
  {\vbox
     {\forgetall
      \setlocalhsize
      \hsize\localhsize
      \offinterlineskip
      \setuppalet
      \tabskip\zeropoint
      \def\colo_groups_show_group##1%
        {\doifcolor{#1:##1}
           {\doifinset\v!number{#2}
              {##1\hskip.5\emwidth}%
            \aligntab
            \color    [#1:##1]{\vrule\s!width2.5\emwidth\s!height\strutht\s!depth\strutdp}%
            \graycolor[#1:##1]{\vrule\s!width2.5\emwidth\s!height\strutht\s!depth\strutdp}
            \aligntab
            \doifinset\v!value{#2}{\hskip.5\emwidth\colorvalue{#1:##1}}%
            \crcr}}%
      \halign
        {\hss\alignmark\alignmark\aligntab\hss\alignmark\alignmark\hss\aligntab\alignmark\alignmark\hss\cr
         \aligntab\doifinset\v!name{#2}{\strut#1}\aligntab\crcr
         \colo_groups_show_group1\colo_groups_show_group2\colo_groups_show_group3\colo_groups_show_group4%
         \colo_groups_show_group5\colo_groups_show_group6\colo_groups_show_group7\colo_groups_show_group8}}}

\unexpanded\gdef\comparecolorgroup
  {\dosingleargument\colo_groups_compare}

\gdef\colo_groups_compare[#1]%
  {\doifcolor{#1:1}
     {\hbox
        {\colo_groups_compare_indeed\color    [#1]\quad
         \colo_groups_compare_indeed\graycolor[#1]}}}

\gdef\colo_groups_compare_indeed#1[#2]%
  {\vbox
     {\forgetall
      \setlocalhsize
      \hsize\localhsize
      \scratchcounter\zerocount
      \dorecurse{15}{\doifcolor{#2:\recurselevel}{\advance\scratchcounter\plusone}}
      \scratchwidth2\emwidth\relax
      \hsize\scratchcounter\scratchwidth
      \dorecurse\scratchcounter{\colo_groups_compare_step{#1}{#2}\recurselevel}}}

\def\colo_groups_compare_step#1#2#3%
  {\hbox to \hsize
     {\setbox\scratchbox\hbox
        {#1[#2:#3]{\vrule\s!width\hsize\s!height3\exheight}}%
      \wd\scratchbox\zeropoint
      \box\scratchbox
      \hbox to \hsize
        {\hss
         \dorecurse\scratchcounter
            {#1[#2:\recurselevel]{\vrule\s!width.5\scratchwidth\s!height2.25\exheight\s!depth-.75\exheight}%
         \hss}}}
   \endgraf}

\protect \endinput
