Draw a palette box in LaTeX

\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}

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

Tags:

Color

Boxes