tikz - 向宏添加更多参数不起作用

tikz - 向宏添加更多参数不起作用

參考所以,我想再添加一个矩阵来绘制另一个立方体,所以想更改代码但使用参数来设置 RGB 坐标(将 matCC 更改为 matPC,并将其用作参数)。我将其更改为以下形式,但编译失败!

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\def\matPC{{0.257, 0.504, 0.098},% 
    {-0.148, -0.291, 0.439},%
    {0.439, -0.368,0.071}}%
\pgfmathdeclarefunction{RGBvec}{4}{% r,g,b,mat
    \begingroup%
    \pgfmathsetmacro{\myY}{16+{#4}[0][0]*#1+{#4}[0][1]*#2+{#4}[0][2]*#3}%
    \pgfmathsetmacro{\myCb}{128+{#4}[1][0]*#1+{#4}[1][1]*#2+{#4}[1][2]*#3}%
    \pgfmathsetmacro{\myCr}{128+{#4}[2][0]*#1+{#4}[2][1]*#2+{#4}[2][2]*#3}%
    \edef\pgfmathresult{\myCr,\myCb,\myY}%
    \pgfmathsmuggle\pgfmathresult\endgroup%
}%

\begin{document}
    \tdplotsetmaincoords{70}{110}
    \begin{tikzpicture}[bullet/.style={circle,inner
        sep=2pt,fill},line cap=round,line join=round,
    RGB coordinate/.code args={(#1,#2,#3),#4)}{\pgfmathparse{RGBvec(#1,#2,#3,#4)}%
        \tikzset{insert path={(\pgfmathresult)}}},font=\sffamily,thick]
    \begin{scope}[tdplot_main_coords,scale=1/40]
    \draw[-stealth] (0,0,0) coordinate (O) -- (280,0,0) coordinate[label=below:Cr] (Cr);
    \draw[-stealth]  (O) -- (0,280,0) coordinate[label=below:Cb] (Cb);
    \draw[-stealth]  (O) -- (0,0,280) coordinate[label=left:Y] (Y);
    \path [RGB coordinate={(255,255,255),\matPC}] node[bullet,draw,fill=white] (white){}
    [RGB coordinate={(0,0,0),\matPC}] node[bullet] (black){}
    [RGB coordinate={(255,0,0),\matPC}] node[bullet,red] (red){}
    [RGB coordinate={(0,255,0),\matPC}] node[bullet,green] (green){}
    [RGB coordinate={(0,0,255),\matPC}] node[bullet,blue] (blue){}
    [RGB coordinate={(255,0,255),\matPC}] node[bullet,magenta] (magenta){}
    [RGB coordinate={(255,255,0),\matPC}] node[bullet,yellow] (yellow){}
    [RGB coordinate={(0,255,255),\matPC}] node[bullet,cyan] (cyan){};
    \draw (red) -- (black) -- (blue) -- (magenta) -- (red) -- (yellow) 
    -- (green) edge (black) -- (cyan) edge (blue) -- (white)  edge (magenta) -- (yellow); 
    \draw[thin] (255,0,0) node[left]{255} -- (255,255,0) -- (0,255,0) node[above]{255}
    (0,0,255) node[left]{255} -- (255,0,255) edge (255,0,0)
    -- (255,255,255)  edge (255,255,0) -- (0,255,255)  edge (0,255,0)
    -- cycle ;
    \end{scope}  
    \end{tikzpicture}
\end{document}

编译错误:

File ended while scanning use of \pgfkeys@code.

答案1

这是一个可以工作的版本。为了使它工作,我

  • 删除了一个额外的),即被code args={(#1,#2,#3),#4)}替换为code args={(#1,#2,#3),#4}without )after #4,并且
  • 包裹#4在……里"...",即\pgfmathparse{RGBvec(#1,#2,#3,#4)}变成\pgfmathparse{RGBvec(#1,#2,#3,"#4")}……

结果:

\documentclass[tikz,border=3mm]{standalone}
\usepackage{tikz-3dplot}
\def\matPC{{0.257, 0.504, 0.098},% 
    {-0.148, -0.291, 0.439},%
    {0.439, -0.368,0.071}}%
\pgfmathdeclarefunction{RGBvec}{4}{% r,g,b,mat
    \begingroup%
    \pgfmathsetmacro{\myY}{16+{#4}[0][0]*#1+{#4}[0][1]*#2+{#4}[0][2]*#3}%
    \pgfmathsetmacro{\myCb}{128+{#4}[1][0]*#1+{#4}[1][1]*#2+{#4}[1][2]*#3}%
    \pgfmathsetmacro{\myCr}{128+{#4}[2][0]*#1+{#4}[2][1]*#2+{#4}[2][2]*#3}%
    \edef\pgfmathresult{\myCr,\myCb,\myY}%
    \pgfmathsmuggle\pgfmathresult\endgroup%
}%

\begin{document}
    \tdplotsetmaincoords{70}{110}
    \begin{tikzpicture}[bullet/.style={circle,inner
        sep=2pt,fill},line cap=round,line join=round,
    RGB coordinate/.code args={(#1,#2,#3),#4}{\pgfmathparse{RGBvec(#1,#2,#3,"#4")}%
        \tikzset{insert path={(\pgfmathresult)}}},font=\sffamily,thick]
    \begin{scope}[tdplot_main_coords,scale=1/40]
    \draw[-stealth] (0,0,0) coordinate (O) -- (280,0,0) coordinate[label=below:Cr] (Cr);
    \draw[-stealth]  (O) -- (0,280,0) coordinate[label=below:Cb] (Cb);
    \draw[-stealth]  (O) -- (0,0,280) coordinate[label=left:Y] (Y);
    \path [RGB coordinate={(255,255,255),\matPC}] node[bullet,draw,fill=white] (white){}
    [RGB coordinate={(0,0,0),\matPC}] node[bullet] (black){}
    [RGB coordinate={(255,0,0),\matPC}] node[bullet,red] (red){}
    [RGB coordinate={(0,255,0),\matPC}] node[bullet,green] (green){}
    [RGB coordinate={(0,0,255),\matPC}] node[bullet,blue] (blue){}
    [RGB coordinate={(255,0,255),\matPC}] node[bullet,magenta] (magenta){}
    [RGB coordinate={(255,255,0),\matPC}] node[bullet,yellow] (yellow){}
    [RGB coordinate={(0,255,255),\matPC}] node[bullet,cyan] (cyan){};
    \draw (red) -- (black) -- (blue) -- (magenta) -- (red) -- (yellow) 
    -- (green) edge (black) -- (cyan) edge (blue) -- (white)  edge (magenta) -- (yellow); 
    \draw[thin] (255,0,0) node[left]{255} -- (255,255,0) -- (0,255,0) node[above]{255}
    (0,0,255) node[left]{255} -- (255,0,255) edge (255,0,0)
    -- (255,255,255)  edge (255,255,0) -- (0,255,255)  edge (0,255,0)
    -- cycle ;
    \end{scope}  
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容