我一直使用tikz 库为 LaTeX 中的图像(和,主要是)fadings
着色,代码如下。我在这里使用的两个库只有和。png
pdf
tikz
graphicx
% Importing the image
\begin{tikzfadingfrompicture}[name=theimage,inner sep=0]
\node [fill={transparent!0}]
{\includegraphics{nameoftheimage}};
\end{tikzfadingfrompicture}
% Printing "theimage"
\begin{tikzpicture}[inner sep=0]
% Colouring
\path[scope fading=theimage,fit fading=false];
\node[fill=magenta, minimum width=thewidth, minimum height=theheight]{};
\end{tikzpicture}
当我这样做时,我得到了以下图像:
这已经很棒了。问题是源文件必须是:
代替:
这迫使我反转我想要使用的所有图像的颜色。我在谷歌上搜索了这个tikzfadingfrompicture
函数,但没有找到任何有用的东西。(tikz
文档中也没有。)有没有一种简单的方法来操作上面的代码,这样就不需要反转原始图片的颜色了?提前感谢任何回答。
答案1
正如您自己所说,您的方法“反转了图片”,即执行了您手动执行的步骤。您可以将此结果存储在 中\savebox
, 的反转版本将给出所需的结果。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newsavebox{\cheat}
\begin{document}
\begin{tikzfadingfrompicture}[name=theimage,inner sep=0]
\node[fill=transparent!0]
{\includegraphics[scale=0.3]{example-image-a}};
\end{tikzfadingfrompicture}
\savebox\cheat{\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\includegraphics[scale=0.3]{example-image-a}}};
\fill[path fading=theimage,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}}
\begin{tikzfadingfrompicture}[name=inverted,inner sep=0]
\node[fill=transparent!0]
{\usebox\cheat};
\end{tikzfadingfrompicture}
\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}};
\fill[magenta,path fading=inverted,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}
\\
original & inverted & inverted twice
\end{tabular}
\end{document}
您可以将其制作成宏,这样就不必重做这些步骤。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\newsavebox{\cheat}
\newcommand{\FadeShade}[2][]{\begin{tikzfadingfrompicture}[name=theimage,inner sep=0]
\node[fill=transparent!0]
{#2};
\end{tikzfadingfrompicture}%
\savebox\cheat{\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{#2}};
\fill[path fading=theimage,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}}%
\begin{tikzfadingfrompicture}[name=inverted,inner sep=0]
\node[fill=transparent!0]
{\usebox\cheat};
\end{tikzfadingfrompicture}
\begin{tikzpicture}[inner sep=0]
\node(img){\phantom{\usebox\cheat}};
\fill[#1,path fading=inverted,fit fading=false] (img.south west) rectangle (img.north east);
\end{tikzpicture}}
\begin{document}
\FadeShade[magenta]{\includegraphics[scale=0.3]{example-image-a}}
~
\FadeShade[blue]{\includegraphics[scale=0.6]{example-image-duck}}
\end{document}