21

I need to build a table of this type:

STUFF | COLOR | PICTURE | COLOR DEFINITION

To retrieve the color definition, I tried to use \extractcolorspecs expecting that the third argument { color-cmd } would allowed me to print what I needed. The problem is that I defined colors in HTML, while the command gives me RGB format in output. Here is a MWE:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

\usepackage{tikz}
\usepackage{booktabs}
\usepackage{xcolor}

\definecolor{mapblue}{HTML}{0000FF}%
\definecolor{maproyalblue}{HTML}{0088FF}%
\definecolor{mapskyblue}{HTML}{00CCFF}%
\definecolor{mapgreen}{HTML}{00CC00}%
\definecolor{mapdandelion}{HTML}{FFCC00}%
\definecolor{maporange}{HTML}{FF8800}%
\definecolor{mapred}{HTML}{FF0000}%

\newcommand{\colpic}[1]{\tikz \draw[#1,fill=#1,draw](0,0)circle(0.1cm);}
\newcommand{\colrow}[1]{#1 & \colpic{#1} & \extractcolorspecs{#1}{\model}{\mycolor}\mycolor}

\begin{document}

\begin{tabular}{cccc}
\toprule
\multicolumn{1}{c|}{\textsc{Relevance Interval}} &
\multicolumn{1}{c|}{\textsc{Color}} & 
\multicolumn{1}{c|}{\textsc{Picture}} & 
\multicolumn{1}{c}{\textsc{ Color Definition}}\\
\midrule 
0 - 5 & \colrow{mapblue}\\
5 - 10 & \colrow{maproyalblue}\\
10 - 25 & \colrow{mapskyblue}\\
25 - 50 & \colrow{mapgreen}\\
50 - 100 & \colrow{mapdandelion}\\
100 - 200 & \colrow{maporange}\\
>200 & \colrow{mapred}\\
\bottomrule
\end{tabular}

\end{document}

and this is the output: enter image description here

How is it possible to obtain the color definition in HTML?

  • 3
    There is also a command \convertcolorspec in the xcolor manual for conversion. – percusse Apr 9 '12 at 11:24
  • Thanks! Now it works. Should I write as answer the solution? – Claudio Fiandrino Apr 9 '12 at 11:31
  • 2
    @percusse Please, expand your comment into an answer – egreg Apr 9 '12 at 12:46
22

We can convert the stored version of the color definition (which is expressed in rgb) to HTML with

\newcommand{\colrow}[1]{%
  #1 & \colpic{#1} & \extractcolorspecs{#1}{\model}{\mycolor}%
    \convertcolorspec{\model}{\mycolor}{HTML}\tmp\tmp
}

Also we can squeeze in the definitions of the commands like \somecolorhtml with some extra coding.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{tikz,booktabs,xcolor}
\newcommand{\definemycolor}[2]{%
        \definecolor{#1}{HTML}{#2}%
        \expandafter\def\csname#1html\endcsname{#2\relax} %Which defines \colornamehtml
        }
\newcommand{\colpic}[1]{\tikz \draw[#1,fill=#1,draw](0,0)circle(0.1cm);}
\newcommand{\colrow}[1]{#1 & \colpic{#1} &\csname#1html\endcsname}
\definemycolor{mapblue}{0000FF}%
\definemycolor{maproyalblue}{0088FF}%
\definemycolor{mapskyblue}{00CCFF}%
\definemycolor{mapgreen}{00CC00}%
\definemycolor{mapdandelion}{FFCC00}%
\definemycolor{maporange}{FF8800}%
\definemycolor{mapred}{FF0000}%
\begin{document}
\begin{tabular}{cccc}
\toprule
\multicolumn{1}{c|}{\textsc{Relevance Interval}} &
\multicolumn{1}{c|}{\textsc{Color}} & 
\multicolumn{1}{c|}{\textsc{Picture}} & 
\multicolumn{1}{c}{\textsc{ Color Definition}}\\    \midrule 
0 - 5 & \colrow{mapblue}\\
5 - 10 & \colrow{maproyalblue}\\
10 - 25 & \colrow{mapskyblue}\\
25 - 50 & \colrow{mapgreen}\\
50 - 100 & \colrow{mapdandelion}\\
100 - 200 & \colrow{maporange}\\
>200 & \colrow{mapred}\\
\bottomrule
\end{tabular}
Since we defined the \verb|\colornamehtml| command versions of the colors,
\verb|\mapbluehtml| gives \textcolor{mapblue}{\mapbluehtml}  and 
\verb|\mapdandelionhtml| gives \textcolor{mapdandelion}{\mapdandelionhtml}.
\end{document}

enter image description here

Also, I think a monospaced font would please more in the last column so replacing the colrow command with the following

\newcommand{\colrow}[1]{#1 & \colpic{#1} &\expandafter\texttt{\csname#1html\endcsname}}

would lead to

enter image description here

0

The question started circa 2012, and the original answers demonstrate the community's experts' prescience.

In 2016, Dr. Uwe Kern addressed the xcolor package (v2.12) (11 May 2016).

On p.23 of xcolor.pdf, https://ctan.org/pkg/xcolor?lang=en , he talks about the package's "Color testing" features. ¶ 2.6.4.

On p.31, there's a code snippet, here put into an MWE to give it working context. With the macro input of the various ways colors are recognized, the output is tabular with rgb, cmyk, hsb, HTML, and gray. He recommends putting this macro in its own environment.

\documentclass[12pt]{article}

\usepackage[margin=0.5in]{geometry}

\usepackage[dvipsnames*, x11names, svgnames, hyperref]{xcolor}

\AtBeginDocument{\colorlet{defaultcolor}{.}}

\begin{document}

\sffamily

\begin{testcolors}[rgb,cmyk,hsb,HTML,gray]
\testcolor{olive}
\testcolor{red!50!green}
\testcolor{-cyan!50!magenta}
\testcolor[cmyk]{0,0,1,0.5}
\testcolor[cmyk]{0,0,.5,.5}
\testcolor[rgb:cmyk]{0,0,.5,.5}
\end{testcolors}

\end{document}

enter image description here

In short, the analysis in the original answers is now, basically, a macro in the xcolor package.

  • Can you update your answer to something like \HTMLcolour{<colour>} that outputs the HTML colour specification as requested in the question? – Werner Sep 30 '18 at 15:41
  • @Werner Let me give it a relook and see. What you're asking about is "output," while the title is asking about "retrieval." – Saphar Koshet Sep 30 '18 at 15:48
  • @Werner By addressing this one, you've helped me spot an answer to a different issue. Back to the "output" question. – Saphar Koshet Sep 30 '18 at 15:53
  • @Werner xcolor driver issue? It may be tomorrow. xcolor-material, kvoptions under test. – Saphar Koshet Sep 30 '18 at 17:38
  • @Werner Started with bad copy/paste of percusse / Carlisle code. That solution generates the HTML "output." It's definitely the situation that mere "retrieval" of color command / spec info and "use" of it are two different things. tex.stackexchange.com/a/452299/170846 My "update" addressed the text of the question. To understand that the "code" expands on the text of the question, we go from "mere retrieval" of color info to "use" of it. The percusse / Carlisle code makes "use" of the retrieved info and produces the columnar HTML output. So, I may need a rephrasing of your ques. – Saphar Koshet Oct 1 '18 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.