I have a colour mycol
which is defined to, say, #123456. In my document, I sometimes use a calculation based on mycol
, for example mycol!40
.
xxxxxxxxxx
\documentclass{article}
\usepackage{xcolor}
\definecolor{mycol}{HTML}{123456}
\begin{document}
\textcolor{mycol}{Hello world}
\textcolor{mycol!70}{Hello world, but with more white.}
\end{document}
How can I get the hex code of the calculated colour in LaTeX?
Using l3color
one can get this data directly. At present, the colours are independent of those from xcolor
, so will have to be defined
xxxxxxxxxx
\documentclass{article}
\ExplSyntaxOn
\color_set:nnn { mycol } { HTML } {123456}
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
\color_export:nnN { mycol!40 } { HTML } \l_tmpa_tl
\tl_show:N \l_tmpa_tl
which will show the colour in the .log
: you could just \tl_use:N \l_tmpa_tl
if you want to print.
You could calculate it manually. An html colour follows the structure #RRGGBB (in base 16) and colour mixing is normally a linear interpolation. So in your case, your colour has a red value of 1216 (= 1810), a green value of 3416 (= 5210) and a blue value of 5616 (= 8610). Thus the colour components after mixing are:
- r: 0.4 × 18 + 0.6 × 255 = 160.2 ≈ A016
- g: 0.4 × 52 + 0.6 × 255 = 173.8 ≈ AE16
- b: 0.4 × 86 + 0.6 × 255 = 187.4 ≈ BB16
Or you could ask xcolor
to show you the key: