\convertdirectly 和 xcolor 的问题

\convertdirectly 和 xcolor 的问题

我一直在与如何从颜色中获取色调、饱和度和亮度值?遇到了一个问题。这个非常有用的函数\convertdirectly的定义(复制自https://tex.stackexchange.com/a/283618) 并从中\hue定义命令。这将返回给定颜色的色调 (hsb):\hue{orange}例如0.08333

其他颜色,例如紫色(色调=0.94444.75)给出了意想不到的色调数字。很容易知道发生了什么,但不知道该如何修复,因为代码看起来非常简单。

\documentclass{article}
\usepackage{xcolor}
% from https://tex.stackexchange.com/a/283618
% https://tex.stackexchange.com/questions/509949/how-do-i-get-the-hue-saturation-and-brightness-values-from-a-color
\newcommand{\convertdirectly}[3][hsb]{\begingroup%
  \extractcolorspecs{#2}{\modelcmd}{\colorcmd}%
  \convertcolorspec{\modelcmd}{\colorcmd}{#1}{\tmp}%
  \message{#2 in #1 is \tmp^^J}%
  \aftergroupdef#3\tmp}
\def\First#1,#2,#3{#1}
\def\Second#1,#2,#3{#2}
\def\Third#1,#2,#3{#3}
\newcommand{\hue}[1]{\convertdirectly{#1}{\tmp}%
\expandafter\First\tmp}
\newcommand{\Hue}[2]{\begingroup\convertdirectly{#1}{\tmp}%
\edef\res{\expandafter\First\tmp}%
\aftergroupdef#2\res}
\newcommand{\saturation}[1]{\convertdirectly{#1}{\tmp}%
\expandafter\Second\tmp}
\newcommand{\Saturation}[2]{\begingroup\convertdirectly{#1}{\tmp}%
\edef\res{\expandafter\Second\tmp}%
\aftergroupdef#2\res}
\newcommand{\brightness}[1]{\convertdirectly{#1}{\tmp}%
\expandafter\Third\tmp}
\newcommand{\Brightness}[2]{\begingroup\convertdirectly{#1}{\tmp}%
\edef\res{\expandafter\Third\tmp}%
\aftergroupdef#2\res}
\newcounter{myc}
\begin{document}

\parindent0pt

\def\testit#1{The hue of #1 is \hue{#1} (hsb spec: \tmp).\par}

\testit{orange}
\testit{red}

\bigskip

\testit{purple}
\testit{red!60!blue}
\testit{red!80!purple}

\end{document}

%% |=====8><----| %%

色调输出

答案1

\First \Second\Third需要#3分隔符 -- 我选择了 vertbar。现在一切都按预期运行。

\documentclass{article}
\usepackage{xcolor}
% from https://tex.stackexchange.com/a/283618
% https://tex.stackexchange.com/questions/509949/how-do-i-get-the-hue-saturation-and-brightness-values-from-a-color
\newcommand{\convertdirectly}[3][hsb]{\begingroup%
  \extractcolorspecs{#2}{\modelcmd}{\colorcmd}%
  \convertcolorspec{\modelcmd}{\colorcmd}{#1}{\tmp}%
  \message{#2 in #1 is \tmp^^J}%
  \aftergroupdef#3\tmp}
\def\First#1,#2,#3|{#1}
\def\Second#1,#2,#3|{#2}
\def\Third#1,#2,#3|{#3}
\newcommand{\hue}[1]{\convertdirectly{#1}{\tmp}%
\expandafter\First\tmp|}
\newcommand{\Hue}[2]{\begingroup\convertdirectly{#1}{\tmp}%
\edef\res{\expandafter\First\tmp|}%
\aftergroupdef#2\res}
\newcommand{\saturation}[1]{\convertdirectly{#1}{\tmp}%
\expandafter\Second\tmp|}
\newcommand{\Saturation}[2]{\begingroup\convertdirectly{#1}{\tmp}%
\edef\res{\expandafter\Second\tmp|}%
\aftergroupdef#2\res}
\newcommand{\brightness}[1]{\convertdirectly{#1}{\tmp}%
\expandafter\Third\tmp|}
\newcommand{\Brightness}[2]{\begingroup\convertdirectly{#1}{\tmp}%
\edef\res{\expandafter\Third\tmp|}%
\aftergroupdef#2\res}
\newcounter{myc}
\begin{document}

\parindent0pt

\def\testit#1{The hue of #1 is \hue{#1} (hsb spec: \tmp).\par}

\testit{orange}
\testit{red}

\bigskip

\testit{purple}
\testit{red!60!blue}
\testit{red!80!purple}

\end{document}

%% |=====8><----| %%

正确的输出

相关内容