如何将先前定义的颜色的组成颜色打印为百分比?在这种情况下,我只关心 CMYK 颜色。
平均能量损失
\documentclass{article}
\usepackage{color}
\definecolor{SomeColour}{cmyk}{0.8,0.6,0.4,0.2}
\newcommand{\printcomponents}[1]{
\begin{tabular}{ll}
Composite & \textcolor{#1}{#1} \\
Cyan & 80 \\ % <- Return cyan component as percentage
Magenta & 60 \\ % <- Return magenta component as percentage
Yellow & 40 \\ % <- Return yellow component as percentage
Black & 20 \\ % <- Return black component as percentage
\end{tabular}
}
\begin{document}
\printcomponents{SomeColour}
\end{document}
答案1
该xcolor
包提供\extractcolorspecs
为了检索颜色模型和颜色规范:
\extractcolorspecs{#1}{\colmodel}{\colspec}
将模型存储到\colmodel
并将颜色存储到\colspec
,这是一个以逗号分隔的列表。
为了处理列表,我使用了expl3
特征。
\documentclass{article}
\usepackage{xcolor}
\definecolor{SomeColour}{cmyk}{0.8,0.6,0.4,0.2}
\definecolor{OtherColour}{cmyk}{0.4,0.66,0.45,0.1}
\usepackage{xparse}
\ExplSyntaxOn
\cs_new:Npn \extractcontent#1#2 {%
\clist_set:Nx \l_tmpa_clist {#1}% Store the list
\fp_eval:n { 100 * \clist_item:Nn \l_tmpa_clist {#2}} % use the list item #2 and multiply it with 100
}
\ExplSyntaxOff
\newcommand{\printcomponents}[1]{%
\extractcolorspecs{#1}{\colmodel}{\colspec}
\begin{tabular}{ll}
Composite & \textcolor{#1}{#1} \\
Cyan & \extractcontent{\colspec}{1} \\ % <- Return cyan component as percentage
Magenta & \extractcontent{\colspec}{2} \\ % <- Return magenta component as percentage
Yellow & \extractcontent{\colspec}{3} \\ % <- Return yellow component as percentage
Black & \extractcontent{\colspec}{4} \\ % <- Return black component as percentage
\end{tabular}
}
\begin{document}
\printcomponents{SomeColour}
\printcomponents{OtherColour}
\end{document}