# Drawing color tiles using Tikz

Hi good people of StackExchange, I want to draw a colored tile using Tikz. Roughly to represent the percentage three kind of things have in comparison to their total. The picture I want can be produced using the following.

\usepackage{tikz}

\newcommand\colorTiles[1]{
\begin{tikzpicture}
\draw[ultra thick](0,0) rectangle (1,0.5);
\draw[fill = red](0, 0) rectangle (0.3, 0.5);
\draw[fill = green](0.3, 0) rectangle (0.7, 0.5);
\draw[fill = purple](0.7, 0) rectangle(1, 0.5);
\end{tikzpicture}
}

\colorTiles{}


Now my question is, how can I define the new command so that I don't have to adjust the proportions every time? Ideally, I just need to use

\colorTile{0.3, 0.5, 0.2}


And I should get a tile with 30% red, 50% green, and 20% purple. I'm not familiar with Tikz so please help me.

Welcome to TeX-SE! The following produces such bars. The syntax is as simple as

\pic{color tile={0.3, 0.5, 0.2}};


The colors are stored in a list and can be adjusted, as illustrated in the following example. The width is stored in a pgf key and can also be adjusted.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}[pics/color tile/.style={code={
\path (0,0) coordinate (aux);
\foreach \XX [count=\YY starting from 0] in {#1}
{\pgfmathsetmacro\mycol{{\LstCols}[\YY]}
\draw[line width=\pgfkeysvalueof{/tikz/color tile/width},color=\mycol]
(aux) -- ++(\XX,0) coordinate (aux);}
\draw[thick] (0,-\pgfkeysvalueof{/tikz/color tile/width}/2) rectangle
(aux|-0,\pgfkeysvalueof{/tikz/color tile/width}/2);
}},color tile/.cd,width/.initial=5mm]
\edef\LstCols{"red","green","purple","blue"} %< adjust and extent if needed
\path pic{color tile={0.3, 0.5, 0.2}} (3,0) pic{color tile={0.3,0.6,0.4, 0.5}};
\edef\LstCols{"orange","purple","yellow","red","blue"} %< adjusted and extended list
\path  (1,-2) pic{color tile={0.3,0.6,0.4, 0.5,0.8}};
\tikzset{color tile/width=1cm}
\path  (1,-4) pic{color tile={0.3,0.6,0.4, 0.5,0.8}};
\end{tikzpicture}
\end{document}


As for your comment on a command: here is a proposal.

\documentclass{article}
\usepackage{tikz}
\tikzset{pics/color tile/.style={code={
\edef\LstCols{\pgfkeysvalueof{/tikz/color tile/colors}}
\path (0,0) coordinate (aux);
\foreach \XX [count=\YY starting from 0] in {#1}
{\pgfmathsetmacro\mycol{{\LstCols}[\YY]}
\draw[line width=\pgfkeysvalueof{/tikz/color tile/width},color=\mycol]
(aux) -- ++(\XX,0) coordinate (aux);}
\draw[thick] (0,-\pgfkeysvalueof{/tikz/color tile/width}/2) rectangle
(aux|-0,\pgfkeysvalueof{/tikz/color tile/width}/2);
}},color tile/.cd,width/.initial=1em,
colors/.initial={"red","green","purple","blue"}}
\newcommand{\ColorTile}[2][]{\tikz{\pic[color tile/.cd,#1]{color tile={#2}};}}
\begin{document}
\begin{tabular}{ll}
\ColorTile{0.3, 0.5, 0.2} & ABC \\
\ColorTile{0.1, 0.2, 0.7} & UVW \\
\ColorTile[colors={"red","green","blue"}]{0.3, 0.5, 0.2}  & XYZ\\
\end{tabular}
\end{document}


• P.S. If you want to "stretch" the tile, use xscale, e.g. \path pic[xscale=5]{color tile={0.3,0.2,0.5}};. You can also rotate it etc. – user121799 Jul 11 '19 at 21:04
• Thanks a lot for the answer! Is it possible to use this inline with texts? In fact I was going to put these in tables. Can your answer be adapted for that? Thanks again! – skyphantasy Jul 11 '19 at 23:14
• @skyphantasy Of course, I added a proposal. – user121799 Jul 12 '19 at 2:12

Answering this just in case someone would need it:

\newcommand\colorTiles[3]{
\begin{tikzpicture}
\draw[ultra thick](0,0) rectangle (2,0.5);
\draw[fill = red](0, 0) rectangle (#1 * 2, 0.5);
\draw[fill = green](#1 * 2, 0) rectangle (#2 * 2 + #1 * 2, 0.5);
\draw[fill = purple](#2 * 2 + #1 * 2, 0) rectangle(#3 * 2 + #2 * 2 + #1 * 2, 0.5);
\draw[fill = gray](#3 * 2 + #2 * 2 + #1 * 2, 0) rectangle(2, 0.5);
\end{tikzpicture}
}


Then we can put \colorTiles{0.2}{0.5}{0.1} inline.