%!TEX pdflatex
% licence etalab 2.0

\documentclass{article}
\usepackage[margin=5mm,a4paper]{geometry}
\usepackage{customenvs}
\usepackage{codehigh}

\newlength\unittkzbattery
\newlength\tmpheightofcharstkzbattery
\newlength\tmpdepthofcharstkzbattery
\newlength\tmpraisevofkzbattery

\NewDocumentCommand\inttkzbattery{O{}O{}m}{%
	%#1=maincolor
	%#2=fillcolor
	%#3=level
	\def\heightbatterylevel{0.27}%
	\xdef\offsetbatterylevel{\fpeval{(1-3*\heightbatterylevel)/4}}%
	\draw[#1,line width=0.125\unittkzbattery,rounded corners=0.05\unittkzbattery,postaction={fill=white}] (-0.33,0) rectangle++ (0.66,1) ;
	\fill[#1,line width=0.05\unittkzbattery,rounded corners=0.01\unittkzbattery] ([yshift=0.0625\unittkzbattery]-0.15,1) rectangle++ (0.3,0.1) ;
	\xintifboolexpr{#3 >= 1}
		{%n1
			\fill[#2,line width=0.125\unittkzbattery,rounded corners=0.05\unittkzbattery] ({-0.33+\offsetbatterylevel},{0+\offsetbatterylevel}) rectangle++ ({0.66-2*\offsetbatterylevel},{\heightbatterylevel}) ;
		}{}%
	\xintifboolexpr{#3 >= 2}
		{%n2
			\fill[#2,line width=0.125\unittkzbattery,rounded corners=0.05\unittkzbattery] ({-0.33+\offsetbatterylevel},{\heightbatterylevel+2*\offsetbatterylevel}) rectangle++ ({0.66-2*\offsetbatterylevel},{\heightbatterylevel}) ;
		}{}%
	\xintifboolexpr{#3 >= 3}
		{%n3
			\fill[#2,line width=0.125\unittkzbattery,rounded corners=0.05\unittkzbattery] ({-0.33+\offsetbatterylevel},{2*\heightbatterylevel+3*\offsetbatterylevel}) rectangle++ ({0.66-2*\offsetbatterylevel},{\heightbatterylevel}) ;
		}{}%
	%\draw[thin,gray,ystep=0.1] (-0.33,0) grid (0.33,1.1) ;
}

%hauteurtotale = 1 + 0.5*0.125 + 0.5*0.125 + 0.1 = 1+0.125+0.1 = 1.225
%largeurtotale = 0.66 + 0.125 = 0.785

\defKV[pictobattery]{%
	maincolor=\def\pictobatterycolor{#1},%
	barcolor=\def\pictobatteryfillcolor{#1},%
	height=\def\pictobatteryheight{#1},%
	scale=\def\pictobatteryscale{#1}
}
\setKVdefault[pictobattery]{%
	maincolor={},%
	barcolor=auto,%
	height=dauto,%
	flip=false,%
	scale=1
}

\NewDocumentCommand\pictobattery{O{}D<>{}m}{%
	\restoreKV[pictobattery]%
	\setKV[pictobattery]{#1}%
	\IfStrEqCase{\pictobatteryheight}{%
		{dauto}{\settototalheight\tmpheightofcharstkzbattery{qH}\settodepth\tmpdepthofcharstkzbattery{qH}}%
		{auto}{\settototalheight\tmpheightofcharstkzbattery{H}\settodepth\tmpdepthofcharstkzbattery{H}}%
	}[%
		\setlength\tmpheightofcharstkzbattery{\pictobatteryheight}%
	]%
	\IfStrEq{#3}{}{\def\tmpbattlevel{0}}{\def\tmpbattlevel{#3}}%
	\IfStrEq{\pictobatteryfillcolor}{auto}%
		{%
			\xintifboolexpr{\tmpbattlevel == 0}{\xdef\pictobatteryfillcolor{}}{}%
			\xintifboolexpr{\tmpbattlevel == 1}{\xdef\pictobatteryfillcolor{red}}{}%
			\xintifboolexpr{\tmpbattlevel == 2}{\xdef\pictobatteryfillcolor{orange}}{}%
			\xintifboolexpr{\tmpbattlevel == 3}{\xdef\pictobatteryfillcolor{green!75!black}}{}%
		}%
		{}%
	\ifboolKV[pictobattery]{flip}%
		{%1,2738
			\xdef\tmpscaaaale{\fpeval{1.2738*\pictobatteryscale}}%
			\setlength\unittkzbattery{\tmpscaaaale\tmpheightofcharstkzbattery}%
			\IfStrEq{\pictobatteryheight}{dauto}%\tmpraisevofkzbattery
				{%vcenter
					\xdef\tmpoffffset{\fpeval{(1-\pictobatteryscale)/2}}%
					\setlength\tmpraisevofkzbattery{\dimexpr\tmpoffffset\tmpheightofcharstkzbattery-\tmpdepthofcharstkzbattery\relax}%
				}%
				{%normal line
					\setlength\tmpraisevofkzbattery{-\tmpdepthofcharstkzbattery}%
				}%
			\raisebox{\tmpraisevofkzbattery}{%
				\begin{tikzpicture}[x=\unittkzbattery,y=\unittkzbattery,rotate=-90,#2]\inttkzbattery[\pictobatterycolor][\pictobatteryfillcolor]{\tmpbattlevel}\end{tikzpicture}%
			}%
		}%
		{%0.8163
			\xdef\tmpscaaaale{\fpeval{0.8163*\pictobatteryscale}}%
			\setlength\unittkzbattery{\tmpscaaaale\tmpheightofcharstkzbattery}%
			\IfStrEq{\pictobatteryheight}{dauto}%\tmpraisevofkzbattery
				{%vcenter
					\xdef\tmpoffffset{\fpeval{(1-\pictobatteryscale)/2}}%
					\setlength\tmpraisevofkzbattery{\dimexpr\tmpoffffset\tmpheightofcharstkzbattery-\tmpdepthofcharstkzbattery\relax}%
				}%
				{%normal line
					\setlength\tmpraisevofkzbattery{-\tmpdepthofcharstkzbattery}%
				}%
			\raisebox{\tmpraisevofkzbattery}{%
				\begin{tikzpicture}[x=\unittkzbattery,y=\unittkzbattery,#2]\inttkzbattery[\pictobatterycolor][\pictobatteryfillcolor]{\tmpbattlevel}\end{tikzpicture}%
			}%
		}%
}

\begin{document}

\section{En ligne, pleine hauteur (avec jambage)}

\begin{codehigh}[language=latex/latex2]
\pictobattery{[maincolor=...,barcolor=...,flip=TF,scale=...]}{nb}
\end{codehigh}

\begin{demohigh}[language=latex/latex2]
{\LARGE\sffamily q\pictobattery{}H ou q\pictobattery{1}H ou q\pictobattery{2}H ou
q\pictobattery{3}%
\pictobattery[flip]{3}%
\pictobattery[flip,scale=0.8]{3}H%
}
\end{demohigh}

\section{En ligne, pleine hauteur (sans jambage)}

\begin{codehigh}[language=latex/latex2]
\pictobattery{[height=auto,maincolor=...,barcolor=...,flip=TF,scale=...]}{nb}
\end{codehigh}

\begin{demohigh}[language=latex/latex2]
{\LARGE\sffamily q\pictobattery[height=auto]{}H ou q\pictobattery[height=auto]{1}H ou q\pictobattery[height=auto]{2}H ou
q\pictobattery[height=auto]{3}%
\pictobattery[height=auto,flip]{3}%
\pictobattery[height=auto,flip,scale=0.8]{3}H%
}
\end{demohigh}

\section{Hauteur fixée}

\begin{codehigh}[language=latex/latex2]
\pictobattery{[height=...,maincolor=...,barcolor=...,flip=TF,scale=...]}{nb}
\end{codehigh}

\begin{demohigh}[language=latex/latex2]
{\tikz\draw[thin,gray](0,0)grid(1,2);}%
\pictobattery[height=2cm,maincolor=black]{1}%
{\tikz\draw[thin,gray](0,0)grid(1,2);}

{\tikz\draw[thin,gray](0,0)grid(1,2);}%
\pictobattery[height=2cm,flip,maincolor=black]{2}%
{\tikz\draw[thin,gray](0,0)grid(1,2);}
\end{demohigh}

\end{document}