使用路径选项设置自定义阴影的颜色

使用路径选项设置自定义阴影的颜色

我正在使用自定义阴影来制作圆柱形条。

阴影使用颜色列表中定义的颜色。

我可以使用表达式来更改颜色\colorlet。但我希望能够使用绘制命令中的选项来实现这一点。

这是我的代码,我最近尝试设置颜色时,已注释掉。我在文档或 stackexchange 上找不到任何内容来告诉我如何操作。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{shadings}

\pgfdeclareradialshading[mycolor,white]
  {barendshading}
  {\pgfpoint{0bp}{5bp}}
  {color(0bp)=(white);
   color(28bp)=(mycolor);
   color(60bp)=(mycolor)}

\pgfdeclareverticalshading[mycolor,white]
  {barshading}
  {100bp}
  {color(0bp)=(mycolor); 
   color(25bp)=(mycolor); 
   color(55bp)=(white); 
   color(75bp)=(mycolor);
   color(100bp)=(mycolor)}

\def\cylindricalsphere{
  \begin{pgfscope}
    \pgfpathcircle{\pgfpoint{0}{0}}{5pt}
    \pgfshadepath{barendshading}{\pgfdecoratedangle}
  \end{pgfscope}}

\pgfdeclaredecoration{cylindricalbarspheres}{initial}
{
  \state{initial}[width=1pt,next state=middle]{\cylindricalsphere}
  \state{middle}[width=1pt]{}
  \state{final}{\cylindricalsphere}}

\def\cylindricalsegment{
  \begin{pgfscope}
    \pgfpathrectanglecorners
      {\pgfpoint{-.55pt}{-5pt}}
      {\pgfpoint{.55pt}{5pt}}
    \pgfshadepath{barshading}{\pgfdecoratedangle}
  \end{pgfscope}}

\pgfdeclaredecoration{cylindricalbarsegments}{initial}
    {
      \state{initial}[width=1pt,next state=middle]{
        %\pgfkeysgetvalue{/tikz/path color}{pcolor}
        %\colorlet{mycolor}{pcolor}
        \cylindricalsegment}
      \state{middle}[width=1pt]{
        \cylindricalsegment}
      \state{final}{}
    } 

\tikzset{
    cylindricalbar/.style={
        preaction={decorate,decoration=cylindricalbarspheres},
        postaction={decorate,decoration=cylindricalbarsegments},
    }}

\begin{document}
  \begin{tikzpicture}


  \colorlet{mycolor}{blue}
  \path [cylindricalbar,color=green] (0,0) arc (90:0:3.5);
 \end{tikzpicture}
\end{document}

蓝色阴影条

答案1

您可以声明一个新的TikZ option来存储颜色并将其用于阴影声明中。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{shadings}

\tikzset{
    /tikz/cylindrical shading color/.store in=\mycolor,
    /tikz/cylindrical shading color=black
}

\pgfdeclareradialshading[\mycolor]
  {barendshading}
  {\pgfpoint{0bp}{5bp}}
  {color(0bp)=(white);
   color(28bp)=(\mycolor);
   color(60bp)=(\mycolor)}

\pgfdeclareverticalshading[\mycolor]
  {barshading}
  {100bp}
  {color(0bp)=(\mycolor); 
   color(25bp)=(\mycolor); 
   color(55bp)=(white); 
   color(75bp)=(\mycolor);
   color(100bp)=(\mycolor)}

\def\cylindricalsphere{
  \begin{pgfscope}
    \pgfpathcircle{\pgfpoint{0}{0}}{5pt}
    \pgfshadepath{barendshading}{\pgfdecoratedangle}
  \end{pgfscope}}

\pgfdeclaredecoration{cylindricalbarspheres}{initial}
{
  \state{initial}[width=1pt,next state=middle]{\cylindricalsphere}
  \state{middle}[width=1pt]{}
  \state{final}{\cylindricalsphere}}

\def\cylindricalsegment{
  \begin{pgfscope}
    \pgfpathrectanglecorners
      {\pgfpoint{-.55pt}{-5pt}}
      {\pgfpoint{.55pt}{5pt}}
    \pgfshadepath{barshading}{\pgfdecoratedangle}
  \end{pgfscope}}

\pgfdeclaredecoration{cylindricalbarsegments}{initial}
    {
      \state{initial}[width=1pt,next state=middle]{
        %\pgfkeysgetvalue{/tikz/path color}{pcolor}
        %\colorlet{mycolor}{pcolor}
        \cylindricalsegment}
      \state{middle}[width=1pt]{
        \cylindricalsegment}
      \state{final}{}
    } 

\tikzset{
    cylindricalbar/.style={
        preaction={decorate,decoration=cylindricalbarspheres},
        postaction={decorate,decoration=cylindricalbarsegments},
    }}

\begin{document}
  \begin{tikzpicture}
  \path [cylindricalbar, cylindrical shading color=red] (0,0) arc (90:0:3.5);
  \path [cylindricalbar, cylindrical shading color=green] (0,-0.5) arc (90:0:2.75);
  \path [cylindricalbar, cylindrical shading color=blue] (0,-1) arc (90:0:2);
  \path [cylindricalbar] (0,-1.5) arc (90:0:1.5);
 \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我倾向于使用.code处理程序并直接设置颜色。

  cylindrical bar colour/.code={
    \colorlet{mycolor}{#1}%
  },
  cylindrical bar colour=black,

这将创建一个键cylindrical bar colour,其参数将用于设置mycolorblack用作初始值。

然后,

\begin{tikzpicture}
  \path [cylindricalbar] (0,.5)  arc (90:-5:4);
  \path [cylindricalbar, cylindrical bar colour=green] (0,0) arc (90:-5:3.5);
  \path [cylindricalbar, cylindrical bar colour=blue] (0,1) arc (90:-5:4.5);
  \path [cylindricalbar, cylindrical bar colour=magenta] (0,1.5) arc (90:-5:5);
\end{tikzpicture}

给出

多色圆柱条

完整代码:

\documentclass[border=10pt,tikz]{standalone}
\usetikzlibrary{arrows.meta,decorations,shadings}
\pgfdeclareradialshading[mycolor,white]
{barendshading}
{\pgfpoint{0bp}{5bp}}
{color(0bp)=(white);
  color(28bp)=(mycolor);
  color(60bp)=(mycolor)}
\pgfdeclareverticalshading[mycolor,white]
{barshading}
{100bp}
{color(0bp)=(mycolor);
  color(25bp)=(mycolor);
  color(55bp)=(white);
  color(75bp)=(mycolor);
  color(100bp)=(mycolor)}
\def\cylindricalsphere{
  \begin{pgfscope}
    \pgfpathcircle{\pgfpoint{0}{0}}{5pt}
    \pgfshadepath{barendshading}{\pgfdecoratedangle}
  \end{pgfscope}}
\pgfdeclaredecoration{cylindricalbarspheres}{initial}
{
  \state{initial}[width=1pt,next state=middle]{\cylindricalsphere}
  \state{middle}[width=1pt]{}
  \state{final}{\cylindricalsphere}}
\def\cylindricalsegment{
  \begin{pgfscope}
    \pgfpathrectanglecorners
    {\pgfpoint{-.55pt}{-5pt}}
    {\pgfpoint{.55pt}{5pt}}
    \pgfshadepath{barshading}{\pgfdecoratedangle}
  \end{pgfscope}}
\pgfdeclaredecoration{cylindricalbarsegments}{initial}
{
  \state{initial}[width=1pt,next state=middle]{
    %\pgfkeysgetvalue{/tikz/path color}{pcolor}
    %\colorlet{mycolor}{pcolor}
    \cylindricalsegment}
  \state{middle}[width=1pt]{
    \cylindricalsegment}
  \state{final}{}
}
\tikzset{
  cylindricalbar/.style={
    preaction={decorate,decoration=cylindricalbarspheres},
    postaction={decorate,decoration=cylindricalbarsegments},
  },
  cylindrical bar colour/.code={
    \colorlet{mycolor}{#1}%
  },
  cylindrical bar colour=black,
}
\begin{document}
\begin{tikzpicture}
  \path [cylindricalbar] (0,.5)  arc (90:-5:4);
  \path [cylindricalbar, cylindrical bar colour=green] (0,0) arc (90:-5:3.5);
  \path [cylindricalbar, cylindrical bar colour=blue] (0,1) arc (90:-5:4.5);
  \path [cylindricalbar, cylindrical bar colour=magenta] (0,1.5) arc (90:-5:5);
\end{tikzpicture}
\end{document}

顺便问一下,在某些情况下,您的圆柱末端会消失吗?我不得不做一些实验来获得 4 个都有两个圆形末端的圆弧。

相关内容