% !TeX root = ./examples/elephant.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% This package is part of the TikZlings package
%% A package to bring cute little animals and other beings into tikz
%% Maintained by samcarter
%%
%% Project repository and bug tracker:
%% https://github.com/samcarter/tikzlings
%%
%% Released under the LaTeX Project Public License v1.3c or later
%% See https://www.latex-project.org/lppl.txt
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{tikzlings-elephants}[2025/03/23 version v2.3 Draw teddy elephants in TikZ]

\RequirePackage{tikz}
\RequirePackage{tikzlings-addons}

\newcommand*{\elephant}[1][]{%
 \begin{scope}%
   \tikzset{/elephant/.cd,#1}%
   \elephant@draw%
 \end{scope}%
 \thing[#1]%
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/elephant/foreground}
 \UseHook{tikzlings/foreground}
 \csname elephanthookforeground\endcsname
 \csname tikzlinghookforeground\endcsname
}

\newif\ifelephant@threeD
\newif\ifelephant@back
\newif\ifelephant@contour

\NewHook{tikzlings/elephant/background}
\NewHook{tikzlings/elephant/belly}
\NewHook{tikzlings/elephant/body}
\NewHook{tikzlings/elephant/foreground}

\tikzset{
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % Pass unknown keys on to tikz
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 /elephant/.search also={/tikz,/pgf,/thing},
 /elephant/.cd,
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % body
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 body/.store in     = \elephant@body,
 body               = gray,
 eyes/.store in     = \elephant@eyes,
 eyes               = \elephant@body!30!black,
 eye/.forward to    = /elephant/eyes,
 ears/.store in     = \elephant@ears,
 ears               = \elephant@body!30!white,
 ear/.forward to    = /elephant/ears,
 feet/.store in     = \elephant@feet,
 feet               = \elephant@body!30!white,
 foot/.forward to   = /elephant/feet,
 hands/.store in    = \elephant@hands,
 hands              = \elephant@body!30!white,
 hand/.forward to   = /elephant/hands,
 belly/.store in    = \elephant@belly,
 belly              = \elephant@body!30!white,
 tail/.store in    = \elephant@tail,
 tail              = \elephant@body!30!white,
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % options
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 3D/.code           = \elephant@threeDtrue,
 back/.code         = \elephant@backtrue,
 contour/.code      = \elephant@contourtrue
                      \def\elephant@contour{#1},
 contour/.default   = black,
 outline/.forward to = /elephant/contour,
}

\def\elephant@draw{%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % minimal bounding box size
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \path (-0.86, 0.0) rectangle (0.86, 2.17);
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % store the current scale factor
 % from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
 \pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
 \pgfmathsetmacro{\xscalefactor}{\tmpscaleA}
 \pgfmathsetmacro{\yscalefactor}{\tmpscaleD}
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % switch between 2D, 3D and contours, from
 % https://chat.stackexchange.com/transcript/message/45991801#45991801
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \ifelephant@threeD
   \def\elephant@part@draw[##1]{\shade[ball color=##1]}
 \else% 3D
   \ifelephant@contour
     \def\elephant@part@draw[##1]{\draw[##1,\elephant@contour,fill=white,line width=\scalingfactor*0.4pt]}
   \else% contour
     \def\elephant@part@draw[##1]{\fill[##1]}
   \fi% contour
 \fi% 3D
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % Body parts
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/elephant/background}
 \UseHook{tikzlings/background}
 \csname elephanthookbackground\endcsname
 \csname tikzlinghookbackground\endcsname
 %
 % Arms %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \elephant@part@draw[\elephant@body] (0.47,0.86) ellipse[x radius=0.35, y radius=0.25, rotate=-50];
 \elephant@part@draw[\elephant@body] (-0.47,0.86) ellipse[x radius=0.35, y radius=0.25, rotate=50];
 %
 % Feet if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \ifelephant@back
   \elephant@part@draw[\elephant@body] (0.425, 0.3) circle[radius=0.28];
   \elephant@part@draw[\elephant@body] (-0.425, 0.3) circle[radius=0.28];
   \elephant@part@draw[\elephant@body] (0.57,0.74) circle [radius=0.20];
   \elephant@part@draw[\elephant@body] (-0.57,0.74) circle [radius=0.20];
 \fi% back
 %
 % Body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \elephant@part@draw[\elephant@body] (0,0.75) ellipse[x radius=0.55, y radius=0.65];
 \unless\ifelephant@back
   \elephant@part@draw[\elephant@belly] (0,0.7) ellipse[x radius=0.35, y radius=0.4];
 \fi% back
 %
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/elephant/belly}
 \UseHook{tikzlings/belly}
 \csname elephanthookbelly\endcsname
 \csname tikzlinghookbelly\endcsname
 %
 % Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \unless\ifelephant@back
   \elephant@part@draw[\elephant@body] (0.425, 0.3) circle[radius=0.28];
   \elephant@part@draw[\elephant@body] (-0.425, 0.3) circle[radius=0.28];
   \elephant@part@draw[\elephant@feet] (0.425, 0.3) circle[radius=0.17];
   \elephant@part@draw[\elephant@feet] (-0.425, 0.3) circle[radius=0.17];
   \elephant@part@draw[\elephant@body] (0.57,0.75) circle [radius=0.20];
   \elephant@part@draw[\elephant@hands] (0.57,0.74) circle [radius=0.12];
   \elephant@part@draw[\elephant@body] (-0.57,0.74) circle [radius=0.20];
   \elephant@part@draw[\elephant@hands] (-0.57,0.74) circle [radius=0.12];
 \fi% back
 %
 % Ears %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \elephant@part@draw[\elephant@body] (0.5, 1.7) ellipse [x radius=0.35,y radius=0.45];
 \elephant@part@draw[\elephant@body] (-0.5, 1.7) ellipse [x radius=0.35,y radius=0.45];
 \unless\ifelephant@back
   \elephant@part@draw[\elephant@ears] (0.5, 1.7) ellipse [x radius=0.25,y radius=0.35];
   \elephant@part@draw[\elephant@ears] (-0.5, 1.7) ellipse [x radius=0.25,y radius=0.35];
 \fi% back
 %
 % Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \elephant@part@draw[\elephant@body] (0, 1.55) circle[radius=0.5];
 %
 % Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \unless\ifelephant@back
   %
   % Trunk% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \ifelephant@threeD
     \elephant@part@draw[\elephant@body,draw=\elephant@body!70!black,line width=\scalingfactor*0.6pt] (0.1428, 1.4375) .. controls (0.1607, 1.0411) and (0.3327, 1.1276) .. (0.6705, 1.355) -- (0.72, 1.22) .. controls (0.3327, 0.9) and (-0.1607, 0.8) .. (-0.1428, 1.4375) arc [start angle=160, end angle=20, radius=0.155] -- cycle;
     \fill[\elephant@body!40!black] (0.7, 1.29) circle [radius=0.08];
   \else% 3D
     \elephant@part@draw[\elephant@body,draw=\elephant@body!70!black,line width=\scalingfactor*0.6pt] (0.1428, 1.4375) .. controls (0.1607, 1.0411) and (0.3327, 1.1276) .. (0.6705, 1.355) -- (0.72, 1.22) .. controls (0.3327, 0.9) and (-0.1607, 0.8) .. (-0.1428, 1.4375);
     \elephant@part@draw[\elephant@body!70!black] (0.7, 1.29) circle [radius=0.08];
   \fi% 3D
   %
   % Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \elephant@part@draw[\elephant@eyes] (-0.155, 1.7) circle[radius=0.05];
   \elephant@part@draw[\elephant@eyes] (0.155, 1.7) circle[radius=0.05];
 %
 \fi% back
 %
 % Tail if back %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \ifelephant@back
   \draw[\elephant@tail,line width=\scalingfactor*0.6pt] (0, 0.5904) .. controls (-0.1619, 0.4147) and (0.203, 0.4157) .. (0.2422, 0.2712);
   \elephant@part@draw[\elephant@tail] (0.2008, 0.1949) .. controls (0.2452, 0.2121) and (0.2799, 0.242) .. (0.2691, 0.2755) .. controls (0.2611, 0.3005) and (0.2127, 0.3115) .. (0.1927, 0.2945) .. controls (0.1674, 0.2729) and (0.2082, 0.2383) .. (0.2008, 0.1949) -- cycle;
 \fi% back
 %
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/elephant/body}
 \UseHook{tikzlings/body}
 \csname elephanthookbody\endcsname
 \csname tikzlinghookbody\endcsname
 %
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Documentation
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\clearpage
%\section[Elephant]{Johannes, the elephant}
%
%\emph{The namesake of Johannes loves elephants}
%
%\subsection{Package name}
%
%\begin{tcolorbox}[title={Package usage}]
%\begin{samcode}
%\usepackage{tikzlings-elephants}
%\end{samcode}
%\end{tcolorbox}
%
%\subsection{Basic Usage}
%
%\begin{tcblisting}{title={Basic elephant}}
%\elephant
%\end{tcblisting}
%
%\subsection{Options}
%
%The basic elephant can be modified by changing its colour:
%\begin{tcblisting}{title={Body colour}}
%\elephant[body=blue]
%\end{tcblisting}
%
%In addition to the colour of the body, the colour of various body parts can be adjusted:
%\begin{tcblisting}{title={Ear colour}}
%\elephant[ears=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Eye colour}}
%\elephant[eyes=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Hand colour}}
%\elephant[hands=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Belly colour}}
%\elephant[belly=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Foot colour}}
%\elephant[feet=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Tail colour}}
%\elephant[back,tail=red]
%\end{tcblisting}
%
%To view the elephant from behind:
%\begin{tcblisting}{title={Back view}}
%\elephant[back]
%\end{tcblisting}
%
%The key \saminline|3D| will make the elephant 3-dimensional:
%\begin{tcblisting}{title={3D view}}
%\elephant[3D]
%\end{tcblisting}
%
%And finally the \saminline|contour| key will only draw the outlines:
%\begin{tcblisting}{title={Contours}}
%\elephant[contour=black]
%\end{tcblisting}