数字列表作为命令选项,数字将用于定义颜色

数字列表作为命令选项,数字将用于定义颜色

我试图将数字列表作为选项传递给命令。这些数字稍后将用于定义图片内的颜色tikz。我可以做的第一部分,下面是一个例子

\documentclass[border = 5pt]{standalone}

\usepackage{tikz}

% keys
\pgfkeys{
 /namespace/.is family,
 /namespace,
 color fractions/.store in = \colorfractions,
}

% command
\newcommand\colorcommand[1][]{%
 \pgfkeys{/namespace, #1}%
 %
 \begin{tikzpicture}[]
   % https://tex.stackexchange.com/questions/45040/pgf-tikz-how-to-store-strings-in-array
   \node[draw] at (0, 0){\pgfmathparse{\colorfractions[1]}\pgfmathresult};
 \end{tikzpicture}%
}

\begin{document}

\colorcommand[%
  color fractions = {{{0.1,0.2,0.3}}},
]

\end{document}

在此处输入图片描述

第二部分给我带来了问题。例如,我可以使用回答定义颜色,

(...)
\begin{tikzpicture}[]

   % https://tex.stackexchange.com/questions/19190/how-to-define-a-random-number-with-xcolor-and-tikz-pgf
   \xdefinecolor{localcolor}{rgb}{\pgfmathparse{\colorfractions[1]}\pgfmathresult, 1.0, 1.0}

   % https://tex.stackexchange.com/questions/45040/pgf-tikz-how-to-store-strings-in-array
   \node[draw, fill = localcolor] at (0, 0){\pgfmathparse{\colorfractions[1]}\pgfmathresult};
 \end{tikzpicture}%
(...)

但失败了!有什么建议吗?谢谢

答案1

您不能像这样扩展颜色,但以下方法有效:

\documentclass[border = 5pt]{standalone}

\usepackage{tikz}

% keys
\pgfkeys{
 /namespace/.is family,
 /namespace,
 color fractions/.store in = \colorfractions,
}

% command
\newcommand\colorcommand[1][]{%
 \pgfkeys{/namespace, #1}%
 %
 \begin{tikzpicture}[]
    \pgfmathsetmacro{\myfraction}{\colorfractions[1]}
    \xdefinecolor{localcolor}{rgb}{\myfraction, 1.0, 1.0}
   % https://tex.stackexchange.com/questions/45040/pgf-tikz-how-to-store-strings-in-array
   \node[draw,fill=localcolor] at (0, 0){\pgfmathparse{\colorfractions[1]}\pgfmathresult};
 \end{tikzpicture}%
}

\begin{document}

\colorcommand[%
  color fractions = {{{0.1,0.2,0.3}}},
]

\end{document}

在此处输入图片描述

它也适用于\definecolor{localcolor}{rgb}{\myfraction, 1.0, 1.0}代替\xdefinecolor

\pgfmathparse{\colorfractions[1]}
\definecolor{localcolor}{rgb}{\pgfmathresult, 1.0, 1.0}

或者

\pgfmathparse{\colorfractions[1]}
\xdefinecolor{localcolor}{rgb}{\pgfmathresult, 1.0, 1.0}

相关内容