%D \module
%D   [     file=t-squares,
%D      version=20240223,
%D        title=\CONTEXT\ User Module,
%D     subtitle=Squares for ConTeXt,
%D       author=Jairo A. del Rio,
%D         date=\currentdate,
%D    copyright=Jairo A. del Rio,
%D        email=jairoadelrio6@gmail.com,
%D      license=MIT License]

%C Copyright (c) 2023-2024 Jairo A. del Rio
%C
%C Permission is hereby granted, free of charge, to any person obtaining
%C a copy of this software and associated documentation files (the
%C "Software"), to deal in the Software without restriction, including
%C without limitation the rights to use, copy, modify, merge, publish,
%C distribute, sublicense, and/or sell copies of the Software, and to
%C permit persons to whom the Software is furnished to do so, subject to
%C the following conditions:
%C
%C The above copyright notice and this permission notice shall be
%C included in all copies or substantial portions of the Software.
%C
%C THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
%C EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
%C MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
%C IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
%C CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
%C TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
%C SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

\writestatus{loading}{Squares for ConTeXt!}

\startmodule[squares]

\registerctxluafile{t-squares}{autosuffix}

\unprotect

% Experiment: simpler setup command handler...

\installnamespace                              {magicsquare}
\installsetuponlycommandhandler\????magicsquare{magicsquare}

\startsetups[\????magicsquare:\c!renderingsetup:\v!color]
\cdef\c_n{\magicsquareparameter\c!n}
\expandedrepeat\numexpr\c_n\relax{
  \expandedrepeat\numexpr\c_n\relax{
    \definecolor[\????magicsquare:\v!color:##P:##I]
      [h=\the\numexpr(360*\clf_magicsquarecell##P##I):(\c_n*\c_n)\relax,v=1,s=0.5]%
    \setupTABLE[##P][##I]
      [\c!background=\v!color,
       \c!backgroundcolor=\????magicsquare:\v!color:##P:##I]%
  }
}
\stopsetups

\setupmagicsquare
  [\c!n=5,
   \c!size=2em,
   \c!align={\v!middle,\v!lohi},
   \c!renderingsetup=\v!none]

\permanent\tolerant\protected\def\magicsquare[#S#parameters]%
  {\begingroup%
   \setupmagicsquare[#parameters]%
   \clf_magicsquareinit\numexpr\magicsquareparameter\c!n\relax%
   \ifempty{\magicsquareparameter\c!renderingsetup}%
   \else
    \directsetup{\????magicsquare:\c!renderingsetup:\magicsquareparameter\c!renderingsetup}%
   \fi%
   \bTABLE
    [#parameters,
     \c!width=\magicsquareparameter\c!size,
     \c!height=\magicsquareparameter\c!size,
     \c!align=\magicsquareparameter\c!align]%
   \clf_magicsquare%
   \eTABLE%
   \clf_magicsquarereset%
   \endgroup}

\installnamespace                              {latinsquare}
\installsetuponlycommandhandler\????latinsquare{latinsquare}

\startsetups[\????latinsquare:\c!renderingsetup:\v!none]
\stopsetups

\startsetups[\????latinsquare:\c!renderingsetup:\v!color]
\expandedrepeat\numexpr\latinsquareparameter\c!n\relax{
  \definecolor[\????latinsquare:\v!color:##I][h=\the\numexpr(360*##I):(\latinsquareparameter\c!n)\relax,v=1,s=0.5]%
  \expandedrepeat\numexpr\latinsquareparameter\c!n\relax{
   \setupTABLE[##P][##I]
    [\c!background=\v!color,
     \c!backgroundcolor=\????latinsquare:\v!color:\clf_latinsquarecell##P##I]%
  }
}
\stopsetups

\setuplatinsquare
  [\c!n=5,
   \c!size=2em,
   \c!align={\v!middle,\v!lohi},
   \c!renderingsetup=\v!none]

\permanent\tolerant\protected\def\latinsquare[#S#parameters]%
  {\begingroup%
   \setuplatinsquare[#parameters]%
   \clf_latinsquareinit\numexpr\latinsquareparameter\c!n\relax%
   \ifempty{\latinsquareparameter\c!renderingsetup}%
   \else
    \directsetup{\????latinsquare:\c!renderingsetup:\latinsquareparameter\c!renderingsetup}%
   \fi%
   \bTABLE
    [#parameters,
     \c!width=\latinsquareparameter\c!size,
     \c!height=\latinsquareparameter\c!size,
     \c!align=\latinsquareparameter\c!align]%
   \clf_latinsquare%
   \eTABLE%
   \clf_latinsquarereset%
   \endgroup}

\protect

\stopmodule

\continueifinputfile{t-squares.mklx}

\starttext

\startsection[title=\type{\magicsquare}]

% Magic squares

\startbuffer
\magicsquare[n=10,renderingsetup=color]
\stopbuffer

\typebuffer

\getbuffer

\startbuffer
\magicsquare[n=3,renderingsetup=color]
\stopbuffer

\typebuffer

\getbuffer

\startbuffer
\magicsquare[n=8,renderingsetup=color]
\stopbuffer

\typebuffer

\getbuffer

\startbuffer
\magicsquare[n=2]
\stopbuffer

\typebuffer

\getbuffer

\startbuffer
\magicsquare
\stopbuffer

\typebuffer

\getbuffer

\stopsection

% Latin squares

\startsection[title=\type{\latinsquare}]

\startbuffer
\latinsquare[n=7,renderingsetup=color]
\stopbuffer

\typebuffer

\getbuffer

\startbuffer
\latinsquare[n=5,renderingsetup=color]
\stopbuffer

\typebuffer

\getbuffer

\stopsection
\stoptext

% vim: tw=80:ts=2:sts=2:expandtab:wrap:linebreak:breakindent