Use the facilities of xparse
and expl3
:
\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\defineCMYKcolor}{mm}
{
\tceighteight_define_CMYK_color:nn { #1 } { #2 }
}
\seq_new:N \l_tceighteight_input_seq
\seq_new:N \l_tceighteight_output_seq
\cs_new_protected:Npn \tceighteight_define_CMYK_color:nn #1 #2
{
% split the input into parts
\seq_set_split:Nnn \l_tceighteight_input_seq { , } { #2 }
% convert each item to a decimal value in [0,1]
\seq_set_map:NNn \l_tceighteight_output_seq \l_tceighteight_input_seq
{
\fp_eval:n { ##1/255 }
}
% now define the color
\definecolor{#1}{cmyk}{ \seq_use:Nn \l_tceighteight_output_seq { , } }
}
\ExplSyntaxOff
\defineCMYKcolor{test}{32,100,211,99}
\begin{document}
\textcolor{test}{ABCDEF}
\end{document}
The four numbers are stored in a sequence (one might add here some sanity checks) and then transformed into another sequence that is used in the appropriate \definecolor
command.
If I ask what the color test
expands to, I get
> \\color@test=macro:
->\xcolor@ {}{0.12549 0.39215 0.82745 0.38823 k 0.12549 0.39215 0.82745 0.38823
K}{cmyk}{0.12549,0.39215,0.82745,0.38823}.
and, for checking the values, I multiplied them back by 255, getting
0.12549*255 = 31.99995
0.39215*255 = 99.99825
0.82745*255 = 210.99975
0.38823*255 = 98.99865
which seems good (some small error is expected anyway).