joulev
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`.
```
\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?
Top Answer
Joseph Wright
Using `l3color` one can get this data directly. At present, the colours are independent of those from `xcolor`, so will have to be defined
```latex
\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.
Answer #2
samcarter
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 12~16~ (= 18~10~), a green value of 34~16~ (= 52~10~) and a blue value of 56~16~ (= 86~10~). Thus the colour components after mixing are:
- r: 0.4 × 18 + 0.6 × 255 = 160.2 ≈ A0~16~
- g: 0.4 × 52 + 0.6 × 255 = 173.8 ≈ AE~16~
- b: 0.4 × 86 + 0.6 × 255 = 187.4 ≈ BB~16~
---
Or you could ask `xcolor` to show you the key:
```
\documentclass{article}
\usepackage[landscape]{geometry}
\usepackage{xcolor}
\definecolor{mycolor}{HTML}{123456}
\begin{document}
\textcolor{mycolor}{\rule{3cm}{3cm}}
\textcolor{mycolor!40}{\rule{3cm}{3cm}}
\begin{testcolors}[HTML,rgb,RGB,cmyk,hsb]
\testcolor{mycolor}
\testcolor{mycolor!40}
\end{testcolors}
\end{document}
```
![Screenshot 2021-08-20 at 18.31.26.png](/image?hash=69a663df8485ee5a7bdee797621728fed7f1affeb557763702874337a8b9ccf7)
Answer #3
frougon
In order to obtain the hexadecimal notation in a way that is accessible to your code (for further processing, possibly involving computations), you can use `xcolor`'s `\extractcolorspecs` macro:
```
\documentclass{article}
\usepackage[T1]{fontenc} % for nice printing of \meaning (only for non-Unicode engines)
\usepackage{xcolor}
\definecolor{mycol}{HTML}{123456}
\ExplSyntaxOn
\cs_new:Npn \__joulev_rgb_spec_to_hex_code_aux:n #1
{
\int_to_Hex:n { \fp_eval:n { round(255*(#1)) } }
}
\cs_new:Npn \joulev_rgb_spec_to_hex_code:n #1
{
\clist_map_function:nN {#1} \__joulev_rgb_spec_to_hex_code_aux:n
}
\cs_generate_variant:Nn \joulev_rgb_spec_to_hex_code:n { V }
\msg_new:nnn { joulev } { not-rgb-color-model }
{ Color~model~for~color~'#1'~isn't~'rgb'. }
\str_new:N \l__joulev_color_model_str
% #1: macro where to store the raw color spec (raw = not converted to hex)
% #2: an rgb-based ⟨color⟩
\cs_new_protected:Npn \__joulev_set_to_raw_color_spec:Nn #1#2
{
\extractcolorspecs {#2} \l__joulev_color_model_str #1
\str_if_eq:VnF \l__joulev_color_model_str { rgb }
{ \msg_error:nnn { joulev } { not-rgb-color-model } {#2} }
}
% #1: macro where to store the color spec in hexadecimal notation
% #2: an rgb-based ⟨color⟩
\NewDocumentCommand \setToHexColorSpec { m m }
{
\__joulev_set_to_raw_color_spec:Nn \l_tmpa_tl {#2}
\tl_set:Nx #1 { \joulev_rgb_spec_to_hex_code:V \l_tmpa_tl }
}
\NewDocumentCommand \printHTMLcolorSpec { m }
{
\mode_leave_vertical:
\__joulev_set_to_raw_color_spec:Nn \l_tmpa_tl {#1}
\hbox:n { \# \joulev_rgb_spec_to_hex_code:V \l_tmpa_tl }
}
\ExplSyntaxOff
\begin{document}
\extractcolorspecs{mycol!40}{\myModel}{\myColorSpec}
\verb|mycol!40| has model \texttt{\myModel} and spec \texttt{\myColorSpec}.
HTML notation: \printHTMLcolorSpec{mycol!40}
\setToHexColorSpec{\mymacro}{mycol!40}
\meaning\mymacro
\end{document}
```
![Screenshot_2021-08-20_21-57-48.png](/image?hash=6dbea88eb3fff0fc4441657d13d64f0df9056058d21df3d522343922a60ec174)