Figuring out RGB or HEX color from xcolor

The \extractcolorspec macro will define a macro that contains the specification for a color; we can use it and the companion \convertcolorspec to define a macro containing the specifications for another model:

\definecolor{canvascolor}{rgb}{0.643,0.690,0.843}
\extractcolorspec{canvascolor!75!white}{\test}
\expandafter\convertcolorspec\test{HTML}\test

Now \test will expand to BBC4E1.

The first definition of \test will be {rgb}{0.73225,0.7675,0.88225} so this is good for putting it after \convertcolorspec, but we have to expand it first.

Use two different macros if you also need to keep the first specification around.

We can implement this in a function

\newcommand{\getColorSpec}[3][\getColorSpecTemp]{%
  \extractcolorspec{#3}\getColorSpecTemp
  \expandafter\convertcolorspec\getColorSpecTemp{#2}#1}

With \getColorSpec{HTML}{canvascolor!75!white} the specification will be stored in \getColorSpecTemp; with

\getColorSpec[\HTMLcolor]{HTML}{canvascolor!75!white}

the same specification will be stored in \HTMLcolor.


\documentclass{article}
\usepackage{xcolor}
\definecolor{canvascolor}{rgb}{0.643,0.690,0.843}
\colorlet{canvas75}{canvascolor!75!white}
\begin{document}
\noindent\ttfamily
\color{canvas75}RGB colour canvascolor!75!white\\
html : \convertcolorspec{named}{canvas75}{HTML}\HTMLcolour
\HTMLcolour\\
cmyk : \convertcolorspec{named}{canvas75}{cmyk}\CMYKcolour
\CMYKcolour

\end{document}

enter image description here

Tags:

Beamer

Color