一致的配色方案

一致的配色方案

我想为我的文档(最好是其他文档)的所有基于 TikZ/PGF 的图形和文本高亮采用一致的配色方案。

更具体地说,我想首先选择一个连续的颜色范围(即pgf图 colormap),并通过该颜色范围中的样本点挑选几种单独的颜色。然后我想将这些单独的颜色排列成连续的列表(例如pgf图 cycle listTikZ/PGF style sheet并且可能彩色 colorseries),但也可以通过\mycolor3或等命令单独访问它们\mycolors{3}。总的来说,我正在寻找一种方法,其中唯一的输入是颜色范围和样本点(最好在范围 [0,1] 内,而不是pgf图 cycle list范围是 [0,1000]),其余部分则自动“生成”。

请注意,我正在寻找一种足够灵活的方法,以便以后根据需要添加更多颜色表示。我想假设的唯一共同点是使用彩色包,因为它在许多其他包中得到广泛的支持。

我尝试了一下电子工具箱包,但迄今为止无济于事。酶切这个包似乎也很有用,但到目前为止我自己还无法很好地利用它。

我的问题现在可能不太具体,但也许一些讨论可以帮助我真正指出什么确切地我在寻找。

编辑:

这是一个示例,展示我想要做的一些事情:

\documentclass[a4paper]{article}

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\usetikzlibrary{datavisualization}

% (A) PGFPLOTS color map
\newcommand{\MyColorMap}{viridis}

% (B) values in [0,1] to sample from the color map
\newcommand{\defineMyColorSample}[2]{%
    \expandafter\newcommand\csname MyColorSample:#1\endcsname{#2}%
}
\newcommand{\MyColorSample}[1]{\csname MyColorSample:#1\endcsname}
\defineMyColorSample{1}{0.0}
\defineMyColorSample{2}{0.5}
\defineMyColorSample{3}{0.25}
\defineMyColorSample{4}{0.75}
\newcommand{\MyColorSamples}{
    \MyColorSample{1},
    \MyColorSample{2},
    \MyColorSample{3},
    \MyColorSample{4}
}

% (C) color map samples based on the samples above but mapped to range [0,1000]
\newcommand{\defineMyColorMapSample}[2]{%
    \expandafter\newcommand\csname MyColorMapSample:#1\endcsname{#2}%
}
\newcommand{\MyColorMapSample}[1]{\csname MyColorMapSample:#1\endcsname}
\newcommand{\MyColorCalc}[1]{\directlua{ tex.write(#1) }}
\defineMyColorMapSample{1}{\MyColorCalc{1000 * \MyColorSample{1}}}
\defineMyColorMapSample{2}{\MyColorCalc{1000 * \MyColorSample{2}}}
\defineMyColorMapSample{3}{\MyColorCalc{1000 * \MyColorSample{3}}}
\defineMyColorMapSample{4}{\MyColorCalc{1000 * \MyColorSample{4}}}
\newcommand{\MyColorMapSamples}{
    \MyColorMapSample{1},
    \MyColorMapSample{2},
    \MyColorMapSample{3},
    \MyColorMapSample{4}
}

% (D) individual colors based on sample points of colomap
% ???
% add some dummies for now
\newcommand{\defineMyColor}[2]{%
    \expandafter\newcommand\csname MyColor:#1\endcsname{#2}%
}
\newcommand{\MyColor}[1]{\csname MyColor:#1\endcsname}
\defineMyColor{1}{red}
\defineMyColor{2}{green}
\defineMyColor{3}{blue}
\defineMyColor{4}{yellow}
\newcommand{\MyColors}{
    \MyColor{1},
    \MyColor{2},
    \MyColor{3},
    \MyColor{4}
}

% (E) PGFPLOTS cycle list
\pgfplotscreateplotcyclelist{MyColorCycleList}{
    [colors of colormap={\MyColorMapSamples} of \MyColorMap]
}

% (F) TikZ/PGF style sheet
\pgfkeys{
    /pgf/data visualization/style sheets/MyStyle/.cd,
    % All these styles have the above prefix.
    1/.style={\MyColor{1}},
    2/.style={\MyColor{2}},
    3/.style={\MyColor{3}},
    4/.style={\MyColor{4}},
    default style/.style={black}
}

\begin{document}

\section{colored text}
\textcolor{\MyColor{3}}{This text} uses \colorbox{\MyColor{4}}{my colors}.
\begin{enumerate}
    \item \textcolor{\MyColor{1}}{first color}
    \item \textcolor{\MyColor{2}}{second color}
    \item \textcolor{\MyColor{3}}{third color}
    \item \textcolor{\MyColor{4}}{fourth color}
\end{enumerate}

\section{PGFPLOTS cycle list}
% adapted from PGFPLOTS manual (v1.18.1) section 4.7.7
\begin{tikzpicture}
\begin{axis}[
    stack plots=y,stack dir=minus,
    cycle list name=MyColorCycleList,
    fatline/.style={line width=5pt},
]
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
\end{axis}
\end{tikzpicture}

\section{TikZ data visaulization}
% adapted from TikZ/PGF manual (v3.1.9a) section 84.4.2
\tikz \datavisualization [
    school book axes, all axes={length=0.5\textwidth},
    visualize as line=1,
    visualize as line=2,
    visualize as line=3,
    style sheet=MyStyle]
data point [x=0, y=0, set=1]
data point [x=2, y=2, set=1]
data point [x=0, y=1, set=2]
data point [x=2, y=1, set=2]
data point [x=0.5, y=1.5, set=3]
data point [x=2.25, y=1.75, set=3];

\end{document}

输出

我想输入的唯一“输入”是colormap(参见(A)序言)和样本值列表(参见(B)序言)。然而,在示例中,后者的定义有点麻烦,因为我更喜欢类似

\newcommand{\MyColorSamples}{0.0, 0.5, 0.25, 0.75}

此外,将样本从 [0,1] 映射到 [0,1000] 目前涉及 Lua 代码(参见(C)前言),我想省略它。

接下来,我不知道如何从中提取采样颜色pgf图 colormap并使其作为基本彩色颜色(参见(D)序言)。此外,我希望我的色彩是某种形式colorseries,这样我就可以使用彩色(例如!!+),尽管这对我来说是可选的。

在定义pgf图 cyclelist非常简单(参见(E)序言),TikZ/PGF style sheet(参见(F)序言)取决于的颜色(D)

总体而言,到目前为止,我的代码非常冗长,每当样本列表的长度(参见(B))发生变化时,整个代码都需要调整。出于这个原因,我想在循环中实现所有内容,但我还不知道如何正确地做到这一点。

答案1

经过一些痛苦的反复尝试,我自己找到了一个解决方案:

\documentclass[a4paper]{article}

\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}
\usetikzlibrary{datavisualization}
\usepackage{etoolbox}
\usepackage{calculus}

% (A) "input" definitions
\newcommand{\MyColorMapName}{colormap/viridis}
\newcommand{\MyColorSamples}{0.5, 0.0, 1.0, 0.25, 0.75}

% (B) retrieve number of sample points and make individual values availabe via \MyColorSample{<i>} command
\newcommand*{\MyColorSample}[1]{\csname MyColorSample:#1\endcsname}
\newcounter{MyColorSamplesSize}
\renewcommand*{\do}[1]{%
    \stepcounter{MyColorSamplesSize}%
    \listxadd{\MyColorSampleIDs}{\theMyColorSamplesSize}%
    \expandafter\def\csname MyColorSample:\theMyColorSamplesSize\endcsname{#1}%
}
\expandafter\docsvlist\expandafter{\MyColorSamples}

% (C) caclulate color map samples based on \MyColorSamples but mapped to range [0,1000]
\def\MyColorMapSamples{}
\newcommand*{\MyColorMapSample}[1]{\csname MyColorMapSample:#1\endcsname}
\renewcommand*{\do}[1]{
    \MULTIPLY{\MyColorSample{#1}}{1000.0}{\val}%
    \FLOOR{\val}{\valf}%
    \ifstrequal{\val}{\valf}{\edef\val{\val .0}}%
    \expandafter\edef\csname MyColorMapSample:#1\endcsname{\val}%
    \edef\MyColorMapSamples{\MyColorMapSamples\MyColorMapSample{#1}\ifnumcomp{#1}{=}{\theMyColorSamplesSize}{}{, }}%
}
\dolistloop{\MyColorSampleIDs}

% (D) define individual colors based on sample points of colormap
\colorlet{DefaultColor}{.} % save current default color
\newcommand*{\MyColor}[1]{MyColor#1}
\renewcommand*{\do}[1]{%
    \pgfplotsset{color of colormap=\MyColorMapSample{#1} of \MyColorMapName}%
    \colorlet{MyColor#1}{.}
}
\dolistloop{\MyColorSampleIDs}
\colorlet{.}{DefaultColor} % restore default color


% (E) define PGFPLOTS cycle list
\pgfplotscreateplotcyclelist{MyColorCycleList}{
    [colors of colormap={\MyColorMapSamples} of \MyColorMapName]
}

% (F) define TikZ/PGF style sheet
\renewcommand*{\do}[1]{%
    #1/.style={\MyColor{#1}, line width=2pt},
}
\edef\MyStyle{\noexpand\pgfkeys{
    /pgf/data visualization/style sheets/MyStyle/.cd,
    \dolistloop{\MyColorSampleIDs}
    default style/.style={black,line width=5pt}
}}
\MyStyle

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\section{color map}
selected color map: \texttt{\MyColorMapName}\\
\pgfplotscolorbardrawstandalone[
    \MyColorMapName,
    colorbar horizontal
]

\section{color boxes and colored text}
\theMyColorSamplesSize\ color samples have been defined:
\begin{enumerate}
    \renewcommand*{\do}[1]{
        \item \colorbox{\MyColor{#1}}{\textcolor{-\MyColor{#1}}{colored box with complementary text color}}
    }
    \dolistloop{\MyColorSampleIDs}
\end{enumerate}

\section{PGFPLOTS cycle list}
% adapted from PGFPLOTS manual (v1.18.1) section 4.7.7
\begin{tikzpicture}
\begin{axis}[
    stack plots=y,stack dir=minus,
    cycle list name=MyColorCycleList,
    fatline/.style={line width=2pt},
]
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
    \addplot+[fatline] coordinates {(0,1) (0.5,1) (1,1)};
\end{axis}
\end{tikzpicture}

\section{TikZ data visualization}
% adapted from TikZ/PGF manual (v3.1.9a) section 84.4.2
\tikz \datavisualization [
    school book axes,
    visualize as line=1,
    visualize as line=2,
    visualize as line=3,
    style sheet=MyStyle]
data point [x=0, y=0, set=1]
data point [x=2, y=2, set=1]
data point [x=0, y=1, set=2]
data point [x=2, y=1, set=2]
data point [x=0.5, y=1.5, set=3]
data point [x=2.25, y=1.75, set=3];

\end{document}

输出

它能做什么

(A)序言部分,指定了两个“输入参数” - colormap\MyColorMapName)的名称和范围 [0,1] 内的样本值的逗号分隔列表( ) 。\MyColorSamples

(B)序言部分,\MyColorSamples执行了第一个循环。之后,列表中的值数量可通过计数器MyColorSampleSize(内部列表,参见电子工具箱) 已创建(\MyColorSampleIDs),其中包含样本 ID(即 <1、2、3、...>),并且可以通过命令访问各个值\MyColorSample{<id>}

(C)序言部分,样本值通过第二个循环映射到范围 [0,1000]。结果值分别通过命令\MyColorMapSamples和提供\MyColorMapSample{<id>}。请注意,我能够通过使用结石包(部分计算器)。

(D)序言部分,执行另一个循环来提取实际的彩色来自颜色图的颜色\MyColorMapName。可以通过命令访问各个颜色\MyColor{<id>}。请注意,需要恢复“默认颜色”,因为绘图将引用此颜色来绘制轴等。

(E)序言部分,pgf图循环列表MyColorCycleList的定义与前面相同。

(F)序言部分,TikZ/PGF样式表MyStyle是通过另一个循环定义的。请注意,循环的执行需要嵌套在\edef正确的宏扩展中。

相关内容