tikz 根据参数改变颜色

tikz 根据参数改变颜色

我正在尝试制作一个元素周期表,其中元素的颜色根据其电负性而变化,例如这里

谢谢这个答案,我可以像这样创建表格(前两行):

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
    \newcommand{\element}[5]{%
        \coordinate (el) at (#2,-#3);
        \coordinate (base) at ($(.95,0)$);
        \coordinate (height) at ($(0,.95)$);
        \coordinate (diag) at ($(base)+(height)$);
        \filldraw[rounded corners, fill=yellow!0.55!red] ($(el)-.5*(diag)$) rectangle +(diag);
        \node[white] at (el) {\sffamily\Large \strut#1};
        \node[white, inner sep=2pt] (en) at ($(el)-.5*(height)$) [anchor=south] {\sffamily\tiny #5};
        \node[white, inner sep=2pt] (atnum) at ($(el)+.5*(height)-.5*(base)$) [anchor=north west] {\sffamily\tiny #4};
    }

    \newcommand{\grayelement}[5]{%
        \coordinate (el) at (#2,-#3);
        \coordinate (base) at ($(.95,0)$);
        \coordinate (height) at ($(0,.95)$);
        \coordinate (diag) at ($(base)+(height)$);
        \filldraw[rounded corners, fill=gray!70] ($(el)-.5*(diag)$) rectangle +(diag);
        \node[white] at (el) {\sffamily\Large \strut#1};
        \node[white, inner sep=2pt] (en) at ($(el)-.5*(height)$) [anchor=south] {\sffamily\tiny #5};
        \node[white, inner sep=2pt] (atnum) at ($(el)+.5*(height)-.5*(base)$) [anchor=north west] {\sffamily\tiny #4};
    }

    \begin{tikzpicture}
        \element{H}{1}{1}{1}{2.20}
        \grayelement{He}{18}{1}{2}{--}

        \element{Li}{1}{2}{3}{0.98}
        \element{Be}{2}{2}{4}{1.57}
        \element{B}{13}{2}{5}{2.04}
        \element{C}{14}{2}{6}{2.55}
        \element{N}{15}{2}{7}{3.04}
        \element{O}{16}{2}{8}{3.44}
        \element{F}{17}{2}{9}{3.98}
        \grayelement{Ne}{18}{2}{10}{--}
    \end{tikzpicture}
\end{document}

我想做类似的事情fill=yellow!#5/4!red,但我不知道该怎么做。#5 是电负性的参数,我想除以 4,因为最高电负性是 3.98

答案1

使用#5/4*100\pgfmathsetmacro

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
    \newcommand{\element}[5]{%
        \coordinate (el) at (#2,-#3);
        \coordinate (base) at ($(.95,0)$);
        \coordinate (height) at ($(0,.95)$);
        \coordinate (diag) at ($(base)+(height)$);                 
        \pgfmathsetmacro{\myperc}{#5/4*100}
        \filldraw[rounded corners, fill=yellow!\myperc!red] ($(el)-.5*(diag)$) rectangle +(diag);
        \node[white] at (el) {\sffamily\Large \strut#1};
        \node[white, inner sep=2pt] (en) at ($(el)-.5*(height)$) [anchor=south] {\sffamily\tiny #5};
        \node[white, inner sep=2pt] (atnum) at ($(el)+.5*(height)-.5*(base)$) [anchor=north west] {\sffamily\tiny #4};
    }

    \newcommand{\grayelement}[5]{%
        \coordinate (el) at (#2,-#3);
        \coordinate (base) at ($(.95,0)$);
        \coordinate (height) at ($(0,.95)$);
        \coordinate (diag) at ($(base)+(height)$);
        \filldraw[rounded corners, fill=gray!70] ($(el)-.5*(diag)$) rectangle +(diag);
        \node[white] at (el) {\sffamily\Large \strut#1};
        \node[white, inner sep=2pt] (en) at ($(el)-.5*(height)$) [anchor=south] {\sffamily\tiny #5};
        \node[white, inner sep=2pt] (atnum) at ($(el)+.5*(height)-.5*(base)$) [anchor=north west] {\sffamily\tiny #4};
    }

    \begin{tikzpicture}
        \element{H}{1}{1}{1}{2.20}
        \grayelement{He}{18}{1}{2}{--}

        \element{Li}{1}{2}{3}{0.98}
        \element{Be}{2}{2}{4}{1.57}
        \element{B}{13}{2}{5}{2.04}
        \element{C}{14}{2}{6}{2.55}
        \element{N}{15}{2}{7}{3.04}
        \element{O}{16}{2}{8}{3.44}
        \element{F}{17}{2}{9}{3.98}
        \grayelement{Ne}{18}{2}{10}{--}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容