答案1
\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}
我一开始没有意识到文本的顶行实际上是 RGB 转换为十六进制。因此,可以从 RGB 计算出来,并将参数数量从两个减少到一个。我使用该binhex.tex
包将其转换为十六进制。
\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}
答案2
这是一个使用 的解决方案tikz
。它用于expl3
自动计算十六进制值。
\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}