% 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}{2025-10-03}{0.1.6}{Colored Judo's belts}

%====HISTORIQUE
% 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
\dim_new:N \g_judobelt_totheight_dim
\dim_new:N \g_judobelt_depth_dim
\dim_new:N \g_judobeltraise_dim

%====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

%====CLÉS (version française)
\keys_define:nn { ceinturejudo }
  {
    Hauteur .tl_set:N = \l_judobeltheight_tl,
    Hauteur .initial:n = {auto},
    DecalV .dim_set:N = \l_judobeltraise_dim,
    DecalV .initial: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
    \bool_if:NTF #1
      {
        \includegraphics[#2]{judobelt-\l_judobeltcolorcolor_tl.pdf}
      }
      {
        \keys_set:nn { ceinturejudo } { #2 }
        \str_if_eq:eeTF { \tl_to_str:N \l_judobeltheight_tl } { auto }
          {
            % hauteur auto
            \hbox_set:Nn \l_tmpa_box { \tl_use:N \l_judobeltstrut_tl }
            \dim_set:Nn \g_judobelt_totheight_dim { \box_dp:N \l_tmpa_box + \box_ht:N \l_tmpa_box }
            \dim_set:Nn \g_judobelt_depth_dim { \box_dp:N \l_tmpa_box }
            \dim_set:Nn \g_judobeltraise_dim
              {
                -\g_judobelt_depth_dim + \fp_eval:n { 0.025 * \dim_to_decimal_in_unit:nn { \g_judobelt_totheight_dim } { 1pt } } pt
              }
            \raisebox{ \dim_use:N \g_judobeltraise_dim }
            {
              \includegraphics[height={\dim_eval:n { 0.95\g_judobelt_totheight_dim }}]{judobelt-\l_judobeltcolorcolor_tl.pdf}
            }
          }
          {
            \raisebox{\dim_use:N \l_judobeltraise_dim}{\includegraphics[height={\l_judobeltheight_tl}]{judobelt-\l_judobeltcolorcolor_tl.pdf}}
          }
      }
    \group_end:
  }

%====KEYS (version anglaise)
\keys_define:nn { judobelt }
  {
    Height .tl_set:N = \l_judobeltheight_tl,
    Height .initial:n = {auto},
    OffsetV .dim_set:N = \l_judobeltraise_dim,
    OffsetV .initial:n = {0pt},
    Strut .tl_set:N = \l_judobeltstrut_tl,
    Strut .initial:n = {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
  }

%====COMMANDE EN
\NewDocumentCommand\ColorBelt{ s O{} m }
  {
    \group_begin:
    % Color gest
    \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
    \bool_if:NTF #1
      {
        \includegraphics[#2]{judobelt-\l_judobeltcolorcolor_tl.pdf}
      }
      {
        \keys_set:nn { judobelt } { #2 }
        \str_if_eq:eeTF { \tl_to_str:N \l_judobeltheight_tl } { auto }
          {
            % hauteur auto
            \hbox_set:Nn \l_tmpa_box { \tl_use:N \l_judobeltstrut_tl }
            \dim_set:Nn \g_judobelt_totheight_dim { \box_dp:N \l_tmpa_box + \box_ht:N \l_tmpa_box }
            \dim_set:Nn \g_judobelt_depth_dim { \box_dp:N \l_tmpa_box }
            \dim_set:Nn \g_judobeltraise_dim
              {
                -\g_judobelt_depth_dim + \fp_eval:n { 0.025 * \dim_to_decimal_in_unit:nn { \g_judobelt_totheight_dim } { 1pt } } pt
              }
            \raisebox{ \dim_use:N \g_judobeltraise_dim }
            {
              \includegraphics[height={\dim_eval:n { 0.95\g_judobelt_totheight_dim }}]{judobelt-\l_judobeltcolorcolor_tl.pdf}
            }
          }
          {
            \raisebox{\dim_use:N \l_judobeltraise_dim}{\includegraphics[height={\l_judobeltheight_tl}]{judobelt-\l_judobeltcolorcolor_tl.pdf}}
          }
      }
    \group_end:
  }

%====TIKZ VERSION
%lengths + tl
\dim_new:N \g_height_colored_belts_dim
\dim_new:N \g_depth_colored_belts_dim
\dim_new:N \g_desired_height_colored_belts_dim
\dim_new:N \g_globalscale_colored_belt_dim
\tl_new:N \l_judobeltcolorborder_tl
\fp_new:N \l_scale_factor_fp

\dim_set:Nn \g_height_colored_belts_dim { 5cm }

\keys_define:nn { coloredbelts }
{
	height   .dim_set:N  = \l_tmpa_dim,
	height   .initial:n  = { 2cm },
	strut    .tl_set:N   = \l_judobeltstrut_tl,
	strut    .initial:n  = {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ},
	border   .tl_set:N   = \l_judobeltcolorborder_tl,
	border   .initial: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:Npn \coloredbelt_starred:nnn #1#2#3
{
	\group_begin:
	\keys_set:nn { coloredbelts } { #1 }
	\tl_set:Ne \l_my_tl { \dim_ratio:nn { \l_tmpa_dim } { \g_height_colored_belts_dim } }
	\fp_set:Nn \l_scale_factor_fp { \fp_eval:n { \l_my_tl } } 
	\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:Npn \coloredbelt_inline:nnn #1#2#3
{
	\group_begin:
	\keys_set:nn { coloredbelts } { #1 }
	\hbox_set:Nn \l_tmpa_box { \tl_use:N \l_judobeltstrut_tl }
	\dim_set:Nn \g_desired_height_colored_belts_dim { \box_ht:N \l_tmpa_box + \box_dp:N \l_tmpa_box }
	\dim_set:Nn \g_depth_colored_belts_dim { \box_dp:N \l_tmpa_box }
	\tl_set:Ne \l_my_tl { \dim_ratio:nn { \g_desired_height_colored_belts_dim } { \g_height_colored_belts_dim } }
	\fp_set:Nn \l_scale_factor_fp { \fp_eval:n { \l_my_tl } } 
	\raisebox { -\g_depth_colored_belts_dim }
	{
		\begin{tikzpicture}[
			scale = \fp_use:N \l_scale_factor_fp,
			line~join=round,
			#3,
			transform~shape,
			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