2

I have a black on transparent icon in a png file. I want to use this icon at different locations in a LaTeX document in different colors. As a bonus it would be good if I can give the upper half of the icon a different color than the lower half because it sometimes appears on the border of different backgrounds.

This answer shows how to colorize a grayscale png file and it's easy to give the upper half a different color than the lower half. Unfortunately, it requires the logo to be on a white background:

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
    \fill[yellow!50!white] (0,1) rectangle (2,2);
    \fill[gray!50!black]   (0,0) rectangle (2,1);

    \begin{scope}[blend group=screen]
        \node[inner sep=0,line width=0] (logo) at (1,1) [anchor=center] {\includegraphics{icon-bw}};
        \fill[blue] (logo.west) rectangle (logo.north east);
        \fill[green] (logo.south west) rectangle (logo.east);
    \end{scope}
\end{tikzpicture}
\end{document}

An example icon with white background added: icon-bw.png

If I use a black to transparent png instead the entire node is painted blue and green.

An example icon with transparent background: icon.png

This answer shows how to use a png file as transparency mask. But how do I move the icon so that it's centered at (1,1)?

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{tikzfadingfrompicture}[name=icon]
    \begin{scope}[transparency group]
    \node (icon) [fill, draw, white] {\includegraphics[height=1cm]{icon}};
    \begin{scope}[blend mode=difference]
    \fill[white] (icon.south west) rectangle (icon.north east);
    \end{scope}
    \end{scope}
\end{tikzfadingfrompicture}

\begin{document}
\begin{tikzpicture}
    \fill[yellow!50!white] (0,1) rectangle (2,2);
    \fill[gray!50!black]   (0,0) rectangle (2,1);

    % How I intend to use it
    \begin{scope}[scope fading=icon, fit fading=false]
        \fill[blue] (0.5,1) rectangle (1.5,1.5);
        \fill[green] (0.5,0.5) rectangle (1.5,1);
    \end{scope}

    % Approach of the original answer. The icon is not positioned correctly.
    % The specified coordinate does not seem to make any difference.
    \path[scope fading=icon,fit fading=false] (1,1);
    \node[fill=yellow,minimum width=1cm,minimum height=1cm] {};
\end{tikzpicture}
\end{document}

In my real use case the icon consists of black and transparency, only. So it would be acceptable if an answer does not work with intermediate gray tones.

2
  • Without icon.png your code cannot be executed. Dec 31, 2023 at 15:14
  • @JohnKormylo icon.png is the second image in the question. The first image is icon-bw.png. They are rendered very similar here on the webpage but the first has a white background, the second has a transparent background.
    – jakun
    Dec 31, 2023 at 16:31

2 Answers 2

3

The fading pattern will be centered about (0,0), and the only way to move it is to move the node contents using things like \hspace and \raisebox. Moving the implementation just uses the pattern at that location (off the edge). Since it is centered, you have to move it twice as far as it actually moves.

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{fadings}

\begin{tikzfadingfrompicture}[name=icon,inner sep=0]
  \node [fill=transparent!0]
    {\hspace{2cm}\raisebox{2cm}{\includegraphics[height=2cm, width=2cm]{images/icon}}};
\end{tikzfadingfrompicture}

\begin{document}
\begin{tikzpicture}
  \fill[yellow!50!white] (0,1) rectangle (2,2);
  \fill[gray!50!black]   (0,0) rectangle (2,1);
  \begin{scope}[shift={(1,1)}]
    \path[scope fading=icon, fit fading=false];
    \node[bottom color=blue,top color=green,minimum width=2cm,minimum height=2cm]{};
  \end{scope}
\end{tikzpicture}
\end{document}

demo

1
  • Thank you for your help. I was not quite happy with using \hspace and \raisebox in the fading definition, though. Instead I have found a different way using fading transform where I am applying the fading.
    – jakun
    Jan 1 at 11:33
1

This answer differs from John Kormylo's mainly in two points:

  • I am using fading transform where I am applying the fading instead of \hspace and \raisebox in the fading definition. Also note that I am using path fading instead of scope fading. I am not sure how big of a difference that is but I did not get it working with passing scope fading and fading transform to a scope environment.
  • I am inverting the colors of the png file so that the black parts are filled instead of the transparent parts.
\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{fadings}

\newcommand\IconNodeContent{%
    \includegraphics[height=1cm]{icon}%
}

\begin{tikzfadingfrompicture}[name=icon]
    % I am using \fill[white] with blend mode=difference to invert the colors
    % so that I can use my black on transparent icon without needing to convert it to white on transparent/black.
    % This has the down side that the area around the icon node is not transparent but opaque.
    % I am increasing the transparent area a little with draw thick to avoid a visible rectangle around the icon when filling it later.
    \begin{scope}[transparency group]
    \node (icon) [fill, draw, thick, white] {\IconNodeContent};
    \begin{scope}[blend mode=difference]
    \fill[white] (icon.south west) rectangle (icon.north east);
    \end{scope}
    \end{scope}
\end{tikzfadingfrompicture}

\begin{document}
\begin{tikzpicture}
    % draw background
    \fill[yellow!50!white] (0,1) rectangle (2,2);
    \fill[gray!50!black]   (0,0) rectangle (2,1);

    % create an invisible node to define size and position of icon
    \node (icon) at (1,1) [anchor=center, transparent] {\IconNodeContent};
    % draw the top half of the icon in one color, shifting the coordinates 1pt inwards to make sure no border is visible
    \fill[blue,  path fading=icon, fit fading=false, fading transform={shift=(icon.center)}] ([shift={(1pt,-1pt)}] icon.north west) rectangle ([shift={(-1pt,+0pt)}] icon.east);
    % draw the bottom half of the icon in another color, shifting the coordinates 1pt inwards to make sure no border is visible
    \fill[green, path fading=icon, fit fading=false, fading transform={shift=(icon.center)}] ([shift={(1pt,+1pt)}] icon.south west) rectangle ([shift={(-1pt,-0pt)}] icon.east);
\end{tikzpicture}
\end{document}

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .