我正在尝试绘制一条具有双色渐变的路径,首先从洋红色到青色,然后从青色到洋红色。
我曾尝试使用以下代码这线程,但我能做的最好的是这样的:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{multicolor/.style={
postaction={
decorate,
decoration={
markings,
mark=between positions 0 and 0.5*\pgfdecoratedpathlength step 0.5pt with {
\pgfmathsetmacro\myval{multiply(divide(
\pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, 1*\pgfdecoratedpathlength),200)};
\pgfsetfillcolor{cyan!\myval!magenta};
\pgfpathcircle{\pgfpointorigin}{#1};
\pgfusepath{fill};},
mark=between positions 0.5*\pgfdecoratedpathlength and \pgfdecoratedpathlength step 0.5pt with {
\pgfmathsetmacro\myval{300+multiply(divide(
\pgfkeysvalueof{/pgf/decoration/mark info/distance from start}, 1*\pgfdecoratedpathlength),200)};
\pgfsetfillcolor{magenta!\myval!cyan};
\pgfpathcircle{\pgfpointorigin}{#1};
\pgfusepath{fill};},
}}}}
\begin{document}
\begin{tikzpicture}
\path [multicolor=0.3mm,draw] (0,0) .. controls (1,3) and (5,-7) .. (4,1) {};
\end{tikzpicture}
\end{document}
我希望路径的第二部分逐渐从青色变为洋红色。
提前致谢
答案1
只是对公式进行了调整。
我还添加了overlay
路径本身,以便边界框不包含控制点,但\pgf@relevantforpicturesizetrue
(即overlay = false
)圆圈本身被计入图片的边界框。
路径本身没有被绘制,并且multicolor
样式不再包含任何内容postaction
,因为无论如何该线都会被圆圈隐藏。
代码
\documentclass[tikz]{standalone}
%\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\tikzset{
multicolor/.style={
decorate,
decoration={
markings,
mark=between positions 0 and 0.5*\pgfdecoratedpathlength step 0.5pt with {
\csname pgf@relevantforpicturesizetrue\endcsname % overlay = false
\pgfmathparse{multiply(divide(
\pgfkeysvalueof{/pgf/decoration/mark info/distance from start},
\pgfdecoratedpathlength),200)}
\pgfsetfillcolor{cyan!\pgfmathresult!magenta}
\pgfpathcircle{\pgfpointorigin}{#1}
\pgfusepath{fill}},
mark=between positions 0.5*\pgfdecoratedpathlength and
\pgfdecoratedpathlength step 0.5pt with {
\csname pgf@relevantforpicturesizetrue\endcsname % overlay = false
\pgfmathparse{subtract(multiply(divide(
\pgfkeysvalueof{/pgf/decoration/mark info/distance from start},
\pgfdecoratedpathlength),200),100)}
\pgfsetfillcolor{magenta!\pgfmathresult!cyan}
\pgfpathcircle{\pgfpointorigin}{#1}
\pgfusepath{fill}}}}}
\begin{document}
\tikz % overlay because of control points
\path[multicolor=0.3mm, overlay] (0,0) .. controls (1,3) and (5,-7) .. (4,1);
\end{document}