7

I want to define a new colour (myorange) by changing the hue value of a previously given colour (oldcolour), thus maintaining its original saturation and brightness. Is there a way for doing this?

I can think of two strategies. First, having a function \changehue that returns a colour from another, but with a hue defined by the user. It would be used as follows:

\colorlet{myorange}{\changehue{oldcolour}{30}}

Or maybe:

\colorlet{myorange}{\changehue{oldcolour}{orange}}

In this last case, \changehue would extract the hue of orange and plug it into the code. This way would be better for me.

In the second strategy, we just define the colour with the definecolor with the hsb colour model with the help of functions for extracting the hue, saturation and brightness of the colour previously given.

\definecolor{myorange}{hsb}{\hue{orange}, \saturation{oldcolour}, \brightness{oldcolour}}

I strongly prefer this second strategy, which is the way presupposed by my question. It would be useful to know if commands like \hue, \saturation, and \brightness exist or can be defined.

Thanks in advance for your help.

3
  • 1
    This varies the hue value: \documentclass{article} \usepackage{xcolor} \newcounter{myc} \begin{document} \definecolorseries{changehue}{hsb}{step}{orange}{0,0.1,0} \resetcolorseries[12]{changehue}% \loop\stepcounter{myc}\colorlet{mycolor}{changehue!![\number\value{myc}]} \textcolor{mycolor}{X}\ifnum\value{myc}<11\repeat \end{document} but you want to retrieve it from a given color, right?
    – user194703
    Sep 27, 2019 at 3:01
  • Thanks, @Schrödinger'scat. Your proposal is very helpful and corresponds to the first strategy. It would be great, though, if something approximating the second could be proposed.
    – lfba
    Sep 27, 2019 at 3:06
  • I agree with that, which is why I upvoted this question. ;-)
    – user194703
    Sep 27, 2019 at 3:08

1 Answer 1

5

This might be a partial answer. You can define a color series and vary, say, the hue value. The perhaps more important information is that \extractcolorspecs allows you to extract the color specifications of a given color. This can be converted to a given color scheme with \convertcolorspec. The last two commands have been merged to the \convertdirectly command, which I am recycling here.

\documentclass{article}
\usepackage{xcolor}
% from https://tex.stackexchange.com/a/283618
\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}
\definecolorseries{changehue}{hsb}{step}{orange}{0,0.1,0}
\resetcolorseries[12]{changehue}%
\loop\stepcounter{myc}\colorlet{mycolor}{changehue!![\number\value{myc}]}%
\extractcolorspecs{mycolor}{\modelcmd}{\colorcmd}%
model=\textcolor{mycolor}{\modelcmd},%
color=\textcolor{mycolor}{\colorcmd}\par\ifnum\value{myc}<11\repeat


the hsb ``code'' of orange is \convertdirectly{orange}{\hsborange}\hsborange

the hsb ``code'' of brown is \convertdirectly{brown}{\hsbbrown}\hsbbrown

hue of orange=\hue{orange}

saturation of orange=\saturation{orange}

brightness of orange=\brightness{orange}

\Hue{orange}{\myhue}\Saturation{blue}{\mysaturation}\Brightness{brown}\mybrightness

\definecolor{myorange}{hsb}{\myhue,\mysaturation,\mybrightness}

\textcolor{myorange}{Test}
\end{document}

enter image description here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .