如何为非白色背景上的灰度图标着色?

如何为非白色背景上的灰度图标着色?

我在 png 文件中有一个透明黑色图标。我想在 LaTeX 文档的不同位置以不同的颜色使用此图标。作为额外奖励,如果我可以为图标的上半部分设置与下半部分不同的颜色,那就太好了,因为它有时会出现在不同背景的边框上。

这个答案展示了如何为灰度 png 文件着色,很容易为上半部分赋予与下半部分不同的颜色。不幸的是,它要求徽标位于白色背景上:

\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}

添加了白色背景的示例图标: 图标-bw.png

如果我使用黑色到透明的 png,整个节点就会被漆成蓝色和绿色。

具有透明背景的示例图标: 图标.png

这个答案展示了如何使用 png 文件作为透明蒙版。但是我该如何移动图标,使其以 (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}

在我的实际使用案例中,图标仅由黑色和透明度组成。因此,如果答案不适用于中间灰色调,这是可以接受的。

答案1

淡入淡出模式将以 (0,0) 为中心,移动它的唯一方法是使用\hspace和 之类的东西移动节点内容\raisebox。移动实现只会使用该位置(边缘外)的模式。由于它居中,因此您必须将其移动的距离是实际移动距离的两倍。

\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}

演示

答案2

这个答案与 John Kormylo 的答案主要有两点不同:

  • fading transform我在应用淡入淡出定义时使用了而不是\hspace和。另请注意,我使用的是 而不是。我不确定这有多大区别,但我没有让它在将和传递到环境中工作。\raiseboxpath fadingscope fadingscope fadingfading transformscope
  • 我正在反转 png 文件的颜色,以便填充黑色部分而不是透明部分。
\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}

输出

相关内容