我的 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}
如果我使用黑色到透明的 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
在\raisebox
淡入淡出定義中使用 和。另請注意,我使用的是path fading
而不是scope fading
.我不確定這有多大的區別,但我沒有讓它在傳球scope fading
和環境fading transform
中發揮作用scope
。- 我正在反轉 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}