Tikz 混合模式如何工作?

Tikz 混合模式如何工作?

简而言之,我希望做与之前完全相同的事情问题,但适用于任何颜色。或者至少更好地了解它的工作原理。

更准确地说:我想根据背景颜色自动更改文本的颜色(这是在前面提到的问题中完成的)。使用混合模式给出的答案对我来说有点“神奇”:它适用于蓝色和白色。但我想知道如何对任何一对颜色(比如说和colorA)进行相同的操作colorB。我阅读了有关混合模式的文档,但我没有找到可以逆向工程的公式或算法来重现任何颜色的“神奇”答案(或至少了解它的工作原理)。

以下是 MWE:

\documentclass{beamer}
\usepackage{tikz}
\usetheme{Madrid}

\definecolor{colorA}{RGB}{255,0,0} % Red for the example but could be any other color
\definecolor{colorB}{RGB}{0,0,255} % Blue for the example but could be or any other color

% Define colorC and colorD to fit with the blend of colorA and colorB
\colorlet{colorC}{colorA!50!colorB} % 
\colorlet{colorD}{colorB!50!colorA} % or maybe something like R_A + R_B, G_A + G_B, B_A + B_B

\usebackgroundtemplate{
    \begin{tikzpicture}
        \coordinate (A) at (0,0);
        \coordinate (B) at (0,\paperheight);
        \coordinate (C) at (\paperwidth,0);
        \coordinate (D) at (\paperwidth,\paperheight);
        \fill[colorA] (A) -- (C) -- (B) -- cycle;
        \fill[colorB](D) -- (C) -- (B) -- cycle;
    \end{tikzpicture}
}
\begin{document}

\begin{frame}
    \frametitle{Example}
    \pgfsetblendmode{difference}% or another blend mode
    \color{colorC} Could this text appear with colorA on a colorB background and with colorB on colorA background?
\end{frame}

\end{document}

答案1

这并没有真正回答问题(我相信对于任何给定的颜色来说这都是不可能的),但这是一种实现结果的方法。

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fadings}
\definecolor{colorA}{RGB}{200,0,0}
\definecolor{colorB}{RGB}{0,0,200}

\begin{tikzfadingfrompicture}[name=myfading]
\fill[transparent!0] (-1.5,-1) -- (-1.5,1) -- (1.5,-1) --cycle;
\node[transparent!100] {\Huge\bf Test};
\clip (-1.5,1) -- (1.5,1) -- (1.5,-1) --cycle;
\node[transparent!0] {\Huge\bf Test};
\end{tikzfadingfrompicture}

\begin{tikzfadingfrompicture}[name=myinversefading]
\fill[transparent!0] (-1.5,-1) rectangle (1.5,1);
\fill[transparent!100, path fading=myfading, fit fading=false] (-1.5,-1) rectangle (1.5,1);
\end{tikzfadingfrompicture}

\begin{document}
\begin{tikzpicture}
\fill[colorA, path fading=myfading, fit fading=false] (-1.5,-1) rectangle (1.5,1);
\fill[colorB, path fading=myinversefading, fit fading=false] (-1.5,-1) rectangle (1.5,1);
\end{tikzpicture}
\end{document}

文本和背景分为两种颜色

编辑:tikzfadingfrompicture如果后台不需要其他东西,也可以只用一个

\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{fadings}
\definecolor{colorA}{RGB}{200,0,0}
\definecolor{colorB}{RGB}{0,0,200}

\begin{tikzfadingfrompicture}[name=myfading]
\fill[transparent!0] (-1.5,-1) -- (-1.5,1) -- (1.5,-1) --cycle;
\node[transparent!100] {\Huge\bf Test};
\clip (-1.5,1) -- (1.5,1) -- (1.5,-1) --cycle;
\node[transparent!0] {\Huge\bf Test};
\end{tikzfadingfrompicture}

\begin{document}
\begin{tikzpicture}
\fill[colorA] (-1.5,-1) rectangle (1.5,1);
\fill[colorB, path fading=myfading, fit fading=false] (-1.5,-1) rectangle (1.5,1);
\end{tikzpicture}
\end{document}

或者完全没有,tikzfadingfrompicture如果目标只是一个结果:

\documentclass[tikz, border=1cm]{standalone}
\definecolor{colorA}{RGB}{200,0,0}
\definecolor{colorB}{RGB}{0,0,200}
\begin{document}
\begin{tikzpicture}
\fill[colorA] (-1.5,-1) -- (1.5,-1) -- (-1.5,1) --cycle;
\node[colorB] {\Huge\bf Test};
\clip (-1.5,1) -- (1.5,1) -- (1.5,-1) --cycle;
\fill[colorB] (-1.5,1) -- (1.5,1) -- (1.5,-1) --cycle;
\node[colorA] {\Huge\bf Test};
\end{tikzpicture}
\end{document}

相关内容