Convert color name to pdfliteral code

How can I convert a color name (such as used with the xcolor package) to a pdfliteral code, like q 1 0 0 rg (for red)?

• What is the context here? The drivers for the color package normally deal with this for LaTeX users, as this keeps code driver-independent. Also, with a sufficiently up-to-date pdfTeX or LuaTeX, why use \pdfliteral rather than \pdfcolorstack? Sep 19 '11 at 9:28
• @JosephWright The context is in Lua code, and I can't use \color there because I'm inserting PDF annotations from the Lua code. Sep 19 '11 at 9:30
• The way that named colours are done is by defining what they convert to: a colour model and then some values. I'd imagine a Lua solution would be done the same way. Can't you just access the appropriate macro definitions from the Lua end? Sep 19 '11 at 9:36
• @JosephWright Sure, then I guess my question would be if there is such a macro which converts a color name into a space separated triplet of RGB components? Sep 19 '11 at 9:38

\pdfcompresslevel=0 % to make everything visible in the pdf
\documentclass{article}
\usepackage{xcolor}
\def\usecolor#1{\csname\string\color@#1\endcsname\space}

\begin{document}
foo
\pdfliteral{\usecolor{red}}
bar
\end{document}


in pdf it is: 1 0 0 rg 1 0 0 RG

• So \usecolor{red} outputs 1 0 0 rg 1 0 0 RG? Sep 19 '11 at 9:48
• yes, can already been seen in the pdf
– user2478
Sep 19 '11 at 9:52
• How to understand the arguments of \pdfliteral? I use {241 240 240 rg} for green but it shows black Dec 8 '17 at 16:25
• use values between 0 and 1
– user2478
Dec 8 '17 at 17:10