我想要一条彩色线,以透明 100 开始,以透明 30 结束。
我尝试了这个代码:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.markings}
\makeatletter
\pgfkeys{/pgf/decoration/.cd,
start shade/.store in = \startshade,
end shade/.store in = \endshade,
}
\pgfdeclaredecoration{shade change}{initial}{
\state{initial}[
width = \pgflinewidth,
next state = line,
persistent precomputation = {\pgfmathdivide{(\startshade-\endshade)/100}{\pgfdecoratedpathlength}%
\let\@increment=\pgfmathresult
\pgfmathsetmacro\@pitch{\pgfdecoratedpathlength/100}
\def\@@n{0}
\def\@sh{\startshade}}
]{}
\state{line}[
width = \pgflinewidth,
persistent postcomputation = {\pgfmathsetmacro\@@n{\@@n+1}
\pgfmathsetmacro\@sh{\startshade/100-\@increment*\@@n}}]
{
\pgfsetstrokeopacity{\@sh}
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfqpoint{\@pitch pt}{0pt}}
\pgfusepath{stroke}}
\state{final}{
\pgfpathmoveto{\pgfpointorigin}%
\pgfsetstrokeopacity{\@sh}
\pgfusepath{stroke}%
}
}
\makeatother
\begin{document}
\centering
\begin{tikzpicture}
\draw[
line width = 2pt,
decoration = {shade change,
start shade = 100,
end shade = 30},
decorate] (0,0)--++(3,3)node[midway,above]{$I$};
\end{tikzpicture}
\end{document}
但结果如下:
我哪里做错了?
答案1
类似这样的:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,fadings,decorations.markings}
\makeatletter
\pgfkeys{/pgf/decoration/.cd,
start shade/.store in = \startshade,
end shade/.store in = \endshade,
}
\pgfdeclaredecoration{shade change}{initial}{
\state{initial}[
width = \pgflinewidth,
next state = line,
persistent precomputation = {\pgfmathdivide{(\startshade-\endshade)/100}%
{\pgfdecoratedpathlength}%
\let\@increment=\pgfmathresult
\pgfmathsetmacro\@pitch{\pgfdecoratedpathlength/100}
\def\@@n{0}
\def\x{\@@n}
\def\@sh{\startshade}}
]{}
\state{line}[
width = \pgflinewidth,
persistent postcomputation = {\pgfmathsetmacro\@@n{\@@n+1}
\pgfmathsetmacro\@sh{\startshade/100-\@increment*\@@n}}]
{%
\pgfsetstrokecolor{black!\@@n!white}%
\pgfpathmoveto{\pgfpointorigin}
\pgfpathlineto{\pgfqpoint{\@pitch pt}{0pt}}
\pgfusepath{stroke}}
\state{final}{
\pgfpathmoveto{\pgfpointorigin}%
\pgfsetstrokecolor{black!\@@n!white}%
\pgfusepath{stroke}%
}
}
\makeatother
\begin{document}
\centering
\begin{tikzpicture}
\draw[
line width = 2pt,
decoration = {shade change,
start shade = 100,
end shade = 30},
decorate] (0,0)--++(3,3)node[midway,above]{$I$};
\end{tikzpicture}
\end{document}