% Author        : C. Pierquet
% licence       : Released under the LaTeX Project Public License v1.3c or later, see http://www.latex-project.org/lppl.txt
% licence svg   : CC BY-SA 3.0 https://commons.wikimedia.org/wiki/File:Judo_yellow_belt.svg
%               : Tks to Sascha Christmann for "in betweeen" colors

\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{coloredbelts}{2026-02-20}{0.20a}{Colored Judo's belts}

%====HISTORIQUE
% v 0.20a  Enhancements with latex3 !!
% v 0.1.6  Enhancements + tikz version
% v 0.1.5  Passage en LaTeX3
% v 0.1.4  Option strut
% v 0.1.3  Uniformisation des fichiers
% v 0.1.2  Amélioration de la gestion des longueurs
% v 0.1.1  Couleurs duo
% v 0.1.0  Version initiale

%====BASE
\RequirePackage { graphicx }
\RequirePackage { tikz }

%====DIMENSIONS  (toutes \l_ = locales, scoped par \group_begin:/\group_end:)
\dim_new:N \l_judobelt_totheight_dim
\dim_new:N \l_judobelt_depth_dim
\dim_new:N \l_judobeltraise_dim
\dim_new:N \l_height_colored_belts_dim
\dim_new:N \l_desired_height_colored_belts_dim
\dim_new:N \l_depth_colored_belts_dim

%====CONSTANTE : hauteur de référence du dessin tikz (fixe, jamais modifiée)
\dim_new:N \c_judobelt_tikzheight_dim
\dim_set:Nn \c_judobelt_tikzheight_dim { 5cm }

%====VARIABLES
\tl_new:N \l_judobeltheight_tl
\tl_new:N \l_judobeltstrut_tl
\tl_new:N \l_judobeltcolor_tl
\tl_new:N \l_judobeltcolorcolor_tl
\tl_new:N \l_judobeltcolorborder_tl
\fp_new:N \l_scale_factor_fp

%====MACRO DE RESET (FR + tikz partagent les mêmes variables)
\cs_new_protected:Nn \__coloredbelts_reset_fr:
  {
    \tl_set:Nn \l_judobeltheight_tl    { auto }
    \dim_set:Nn \l_judobeltraise_dim   { 0pt }
    \tl_set:Nn \l_judobeltstrut_tl
      { abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ }
  }

\cs_new_protected:Nn \__coloredbelts_reset_en:
  {
    \tl_set:Nn \l_judobeltheight_tl    { auto }
    \dim_set:Nn \l_judobeltraise_dim   { 0pt }
    \tl_set:Nn \l_judobeltstrut_tl
      { abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ }
  }

\cs_new_protected:Nn \__coloredbelts_reset_tikz:
  {
    \dim_set:Nn \l_height_colored_belts_dim  { 2cm }
    \tl_set:Nn \l_judobeltstrut_tl
      { abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ }
    \tl_set:Nn \l_judobeltcolorborder_tl     { black }
  }

%====CLÉS (version française)
\keys_define:nn { ceinturejudo }
  {
    Hauteur .tl_set:N  = \l_judobeltheight_tl,
    Hauteur .initial:n = { auto },
    Hauteur .default:n = { auto },
    DecalV  .dim_set:N = \l_judobeltraise_dim,
    DecalV  .initial:n = { 0pt },
    DecalV  .default:n = { 0pt },
    Strut   .tl_set:N  = \l_judobeltstrut_tl,
    Strut   .initial:n = { abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ },
  }

%====COMMANDE FR
\NewDocumentCommand \CeintureCouleur { s O{} m }
  {
    \group_begin:
    % Gestion des couleurs
    \str_case:enF { #3 }
      {
        {white}         { \tl_set:Nn \l_judobeltcolorcolor_tl { white }        }
        {blanc}         { \tl_set:Nn \l_judobeltcolorcolor_tl { white }        }
        {jaune}         { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow }       }
        {yellow}        { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow }       }
        {orange}        { \tl_set:Nn \l_judobeltcolorcolor_tl { orange }       }
        {red}           { \tl_set:Nn \l_judobeltcolorcolor_tl { red }          }
        {rouge}         { \tl_set:Nn \l_judobeltcolorcolor_tl { red }          }
        {bleu}          { \tl_set:Nn \l_judobeltcolorcolor_tl { blue }         }
        {blue}          { \tl_set:Nn \l_judobeltcolorcolor_tl { blue }         }
        {purple}        { \tl_set:Nn \l_judobeltcolorcolor_tl { purple }       }
        {violet}        { \tl_set:Nn \l_judobeltcolorcolor_tl { purple }       }
        {marron}        { \tl_set:Nn \l_judobeltcolorcolor_tl { brown }        }
        {brown}         { \tl_set:Nn \l_judobeltcolorcolor_tl { brown }        }
        {green}         { \tl_set:Nn \l_judobeltcolorcolor_tl { green }        }
        {vert}          { \tl_set:Nn \l_judobeltcolorcolor_tl { green }        }
        {rose}          { \tl_set:Nn \l_judobeltcolorcolor_tl { pink }         }
        {pink}          { \tl_set:Nn \l_judobeltcolorcolor_tl { pink }         }
        {gris}          { \tl_set:Nn \l_judobeltcolorcolor_tl { gray }         }
        {gray}          { \tl_set:Nn \l_judobeltcolorcolor_tl { gray }         }
        {noir}          { \tl_set:Nn \l_judobeltcolorcolor_tl { black }        }
        {black}         { \tl_set:Nn \l_judobeltcolorcolor_tl { black }        }
        {white-yellow}  { \tl_set:Nn \l_judobeltcolorcolor_tl { white-yellow } }
        {blanc-jaune}   { \tl_set:Nn \l_judobeltcolorcolor_tl { white-yellow } }
        {yellow-orange} { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow-orange} }
        {jaune-orange}  { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow-orange} }
        {orange-green}  { \tl_set:Nn \l_judobeltcolorcolor_tl { orange-green } }
        {orange-vert}   { \tl_set:Nn \l_judobeltcolorcolor_tl { orange-green } }
        {green-blue}    { \tl_set:Nn \l_judobeltcolorcolor_tl { green-blue }   }
        {vert-bleu}     { \tl_set:Nn \l_judobeltcolorcolor_tl { green-blue }   }
        {purple-brown}  { \tl_set:Nn \l_judobeltcolorcolor_tl { purple-brown } }
        {violet-marron} { \tl_set:Nn \l_judobeltcolorcolor_tl { purple-brown } }
        {blue-brown}    { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-brown }   }
        {bleu-marron}   { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-brown }   }
        {blue-purple}   { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-purple }  }
        {bleu-violet}   { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-purple }  }
        {brown-black}   { \tl_set:Nn \l_judobeltcolorcolor_tl { brown-black }  }
        {marron-noir}   { \tl_set:Nn \l_judobeltcolorcolor_tl { brown-black }  }
      }
      { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow } }
    % Étoilée := includegraphics simple (options passées directement à graphicx)
    \bool_if:NTF #1
      {
        \includegraphics[#2]{ judobelt-\l_judobeltcolorcolor_tl.pdf }
      }
      {
        %--- Reset puis options utilisateur
        \__coloredbelts_reset_fr:
        \keys_set:nn { ceinturejudo } { #2 }
        \str_if_eq:eeTF { \tl_to_str:N \l_judobeltheight_tl } { auto }
          {
            % Hauteur auto : calée sur le strut courant
            \hbox_set:Nn \l_tmpa_box { \tl_use:N \l_judobeltstrut_tl }
            \dim_set:Nn \l_judobelt_totheight_dim
                { \box_dp:N \l_tmpa_box + \box_ht:N \l_tmpa_box }
            \dim_set:Nn \l_judobelt_depth_dim { \box_dp:N \l_tmpa_box }
            \dim_set:Nn \l_judobeltraise_dim
              {
                -\l_judobelt_depth_dim
                + \fp_eval:n
                  { 0.025 * \dim_to_decimal_in_unit:nn { \l_judobelt_totheight_dim } { 1pt } }
                pt
              }
            \raisebox { \dim_use:N \l_judobeltraise_dim }
              {
                \includegraphics
                  [ height = { \dim_eval:n { 0.95\l_judobelt_totheight_dim } } ]
                  { judobelt-\l_judobeltcolorcolor_tl.pdf }
              }
          }
          {
            % Hauteur manuelle
            \raisebox { \dim_use:N \l_judobeltraise_dim }
              {
                \includegraphics
                  [ height = { \l_judobeltheight_tl } ]
                  { judobelt-\l_judobeltcolorcolor_tl.pdf }
              }
          }
      }
    \group_end:
  }

%====CLÉS (version anglaise)
\keys_define:nn { judobelt }
  {
    Height  .tl_set:N  = \l_judobeltheight_tl,
    Height  .initial:n = { auto },
    Height  .default:n = { auto },
    OffsetV .dim_set:N = \l_judobeltraise_dim,   % fix: pointe désormais sur \l_
    OffsetV .initial:n = { 0pt },
    OffsetV .default:n = { 0pt },
    Strut   .tl_set:N  = \l_judobeltstrut_tl,
    Strut   .initial:n = { abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ },
  }

%====COMMANDE EN
\NewDocumentCommand \ColorBelt { s O{} m }
  {
    \group_begin:
    % Color management
    \str_case:enF { #3 }
      {
        {white}         { \tl_set:Nn \l_judobeltcolorcolor_tl { white }        }
        {blanc}         { \tl_set:Nn \l_judobeltcolorcolor_tl { white }        }
        {jaune}         { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow }       }
        {yellow}        { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow }       }
        {orange}        { \tl_set:Nn \l_judobeltcolorcolor_tl { orange }       }
        {red}           { \tl_set:Nn \l_judobeltcolorcolor_tl { red }          }
        {rouge}         { \tl_set:Nn \l_judobeltcolorcolor_tl { red }          }
        {bleu}          { \tl_set:Nn \l_judobeltcolorcolor_tl { blue }         }
        {blue}          { \tl_set:Nn \l_judobeltcolorcolor_tl { blue }         }
        {purple}        { \tl_set:Nn \l_judobeltcolorcolor_tl { purple }       }
        {violet}        { \tl_set:Nn \l_judobeltcolorcolor_tl { purple }       }
        {marron}        { \tl_set:Nn \l_judobeltcolorcolor_tl { brown }        }
        {brown}         { \tl_set:Nn \l_judobeltcolorcolor_tl { brown }        }
        {green}         { \tl_set:Nn \l_judobeltcolorcolor_tl { green }        }
        {vert}          { \tl_set:Nn \l_judobeltcolorcolor_tl { green }        }
        {rose}          { \tl_set:Nn \l_judobeltcolorcolor_tl { pink }         }
        {pink}          { \tl_set:Nn \l_judobeltcolorcolor_tl { pink }         }
        {gris}          { \tl_set:Nn \l_judobeltcolorcolor_tl { gray }         }
        {gray}          { \tl_set:Nn \l_judobeltcolorcolor_tl { gray }         }
        {noir}          { \tl_set:Nn \l_judobeltcolorcolor_tl { black }        }
        {black}         { \tl_set:Nn \l_judobeltcolorcolor_tl { black }        }
        {white-yellow}  { \tl_set:Nn \l_judobeltcolorcolor_tl { white-yellow } }
        {blanc-jaune}   { \tl_set:Nn \l_judobeltcolorcolor_tl { white-yellow } }
        {yellow-orange} { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow-orange} }
        {jaune-orange}  { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow-orange} }
        {orange-green}  { \tl_set:Nn \l_judobeltcolorcolor_tl { orange-green } }
        {orange-vert}   { \tl_set:Nn \l_judobeltcolorcolor_tl { orange-green } }
        {green-blue}    { \tl_set:Nn \l_judobeltcolorcolor_tl { green-blue }   }
        {vert-bleu}     { \tl_set:Nn \l_judobeltcolorcolor_tl { green-blue }   }
        {purple-brown}  { \tl_set:Nn \l_judobeltcolorcolor_tl { purple-brown } }
        {violet-marron} { \tl_set:Nn \l_judobeltcolorcolor_tl { purple-brown } }
        {blue-brown}    { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-brown }   }
        {bleu-marron}   { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-brown }   }
        {blue-purple}   { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-purple }  }
        {bleu-violet}   { \tl_set:Nn \l_judobeltcolorcolor_tl { blue-purple }  }
        {brown-black}   { \tl_set:Nn \l_judobeltcolorcolor_tl { brown-black }  }
        {marron-noir}   { \tl_set:Nn \l_judobeltcolorcolor_tl { brown-black }  }
      }
      { \tl_set:Nn \l_judobeltcolorcolor_tl { yellow } }
    % Starred := simple includegraphics (options passées directement à graphicx)
    \bool_if:NTF #1
      {
        \includegraphics[#2]{ judobelt-\l_judobeltcolorcolor_tl.pdf }
      }
      {
        %--- Reset puis options utilisateur
        \__coloredbelts_reset_en:
        \keys_set:nn { judobelt } { #2 }
        \str_if_eq:eeTF { \tl_to_str:N \l_judobeltheight_tl } { auto }
          {
            % Hauteur auto : calée sur le strut courant
            \hbox_set:Nn \l_tmpa_box { \tl_use:N \l_judobeltstrut_tl }
            \dim_set:Nn \l_judobelt_totheight_dim
                { \box_dp:N \l_tmpa_box + \box_ht:N \l_tmpa_box }
            \dim_set:Nn \l_judobelt_depth_dim { \box_dp:N \l_tmpa_box }
            \dim_set:Nn \l_judobeltraise_dim
              {
                -\l_judobelt_depth_dim
                + \fp_eval:n
                  { 0.025 * \dim_to_decimal_in_unit:nn { \l_judobelt_totheight_dim } { 1pt } }
                pt
              }
            \raisebox { \dim_use:N \l_judobeltraise_dim }
              {
                \includegraphics
                  [ height = { \dim_eval:n { 0.95\l_judobelt_totheight_dim } } ]
                  { judobelt-\l_judobeltcolorcolor_tl.pdf }
              }
          }
          {
            % Hauteur manuelle
            \raisebox { \dim_use:N \l_judobeltraise_dim }
              {
                \includegraphics
                  [ height = { \l_judobeltheight_tl } ]
                  { judobelt-\l_judobeltcolorcolor_tl.pdf }
              }
          }
      }
    \group_end:
  }

%====TIKZ VERSION
\keys_define:nn { coloredbelts }
  {
    height .dim_set:N  = \l_height_colored_belts_dim,  % fix: variable dédiée, pas \l_tmpa_dim
    height .initial:n  = { 2cm },
    height .default:n  = { 2cm },
    strut  .tl_set:N   = \l_judobeltstrut_tl,
    strut  .initial:n  = { abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ },
    border .tl_set:N   = \l_judobeltcolorborder_tl,
    border .initial:n  = { black },
    border .default:n  = { black },
  }

\NewDocumentCommand \inttikzcoloredbelt {}
  {%from svg2tikz script
    \draw[colored~belt~simple] (5.1, 3.7) to[bend~right=7.5] (5.0, 3.4) -- (5.6,3.4) -- cycle;
    \draw[colored~belt~simple] (0.3, 4.3).. controls (0.3, 4.3) and (3.4, 3.4) .. (6.4, 3.4).. controls (9.5, 3.4) and (12.1, 4.8) .. (12.2, 4.8).. controls (12.3, 4.9) and (12.6, 3.8) .. (12.2, 3.6).. controls (11.9, 3.3) and (10.1, 2.3) .. (6.5, 2.3).. controls (2.9, 2.3) and (0.4, 2.8) .. (0.2, 3.0).. controls (-0.0, 3.2) and (-0.0, 4.1) .. (0.3, 4.3) -- cycle;
    \draw[colored~belt~simple] (6.6, 1.59) to[bend~left=5] (5.6, 2.1).. controls (5.8, 2.5) and (7.4, 2.9) .. (7.4, 2.9) -- (7.3, 2.1) -- cycle;
    \draw[colored~belt~simple] (5.2, 2.2).. controls (5.2, 2.2) and (5.9, 1.6) .. (6.1, 1.7) -- (7.7, 2.6) -- (6.4, 3.5) -- (5.1, 2.5) -- cycle;
    \draw[colored~belt~simple] (0.6, 0.9).. controls (0.6, 0.9) and (3.9, 2.9) .. (5.9, 3.7).. controls (7.9, 4.5) and (6.7, 2.9) .. (6.7, 2.9).. controls (6.7, 2.9) and (4.6, 1.9) .. (3.8, 1.4).. controls (2.9, 0.9) and (1.6, 0.1) .. (1.5, 0.1).. controls (1.3, 0.0) and (0.6, 0.9) .. (0.6, 0.9) -- cycle;
    \draw[colored~belt~simple] (6.8, 4.2).. controls (6.8, 4.2) and (6.7, 4.2) .. (6.4, 3.9).. controls (6.2, 3.7) and (6.8, 3.7) .. (6.8, 3.7) -- cycle;
    \draw[colored~belt~simple] (6.1, 4.1).. controls (6.1, 4.1) and (8.6, 2.8) .. (9.5, 2.5).. controls (10.4, 2.3) and (12.5, 1.3).. (12.6, 1.1).. controls (12.7, 0.8) and (11.9, 0.3) .. (11.9, 0.3).. controls (11.9, 0.3) and (11.3, 0.8) .. (10.4, 1.2).. controls (9.6, 1.5) and (8.2, 2.1) .. (7.5, 2.4).. controls (6.7, 2.8) and (5.1, 3.7) .. (5.1, 3.7) -- cycle;
    \draw[colored~belt~simple] (6.8, 4.2).. controls (6.8, 4.2) and (6.7, 1.7) .. (6.6, 1.6).. controls (6.6, 1.5) and (7.8, 1.9) .. (7.8, 2.1).. controls (7.8, 2.2) and (8.0, 3.4) .. (7.9, 3.5).. controls (7.7, 3.6) and (6.9, 4.2) .. (6.8, 4.2) -- cycle;
  }

\cs_new_protected:Nn \__coloredbelt_starred:nnn
  {
    \group_begin:
      \__coloredbelts_reset_tikz:
      \keys_set:nn { coloredbelts } { #1 }
      \fp_set:Nn \l_scale_factor_fp
        {
          \fp_eval:n
            { \dim_to_decimal_in_unit:nn { \l_height_colored_belts_dim } { 1pt }
              / \dim_to_decimal_in_unit:nn { \c_judobelt_tikzheight_dim } { 1pt } }
        }
      \begin{tikzpicture}
        [
          scale           = \fp_use:N \l_scale_factor_fp,
          line~join       = round,
          transform~shape,
          #3,
          colored~belt~simple/.style =
            {
              line~width = \fp_eval:n { \l_scale_factor_fp * 0.1 } cm,
              fill       = #2,
              draw       = \tl_use:N \l_judobeltcolorborder_tl
            }
        ]
        \inttikzcoloredbelt
      \end{tikzpicture}
    \group_end:
  }

\cs_new_protected:Nn \__coloredbelt_inline:nnn
  {
    \group_begin:
      \__coloredbelts_reset_tikz:
      \keys_set:nn { coloredbelts } { #1 }
      \hbox_set:Nn \l_tmpa_box { \tl_use:N \l_judobeltstrut_tl }
      \dim_set:Nn \l_desired_height_colored_belts_dim
          { \box_ht:N \l_tmpa_box + \box_dp:N \l_tmpa_box }
      \dim_set:Nn \l_depth_colored_belts_dim { \box_dp:N \l_tmpa_box }
      \fp_set:Nn \l_scale_factor_fp
        {
          \fp_eval:n
            { \dim_to_decimal_in_unit:nn { \l_desired_height_colored_belts_dim } { 1pt }
              / \dim_to_decimal_in_unit:nn { \c_judobelt_tikzheight_dim } { 1pt } }
        }
      \raisebox { -\dim_use:N \l_depth_colored_belts_dim }
        {
          \begin{tikzpicture}
            [
              scale           = \fp_use:N \l_scale_factor_fp,
              line~join       = round,
              transform~shape,
              #3,
              colored~belt~simple/.style =
                {
                  line~width = \fp_eval:n { \fp_use:N \l_scale_factor_fp * 0.1 } cm,
                  fill       = #2,
                  draw       = \tl_use:N \l_judobeltcolorborder_tl
                }
            ]
            \inttikzcoloredbelt
          \end{tikzpicture}
        }
    \group_end:
  }

\NewDocumentCommand \tkzcoloredbelt { s O{} D<>{} m }
  {
    \bool_if:NTF #1
      { \__coloredbelt_starred:nnn { #2 } { #4 } { #3 } }
      { \__coloredbelt_inline:nnn  { #2 } { #4 } { #3 } }
  }

\endinput