Since you seem to want expandable commands, you have to set the relevant bits at color definition time, because the extraction/conversion functions provided by xcolor
are not expandable.
Here's a way, with two “extended” commands for color definition, namely \Xdefinecolor
and \Xcolorlet
.
\documentclass{article}
\usepackage{xcolor,xparse}
\ExplSyntaxOn
\NewDocumentCommand{\Xdefinecolor}{mmm}
{
\definecolor{#1}{#2}{#3}
\str_if_eq:nnTF { #2 } { RGB }
{ % no conversion necessary
\tl_set:cx { l_commusoft_#1_red_tl } { \clist_item:nn { #3 } { 1 } }
\tl_set:cx { l_commusoft_#1_green_tl } { \clist_item:nn { #3 } { 2 } }
\tl_set:cx { l_commusoft_#1_blue_tl } { \clist_item:nn { #3 } { 3 } }
}
{
\convertcolorspec { #2 } { #3 } { RGB } \l_tmpa_clist
\tl_set:cx { l_commusoft_#1_red_tl } { \clist_item:Nn \l_tmpa_clist { 1 } }
\tl_set:cx { l_commusoft_#1_green_tl } { \clist_item:Nn \l_tmpa_clist { 2 } }
\tl_set:cx { l_commusoft_#1_blue_tl } { \clist_item:Nn \l_tmpa_clist { 3 } }
}
}
\NewDocumentCommand{\Xcolorlet}{mm}
{
\colorlet{#1}{#2}
\commusoft_extractcolorspec:nN {#1} \l_tmpa_tl
\exp_last_unbraced:Nf \commusoft_convertcolorspec:nnnN \l_tmpa_tl { RGB } \l_tmpa_clist
\tl_set:cx { l_commusoft_#1_red_tl } { \clist_item:Nn \l_tmpa_clist { 1 } }
\tl_set:cx { l_commusoft_#1_green_tl } { \clist_item:Nn \l_tmpa_clist { 2 } }
\tl_set:cx { l_commusoft_#1_blue_tl } { \clist_item:Nn \l_tmpa_clist { 3 } }
}
\DeclareExpandableDocumentCommand{\getred}{m}
{
\tl_use:c { l_commusoft_#1_red_tl }
}
\DeclareExpandableDocumentCommand{\getgreen}{m}
{
\tl_use:c { l_commusoft_#1_green_tl }
}
\DeclareExpandableDocumentCommand{\getblue}{m}
{
\tl_use:c { l_commusoft_#1_blue_tl }
}
\cs_set_eq:NN \commusoft_convertcolorspec:nnnN \convertcolorspec
\cs_set_eq:NN \commusoft_extractcolorspec:nN \extractcolorspec
\ExplSyntaxOff
\Xdefinecolor{somecolor}{RGB}{0,42,14}
\Xdefinecolor{another}{cmyk}{0.2,0.3,0.4,0.5}
\Xcolorlet{tint}{another!20!red}
\begin{document}
\getred{somecolor}, \getgreen{somecolor}, \getblue{somecolor}
\getred{another}, \getgreen{another}, \getblue{another}
\edef\temp{\getred{another}, \getgreen{another}, \getblue{another}}
\texttt{\meaning\temp}
\getred{tint}, \getgreen{tint}, \getblue{tint}
\colorbox{tint}{\qquad}
\colorbox[RGB]{\getred{tint}, \getgreen{tint}, \getblue{tint}}{\qquad}
\colorbox{somecolor}{\qquad}
\colorbox[RGB]{\getred{somecolor}, \getgreen{somecolor}, \getblue{somecolor}}{\qquad}
\end{document}
Notice that in the case of tint
, the colors are not the same, due to the flaky conversion from cmyk
to RGB
(that has less colors available). The \edef
shows that the commands are fully expandable.

\extractcolorspecs
from thexcolor
package? – user31729 Nov 24 '15 at 16:58