7

Is there a way to build a structure like the following in Latex?

Color palette

They're probably just boxes, but I have very little experience with Latex and I'm not sure how to get this result.

  • Welcome to TeX.SX. Questions about how to draw specific graphics that just post an image of the desired result are really not reasonable questions to ask on the site. Please post a minimal compilable document showing that you've tried to produce the image and then people will be happy to help you with any specific problems you may have. See minimal working example (MWE) for what needs to go into such a document. – Stefan Pinnow Feb 15 at 11:36
  • Yes, it is possible. Here are some examples, not boxed, but with palletes. texample.net/tikz/examples/tag/color-manipulation – Sigur Feb 15 at 11:56
14
\documentclass{article}
\usepackage{xcolor,stackengine}
\newcommand\palbox[2]{{\sffamily\fboxsep=5pt\relax\fboxrule=1pt\relax\footnotesize%
  \fcolorbox{gray!50}{gray!10}{%
    \stackengine{8pt}{%
      \colorbox[RGB]{#1}{\rule{60pt}{0pt}\rule{0pt}{60pt}}%
    }{%
      \color{black!60}\stackengine{12pt}{\##2}{\saycolors{#1}}{U}{l}{F}{F}{S}%
    }{U}{l}{F}{F}{S}%
  }%
}}
\newcommand\saycolors[1]{\saycolorsaux#1\relax}
\def\saycolorsaux#1 #2 #3\relax{R:#1 G:#2 B:#3}
\begin{document}
\palbox{1 103 143}{01678f}\quad
\palbox{221 109 16}{dd6d10}\quad
\palbox{18 54 69}{123645}\quad
\palbox{120 121 124}{78797c}
\end{document}

enter image description here

I hadn't at first realized that the top line of text is actually the RGB converted to hex. Thus, one can calculate that from the RGB and can reduce the number of arguments from two to one. I use the binhex.tex package for the conversion into hex.

\documentclass{article}
\usepackage{xcolor,stackengine}
\input binhex.tex
\newcommand\palbox[1]{{\sffamily\fboxsep=5pt\relax\fboxrule=1pt\relax\footnotesize%
  \fcolorbox{gray!50}{gray!10}{%
    \stackengine{8pt}{%
      \colorbox[RGB]{#1}{\rule{60pt}{0pt}\rule{0pt}{60pt}}%
    }{%
      \color{black!60}\stackengine{12pt}{\intohex{#1}}{\saycolors{#1}}{U}{l}{F}{F}{S}%
    }{U}{l}{F}{F}{S}%
  }%
}}
\newcommand\saycolors[1]{\saycolorsaux#1\relax}
\def\saycolorsaux#1 #2 #3\relax{R:#1 G:#2 B:#3}
\newcommand\intohex[1]{\#\intohexaux#1\relax}
\def\intohexaux#1 #2 #3\relax{\twodigithex{#1}\twodigithex{#2}\twodigithex{#3}}
\newcommand\twodigithex[1]{\ifnum#1<16\relax0\fi\MakeLowercase{\hex{#1}}}
\begin{document}
\palbox{1 103 143}\quad
\palbox{221 109 16}\quad
\palbox{18 54 69}\quad
\palbox{120 121 124}
\end{document}
  • Amazing (twice). – Sigur Feb 15 at 12:41
  • @Sigur Thanks. (thanks). Macros make the world go round. – Steven B. Segletes Feb 15 at 12:44
  • What I liked is that the 1st argument is a list of numbers separated by spaces, not comma. Very useful. – Sigur Feb 15 at 12:46
  • @Sigur Often, one can structure the \def arguments (in this case, of \saycolorsaux) to achieve the desired parsing. When that proves too difficult, I always fall back on the listofitems package, which is unsurpassed in its ability to parse a difficult construct into an expandable array structure.. – Steven B. Segletes Feb 15 at 12:50
  • What about using \MakeLowercase{}? – Sigur Feb 15 at 15:36
7

Here's a solution using tikz. It uses expl3 to automatically calculate the hex value.

\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{positioning, backgrounds, shadows}
\ExplSyntaxOn
\cs_new:Nn \__criw_rgbtohex:n
  {
    \clist_set:Nx \l_tmpa_clist {#1}
    \clist_map_inline:Nn \l_tmpa_clist
      {
        \int_compare:nNnT { ##1 } < { 16 } { 0 }
        \int_to_hex:n {##1}
      }
  }
\cs_new:Nn \__criw_palette_box:n
  {
    \clist_set:Nx \l_tmpa_clist {#1}
    \definecolor { palettecolour } { RGB } {#1}
    \begin {tikzpicture}
      [
        node~distance = 4mm,
        inner~sep = 0mm,
        every~node/.style = { font = \sffamily\footnotesize }
      ]
      \node (colour)
        [
          fill = palettecolour,
          minimum~width = 3cm,
          minimum~height = 3cm
        ]
        { } ;
      \node (hex)
        [
          below = of~colour.south~west,
          anchor = north~west
        ]
        { \#\__criw_rgbtohex:n {#1} } ;
      \node (rgb)
        [
          below = of~hex.south~west,
          anchor = north~west
        ]
        { R: \clist_item:Nn \l_tmpa_clist { 1 }~
          G: \clist_item:Nn \l_tmpa_clist { 2 }~
          B: \clist_item:Nn \l_tmpa_clist { 3 } } ;
      \begin {scope} [ on~background~layer ]
        \shadedraw 
          [
            left~color = white,
            right~color = black!10,
            draw = black!15,
            drop~shadow =
              {
                shadow~xshift = 0.5mm,
                shadow~yshift = -0.5mm,
                fill = black!40,
                opacity = 1
              }
          ]
          (current~bounding~box.south~west) + (-2mm, -2mm)
          rectangle
          ( [ shift = { (2mm, 2mm) } ] current~bounding~box.north~east) ;
      \end {scope}
    \end {tikzpicture}
  }
\NewDocumentCommand \PaletteBox { m }
  {
    \__criw_palette_box:n {#1}
  }
\ExplSyntaxOff
\begin{document}
\PaletteBox{1, 103, 143}\quad
\PaletteBox{221, 109, 16}\quad
\PaletteBox{18, 54, 69}\quad
\PaletteBox{120, 121, 124}
\end{document}

output

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.