![tikz 将颜色字符串转换为十六进制值](https://linux22.com/image/393526/tikz%20%E5%B0%86%E9%A2%9C%E8%89%B2%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E5%80%BC.png)
我希望将 tikz 中的颜色字符串转换为十六进制值。
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{box/.style={draw,outer sep=0,minimum size=1cm}}
\def\dy{1}
\foreach \col [count=\i] in {red,orange,yellow,green,cyan,blue,purple} {
\def\mycol{\col!40!gray}
\node[box,yshift=1cm*\i*\dy,fill=\mycol,label=left:\mycol] (main) {ABC};
}
\end{tikzpicture}
\end{document}
所以我希望展示red!40!grey
类似的东西#xxxxxxxx
但不展示如何进行这样的转换!
答案1
也许是这个(带有\extractcolorspecs
和\convertcolorspec
来自xcolor
包,由包加载tikz
)?
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\tikzset{box/.style={draw,outer sep=0,minimum size=1cm}}
\def\dy{1}
\foreach \col [count=\i] in {red,orange,yellow,green,cyan,blue,purple} {
\def\mycol{\col!40!gray}
\extractcolorspecs{\mycol}{\modelcmd}{\colorcmd}
\convertcolorspec{\modelcmd}{\colorcmd}{HTML}\hex
\node[box,yshift=1cm*\i*\dy,fill=\mycol,label=left:\mycol] (main) {\#\hex};
}
\end{tikzpicture}
\end{document}
答案2
不是在中完成的tikz
,而是使用colorinfo
包来提取颜色信息,并使用binhex.tex
包将颜色信息转换为十六进制格式。
在这里扩展我的答案在 LaTeX 中绘制调色板框允许根据 LaTeX 颜色规范进行规范。
\documentclass{article}
\usepackage{xcolor,stackengine,colorinfo}
\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}}}
\newlength\rcomp
\newlength\gcomp
\newlength\bcomp
\newcommand\colcomponents[1]{\expandafter\colcomponentsaux#1\relax}
\def\colcomponentsaux#1,#2,#3\relax{%
\setlength\rcomp{\dimexpr255\dimexpr#1pt\relax+.5pt\relax}%
\setlength\gcomp{\dimexpr255\dimexpr#2pt\relax+.5pt\relax}%
\setlength\bcomp{\dimexpr255\dimexpr#3pt\relax+.5pt\relax}%
}
\newcommand\truncatergb[1]{\expandafter\truncatergbaux#1\relax}
\def\truncatergbaux#1.#2\relax{#1}
\newcommand\xpalbox[1]{%
\colorlet{mycolor}{#1}%
\setbox0=\hbox{\colorInfo{mycolor}}%
\colcomponents{\colorValue}%
\edef\Rcomp{\truncatergb{\the\rcomp}}%
\edef\Gcomp{\truncatergb{\the\gcomp}}%
\edef\Bcomp{\truncatergb{\the\bcomp}}%
\edef\tmp{\expandafter\expandafter\expandafter\Rcomp
\expandafter\expandafter\expandafter\space
\expandafter\Gcomp\expandafter\space\Bcomp}%
\expandafter\palbox\expandafter{\tmp} = \colorbox{mycolor}{#1}%
\par
}
\begin{document}
\xpalbox{blue!40!red!25}
\xpalbox{blue!60!green!45}
\end{document}
答案3
基于@quark67 的答案的另一个例子:
\documentclass[border=1mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning,chains}
\begin{document}
\begin{tikzpicture}[start chain=M1 going below,node distance=0]
\tikzset{box/.style={draw,outer sep=0,minimum width=2cm,on chain=M1}}
\def\dy{1}
\foreach \col [count=\i] in {red,orange,yellow,green,cyan,blue,purple,
{rgb,255:red,0; green,255; blue,0},
{rgb:red,1;green,1;yellow,1}} {
\extractcolorspecs{\col}{\modelcmd}{\colorcmd}
\convertcolorspec{\modelcmd}{\colorcmd}{HTML}\hex
\node[box,fill=\col,label=right:\col] (main) {\#\hex};
}
\end{tikzpicture}
\end{document}