是否可以使用 tikz 及其 calc 库来计算这样的颜色值?
\filldraw[fill=black!{50-95.7/2},draw=black] (1,0) rectangle (2,1); %Gives a 'Missing number, treated as zero.' error
我知道我可以定义一种颜色使用宏,但我有很多这样的行,我想保持代码简洁易读。
答案1
噢,这是丑陋的...
\documentclass[tikz,border=5]{standalone}
\def\atchar{@}
\tikzset{set color/.style 2 args={.. set color={#2!@!@;0;;#1;}},
.. set color/.code args={#1!#2;#3;#4;#5;}{
\def\tmp{#1}\ifx\tmp\atchar%
\tikzset{#5=#4}%
\else%
\ifcase#3\relax
\tikzset{.. set color/.expanded={#2;1;#1;#5;}}%
\or
\pgfmathparse{max(min(100,#1),0)}%
\tikzset{.. set color/.expanded={#2;2;#4!\pgfmathresult;#5;}}%
\else
\tikzset{.. set color/.expanded={#2;1;#4!#1;#5;}}%
\fi
\fi%
},
fill color/.style={set color={fill}{#1}},
draw color/.style={set color={draw}{#1}},
shade left color/.style={set color={left color}{#1}},
shade right color/.style={set color={right color}{#1}}
}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0, 10, ..., 360}
\filldraw [fill color=cyan!sin(\i)*50 + 50, draw color=red!\i/3.6!yellow,
ultra thick] (\i:2) circle [radius=1/10];
\shade [shade left color=purple!pi*25, shade right color=orange!50 * 1.5]
circle [radius=1];
\end{tikzpicture}
\end{document}