[Tex/LaTex] change colors within a \draw

tikz-pgf

I was hoping to draw something like the following:

And I can by using \draw twice:

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width=1mm]
\draw[fill=red](0,0)rectangle(4,4)--(8,8);
\draw[fill=blue](8,8)rectangle(12,12);
\end{tikzpicture}
\end{document}

However I was hoping to do this with a single \draw. I can do this easily if both of the squares were the same color. à la,

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width=1mm,fill=red]
\draw(0,0)rectangle(4,4)--(8,8)rectangle(12,12);
\end{tikzpicture}
\end{document}

But I can't figure out how to get two differently colored shapes with a single draw. Is there a way to change colors partway through a draw?

Best Answer

You could perhaps draw a line from (0,0) to (4,4) and add a node at each end.

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width=1mm,every node/.style={outer sep=0pt,minimum size=2cm}]
\draw(0,0) node[draw=black,fill=red,below left] {} -- (4,4) node[draw=black,fill=blue,below left] {};
\end{tikzpicture}
\end{document}