Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to color by color series name does not work #8

Open
schtandard opened this issue Nov 3, 2021 · 5 comments
Open

Access to color by color series name does not work #8

schtandard opened this issue Nov 3, 2021 · 5 comments

Comments

@schtandard
Copy link

(I reported this bug via email a couple years ago, but it seems maintenance has changed, so I report it again here.)

The following MWE produces a black square even though it should produce a blue one according to the documentation.

\documentclass{article}

\usepackage{xcolor}

\begin{document}

\definecolorseries{foo}{rgb}{last}{blue}{red}
\resetcolorseries{foo}

\textcolor{foo}{\rule{1ex}{1ex}}

\end{document}

From what I understood, this is due to the fact that the second argument of \xcolor@ in \\color@foo is empty.

As a workaround, one can use the (undocumented) syntax \color{foo!!}, but the documented \color{foo} would be preferred.

@u-fischer
Copy link
Member

I'm not sure it is a bug. The code quite explicitly stores the color without backend, and nearly all examples use the !!+ prefix.

But the following should work and set the color with backend:

\documentclass{article}

\usepackage{xcolor}

\makeatletter
\def\XC@resetcolorseries[#1]#2%
 {\begingroup
  \@namexdef
   {\@backslashchar color@#2}{\noexpand\xcolor@{}{}\@nameuse{\@backslashchar colorB@#2}}%
  \xglobal\colorlet{foo}{foo!}% <----
  \XC@let@Nc\@@tmp{\@backslashchar colorD@#2}%
  \edef\@@met{\expandafter\@firstoftwo\@@tmp}%
  \edef\@@tmp{\expandafter\@secondoftwo\@@tmp}%
  \ifx\@@met\XC@met@step\else
    \edef\@@scl{\@ifxempty{#1}\colorseriescycle{#1}}%
    \expandafter\XC@calc@\@@tmp,,,,:D%
  \fi
  \@namexdef{\@backslashchar colorS@#2}{\@@tmp}%
  \endgroup}
\makeatother

\begin{document}

\definecolorseries{foo}{rgb}{last}{blue}{red}
\resetcolorseries[4]{foo}
  
xx \textcolor{foo}{\rule{1ex}{1ex}}

\end{document}

@schtandard
Copy link
Author

I think the documentation is pretty explicit that just using the color series name should be fine, e.g.

A ⟨name⟩ denotes the declared name (or the name to be declared) of a color or a color series; [...]

from section 2.3.1 or

[...] commands like \color{foo} or \color{foo!!+} may be used without restrictions [...]

from section 2.9.4.

(Uwe Kern also seemed to agree: He wrote "das wirkt tatsächlich wie ein Bug" in reply to my email reporting this.)

@schtandard
Copy link
Author

Regarding your suggestion: It seems to only work as long as the color series isn't stepped:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{xcolor}

\makeatletter
\def\XC@resetcolorseries[#1]#2%
 {\begingroup
  \@namexdef
   {\@backslashchar color@#2}{\noexpand\xcolor@{}{}\@nameuse{\@backslashchar colorB@#2}}%
  \xglobal\colorlet{foo}{foo!}% <----
  \XC@let@Nc\@@tmp{\@backslashchar colorD@#2}%
  \edef\@@met{\expandafter\@firstoftwo\@@tmp}%
  \edef\@@tmp{\expandafter\@secondoftwo\@@tmp}%
  \ifx\@@met\XC@met@step\else
    \edef\@@scl{\@ifxempty{#1}\colorseriescycle{#1}}%
    \expandafter\XC@calc@\@@tmp,,,,:D%
  \fi
  \@namexdef{\@backslashchar colorS@#2}{\@@tmp}%
  \endgroup}
\makeatother

\begin{document}

\definecolorseries{foo}{rgb}{last}{blue}{red}
\resetcolorseries{foo}

% works now
\textcolor{foo}{\rule{1ex}{1ex}}

% always worked
\textcolor{foo!!+}{\rule{1ex}{1ex}}

% still doesn't work
\textcolor{foo}{\rule{1ex}{1ex}}

\end{document}

@u-fischer
Copy link
Member

I think the documentation is pretty explicit that just using the color series name should be fine,

I didn't find it very explicit. On the whole I had the impression that it doesn't really consider the case of using the color without stepping. In any case: one must set the color also in the step code. Be aware that foo always refers to the currently active color of a series.

\documentclass{article}

\usepackage{xcolor}

\makeatletter
\def\XC@resetcolorseries[#1]#2%
 {\begingroup
  \@namexdef
   {\@backslashchar color@#2}{\noexpand\xcolor@{}{}\@nameuse{\@backslashchar colorB@#2}}%
  \xglobal\colorlet{foo}{foo!}% <----
  \XC@let@Nc\@@tmp{\@backslashchar colorD@#2}%
  \edef\@@met{\expandafter\@firstoftwo\@@tmp}%
  \edef\@@tmp{\expandafter\@secondoftwo\@@tmp}%
  \ifx\@@met\XC@met@step\else
    \edef\@@scl{\@ifxempty{#1}\colorseriescycle{#1}}%
    \expandafter\XC@calc@\@@tmp,,,,:D%
  \fi
  \@namexdef{\@backslashchar colorS@#2}{\@@tmp}%
  \endgroup}
  
\def\XC@step#1%
 {\let\xcolor@\@gobbletwo\edef\@@tmp{\@nameuse{\@backslashchar color@#1}}%
  \expandafter\XC@st@p\@@tmp{#1}%
  \@namexdef{\@backslashchar color@#1}{\noexpand\xcolor@{}{}{\@@mod}{\@@tmp}}%
  \xglobal\colorlet{foo}{foo!}% <----
  }  
\makeatother

\begin{document}

\definecolorseries{foo}{rgb}{last}{blue}{red}
\resetcolorseries[2]{foo}

% works now
\textcolor{foo}{\rule{1ex}{1ex}}

\expandafter\show\csname \string\color@foo\endcsname
% always worked
\textcolor{foo!!+}{\rule{1ex}{1ex}}

\expandafter\show\csname \string\color@foo\endcsname
% still doesn't work
\textcolor{foo}{\rule{1ex}{1ex}}

\end{document}

@schtandard
Copy link
Author

schtandard commented Nov 11, 2021

I didn't find it very explicit. On the whole I had the impression that it doesn't really consider the case of using the color without stepping.

Hmm, ok. I thought stating that one may use \color{foo} after saying \definecolorseries{foo}{rgb}{last}{red}{blue}\resetcolorseries[10]{foo} was conclusive. And section 2.3.1 / table 4 do state that the ⟨postfix⟩ in an ⟨expr⟩ may be empty. But anyway, even if this is not considered a bug, providing this syntax seems like an improvement to the package to me, so I hope it can be included in the next version.

Your last MWE looks good to me. At least I couldn't find any issues playing around with it for a bit.

Thank you!

davidcarlisle added a commit that referenced this issue Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants