[Tex/LaTex] tikz convert color string to hex value

tikz-pgf

I wish to convert a color string to hex value in tikz.

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{box/.style={draw,outer sep=0,minimum size=1cm}}
\def\dy{1}
\foreach \col [count=\i] in {red,orange,yellow,green,cyan,blue,purple} {
    \def\mycol{\col!40!gray}
    \node[box,yshift=1cm*\i*\dy,fill=\mycol,label=left:\mycol] (main) {ABC};
}
\end{tikzpicture}
\end{document}

So I wish show red!40!grey to something like #xxxxxxxx but not show how can do such convert!

Best Answer

Perhaps this (with \extractcolorspecs and \convertcolorspec from the xcolor package, loaded by the tikz package)?

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{box/.style={draw,outer sep=0,minimum size=1cm}}
\def\dy{1}
\foreach \col [count=\i] in {red,orange,yellow,green,cyan,blue,purple} {
    \def\mycol{\col!40!gray}
    \extractcolorspecs{\mycol}{\modelcmd}{\colorcmd}
    \convertcolorspec{\modelcmd}{\colorcmd}{HTML}\hex
    \node[box,yshift=1cm*\i*\dy,fill=\mycol,label=left:\mycol] (main) {\#\hex};
}
\end{tikzpicture}
\end{document}

enter image description here