# tikz convert color string to hex value

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!

• I believe this question also deals with the issue of color conversion to hex: tex.stackexchange.com/questions/475007/… See also the binhex.tex package. Apr 5, 2019 at 22:42
• @StevenB.Segletes Your answer is definitely a great step towards a solution but does it also work if the colors are defined in the way the OP does? If so, your answer may also be mentioned under this answer.
– user121799
Apr 6, 2019 at 0:55
• I think combining my answer with the colorinfo package will allow what needs to be done. Apr 6, 2019 at 1:07

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}


Not done in tikz, but rather using the colorinfo package to extract color information, and the binhex.tex package to convert the color info into hex format.

Extending my answer here Draw a palette box in LaTeX to allow the specification in terms of LaTeX color specification.

\documentclass{article}
\usepackage{xcolor,stackengine,colorinfo}
\input binhex.tex
\newcommand\palbox[1]{{\sffamily\fboxsep=5pt\relax\fboxrule=1pt\relax\footnotesize%
\fcolorbox{gray!50}{gray!10}{%
\stackengine{8pt}{%
\colorbox[RGB]{#1}{\rule{60pt}{0pt}\rule{0pt}{60pt}}%
}{%
\color{black!60}\stackengine{12pt}{\intohex{#1}}{\saycolors{#1}}{U}{l}{F}{F}{S}%
}{U}{l}{F}{F}{S}%
}%
}}
\newcommand\saycolors[1]{\saycolorsaux#1\relax}
\def\saycolorsaux#1 #2 #3\relax{R:#1 G:#2 B:#3}
\newcommand\intohex[1]{\#\intohexaux#1\relax}
\def\intohexaux#1 #2 #3\relax{\twodigithex{#1}\twodigithex{#2}\twodigithex{#3}}
\newcommand\twodigithex[1]{\ifnum#1<16\relax0\fi\MakeLowercase{\hex{#1}}}

\newlength\rcomp
\newlength\gcomp
\newlength\bcomp
\newcommand\colcomponents[1]{\expandafter\colcomponentsaux#1\relax}
\def\colcomponentsaux#1,#2,#3\relax{%
\setlength\rcomp{\dimexpr255\dimexpr#1pt\relax+.5pt\relax}%
\setlength\gcomp{\dimexpr255\dimexpr#2pt\relax+.5pt\relax}%
\setlength\bcomp{\dimexpr255\dimexpr#3pt\relax+.5pt\relax}%
}
\newcommand\truncatergb[1]{\expandafter\truncatergbaux#1\relax}
\def\truncatergbaux#1.#2\relax{#1}
\newcommand\xpalbox[1]{%
\colorlet{mycolor}{#1}%
\setbox0=\hbox{\colorInfo{mycolor}}%
\colcomponents{\colorValue}%
\edef\Rcomp{\truncatergb{\the\rcomp}}%
\edef\Gcomp{\truncatergb{\the\gcomp}}%
\edef\Bcomp{\truncatergb{\the\bcomp}}%
\edef\tmp{\expandafter\expandafter\expandafter\Rcomp
\expandafter\expandafter\expandafter\space
\expandafter\Gcomp\expandafter\space\Bcomp}%
\expandafter\palbox\expandafter{\tmp} = \colorbox{mycolor}{#1}%
\par
}
\begin{document}
\xpalbox{blue!40!red!25}
\xpalbox{blue!60!green!45}
\end{document}


One more example base on @quark67's answer:

\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,chains}
\begin{document}
\begin{tikzpicture}[start chain=M1 going below,node distance=0]
\tikzset{box/.style={draw,outer sep=0,minimum width=2cm,on chain=M1}}
\def\dy{1}
\foreach \col [count=\i] in {red,orange,yellow,green,cyan,blue,purple,
{rgb,255:red,0; green,255; blue,0},
{rgb:red,1;green,1;yellow,1}} {
\extractcolorspecs{\col}{\modelcmd}{\colorcmd}
\convertcolorspec{\modelcmd}{\colorcmd}{HTML}\hex
\node[box,fill=\col,label=right:\col] (main) {\#\hex};
}
\end{tikzpicture}
\end{document}