TiKz:部分着色节点

TiKz:部分着色节点

我想对节点进行部分着色,就像渐变一样,但只在某些特定部分。例如,前 50% 为黑色,然后其他 25% 为黑色!80,然后接下来的 12.5% 为黑色!60,以任意数量的子部分继续下去,在这种情况下,始终使用前一个空间的一半,就像 $\frac{1}{2^n}$ 的无限和,如图所示

enter image description here

另外,随意选择颜色也很好,比如红色、蓝色、绿色、黄色、洋红色、棕色等。比如

enter image description here

这可行吗?如果可行,该怎么做?

PS:这是在 GIMP 上制作的。

答案1

当然,但是没有节点会更容易:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\begin{scope}
    \clip (0.5,0.5) circle[radius=0.5];
    \foreach \i/\c in {0/100,1/80,2/60,3/40,4/20,5/10} {
        \fill[black!\c] ({1-1/pow(2,\i)},0) rectangle (1,1);
    }
\end{scope}

\begin{scope}[yshift=-1.5cm]
    \clip (0.5,0.5) circle[radius=0.5];
    \foreach \i/\c in {0/red,1/blue,2/green,3/yellow,4/magenta,5/brown} {
        \fill[\c] ({1-1/pow(2,\i)},0) rectangle (1,1);
    }
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here


如果您仍然希望用这种模式填充节点,则可以使用path pictures:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
    gray steps/.style={
        path picture={
            \foreach \i/\c in {0/100,1/80,2/60,3/40,4/20,5/10} {
                \fill[black!\c] 
                    let \p1=($(path picture bounding box.east)-(path picture bounding box.west)$) in
                    ([xshift={(1-1/pow(2,\i))*\x1}]path picture bounding box.south west)
                    rectangle 
                    (path picture bounding box.north east);
            }
        }
    },
    colorful steps/.style={
        path picture={
            \foreach \i/\c in {0/red,1/blue,2/green,3/yellow,4/magenta,5/brown} {
                \fill[\c] 
                    let \p1=($(path picture bounding box.east)-(path picture bounding box.west)$) in
                    ([xshift={(1-1/pow(2,\i))*\x1}]path picture bounding box.south west)
                    rectangle 
                    (path picture bounding box.north east);
            }
        }
    }
}

\begin{document}
\begin{tikzpicture}

\node[gray steps, circle, text width=1cm] at (0,0) {};

\node[colorful steps, circle, text width=1cm] at (0,-1.5) {};

\node[colorful steps, text width=1cm] at (0,-2.5) {};

\end{tikzpicture}
\end{document}

enter image description here


如果您需要多个节点,并且除了颜色及其数量之外,模式始终相同,则可以将上述内容概括为:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
    colorful steps/.style={
        path picture={
            \foreach \c [count=\i from 0] in {#1} {
                \fill[\c] 
                    let \p1=($(path picture bounding box.east)-(path picture bounding box.west)$) in
                    ([xshift={(1-1/pow(2,\i))*\x1}]path picture bounding box.south west)
                    rectangle 
                    (path picture bounding box.north east);
            }
        }
    }
}

\begin{document}
\begin{tikzpicture}

\node[colorful steps={red,blue}, circle, text width=1cm] at (0,0) {};

\node[colorful steps={red,blue,green,yellow,magenta,brown}, circle, text width=1cm] at (0,-1.5) {};

\node[colorful steps={black, black!80, black!60}, circle, text width=1cm] at (0,-3) {};

\end{tikzpicture}
\end{document}

enter image description here

相关内容