# Coloring graphics with tikz

I have been using the fadings tikz library for colouring images (png and pdf, mostly) in LaTeX with the code below. The only two libraries I am using here are tikz and graphicx.

% Importing the image
\node [fill={transparent!0}]
{\includegraphics{nameoftheimage}};

% Printing "theimage"
\begin{tikzpicture}[inner sep=0]
% Colouring
\node[fill=magenta, minimum width=thewidth, minimum height=theheight]{};
\end{tikzpicture}


When I do this, I obtain the following image:

That is already great. The problem is that the source file has to be:

This forces me to invert the colouring of all the images I want to use. I've googled the tikzfadingfrompicture function, but I found nothing useful. (Nor in the tikz documentation.) Is there an easy way to manipulate the code above so that it is not necessary to invert the colours of the original picture? Thanks in advance for any answer.

As you note yourself, your method "inverts the picture", i.e. does the step you do by hand. You could just store this result in a \savebox, the inverted version of gives the desired result.

\documentclass{article}
\usepackage{tikz}
\newsavebox{\cheat}
\begin{document}
\node[fill=transparent!0]
{\includegraphics[scale=0.3]{example-image-a}};

\savebox\cheat{\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\includegraphics[scale=0.3]{example-image-a}}};
\end{tikzpicture}}
\node[fill=transparent!0]
{\usebox\cheat};

\begin{tabular}{ccc}
\tikz[baseline={(pic.base)}]{\node(pic){\includegraphics[scale=0.3]{example-image-a}};}
& \tikz[baseline={(pic.base)}]{\node(pic){\usebox\cheat};} &
\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\usebox\cheat}};
\end{tikzpicture}
\\
original & inverted & inverted twice
\end{tabular}

\end{document}


And you can make this a macro so that you do not have to redo these steps.

\documentclass{article}
\usepackage{tikz}
\newsavebox{\cheat}
\node[fill=transparent!0]
{#2};
\savebox\cheat{\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{#2}};
\end{tikzpicture}}%
\node[fill=transparent!0]
{\usebox\cheat};
\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\usebox\cheat}};
\end{tikzpicture}}
\begin{document}