add tag
4 years ago 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.

How can I get the hex code of the calculated colour in LaTeX?

Top Answer
4 years ago 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

which will show the colour in the .log: you could just \tl_use:N \l_tmpa_tl if you want to print.

Answer #2
4 years ago 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 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:

Screenshot 2021-08-20 at 18.31.26.png

Answer #3
4 years ago 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:

Screenshot_2021-08-20_21-57-48.png

4 years
frougon replying to निरंजन — Sunday, 22nd Aug 2021 12:14

You’re welcome. 😃

an hour
samcarter replying to Joseph Wright — Sunday, 22nd Aug 2021 11:01

Ah, got it. The magic is in the log file. Sorry for the noise!

Joseph Wright — Sunday, 22nd Aug 2021 11:00

To be clear, we are working on integrating l3color into existing colour support such that there will be one set of data

Joseph Wright replying to samcarter — Sunday, 22nd Aug 2021 11:00

No, I just stopped where you’d see the data being formatted 😃

samcarter — Sunday, 22nd Aug 2021 10:52

@Joseph, re: your answer, This looks like an interesting approach! Could it be that some code got cut off at the end?

3 hours
निरंजन — Sunday, 22nd Aug 2021 07:25

@frougon Thanks for the explanation and the correction 😃

a day
frougon replying to निरंजन — Saturday, 21st Aug 2021 09:48

Here is one example of undesirable behavior when using \usepackage[T1]{fontenc} with LuaTeX: try using “a” in the input after \begin{document}. If you additionally use \tracinglostchars=1\tracingonline=1, you’ll see Missing character: There is no “ (U+201C) in font ec-lmr10! and Missing character: There is no ” (U+201D) in font ec-lmr10! (assuming the default font settings). OTOH, without the \usepackage[T1]{fontenc} line, the Unicode chars and are found without any problem in font lmroman10-regular.otf.

25 minutes
frougon replying to निरंजन — Saturday, 21st Aug 2021 09:22

I’ve modified the comment to make it clear that \usepackage[T1]{fontenc} should not be used with Unicode engines (e.g., LuaTeX or XeTeX).

frougon replying to निरंजन — Saturday, 21st Aug 2021 09:17

This comment assumes one compiles with the pdfTeX engine. In this case, omitting the \usepackage[T1]{fontenc} line causes the > from \meaning to be printed incorrectly as ¿, because that is what the default font has in slot "3E (which is the ASCII code of >). If you compile with the LuaTeX or XeTeX engine, the default font settings are different, more ASCII-compatible (actually, Unicode-compatible); moreover, I believe that in this case, it is not a good idea to load fontenc.

2 hours
निरंजन — Saturday, 21st Aug 2021 07:32

@frougon, re: your answer, I am just curious about one thing.

I tried commenting this line out, but I didn’t see any visual change. What did you mean by “nice printing”?

2 hours
joulev — Saturday, 21st Aug 2021 05:16

Thanks everyone!

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.