pgf-tikz / pgf

A Portable Graphic Format for TeX
https://pgf-tikz.github.io/
1.14k stars 108 forks source link

use of color internals #1244

Open u-fischer opened 1 year ago

u-fischer commented 1 year ago

I'm trying to improve the use of spot colors in LaTeX (for some background see https://www.tug.org/TUGboat/tb43-2/tb134fischer-spotcolor.pdf) and also to unify the color packages.

A good solution for this problem requires the switch to the color commands provided the l3color module. This is complicated as various packages use "internal" commands of the color and xcolor package and would break if these internal commands change. (This is not meant as a reproach, due to the long history of these packages I'm quite aware that the border between internal and public interfaces are quite blurry and that some uses of internal commands simply reflect the fact that some interfaces were or still are missing.)

Problematic uses of internal color commands in pgf are from my point of view for example

muzimuzhi commented 1 year ago

One side question: currently l3color only supports ! operator in color expression, which is far less than those provided by xcolor. Will l3color gradually add more operators?

u-fischer commented 1 year ago

I'm not sure, that's something for @josephwright. Personally I think that xcolor is overdoing a bit. I certainly never used anything else than !. Do you have real use cases for the other operators?

muzimuzhi commented 1 year ago

Do you have real use cases for the other operators?

Just did a rough test and it seems at least the pgfmanual doesn't use operators other than !, see branch test/l3color.

But I can recall some use cases: <name>!!+ for color series used in tabulars, extended color expression like rgb,255:red,231;green,84;blue,121 used in some TeX-SX Q&A[^1], and -<name> used for complement color.

[^1]: for example https://tex.stackexchange.com/a/343680

muzimuzhi commented 1 year ago

I've filed latex3/latex3#1181 so discussions here won't be too off-topic.

muzimuzhi commented 11 months ago

I would be grateful if you could look if pgf could switch here to "official" interfaces. I'm also interested to know if some interfaces are missing in the color packages.

@u-fischer It seems pgf could stick to \extractcolorspec and \extractcolorspecs. Quick versions of these two commands that only take a color name, rather than a color expression are welcome. I'll look into this.

u-fischer commented 11 months ago

I could add a test if that the color exist. That should make them quite fast:

\documentclass{article} 

\usepackage{xcolor,l3benchmark} 
\makeatletter %\let\XR@useURL\XR@URL

\def\XC@extractcolorspec#1#2#3#4%
 {\XC@sdef\@@mod{#3}\edef\@@clr{#4}\edef\@@tmp{{\@@mod}{#4}}}%

\def\extractcolorspec#1#2%
 {%  
  \ifcsname \@backslashchar color@#1\endcsname 
   \begingroup 
   \let\xcolor@\XC@extractcolorspec   
    \@nameuse{\@backslashchar color@#1}%
   \aftergroupdef#2\@@tmp 
  \else 
   \XC@split{#1}\edef\@@tmp{{\@@mod}{\@@clr}}\aftergroupdef#2\@@tmp
  \fi 
 }

\begin{document}

\extractcolorspec{red}\mycolor \show\mycolor

\extractcolorspec{red!50!blue}\mycolor \show\mycolor
\ExplSyntaxOn
\benchmark:n {\extractcolorspec{red}\mycolor}

\benchmark:n {\extractcolorspec{red!50!blue}\mycolor}
\ExplSyntaxOff
abc
\end{document}
muzimuzhi commented 10 months ago

More findings, still incomplete

u-fischer commented 10 months ago
  • found in \pgfutil@doifcolorelse{}{}{}, used to test if a color is defined, so tikz can distinguish \draw[red] (color expression as unknown key)

We should provide a counterpart to \color_if_exist:nTF in l3color. The main question is the name. Perhaps \IfColorExistTF? But I wonder a bit about the pgf code, this handles only defined color names, like red, but tikz actually also can handle color expressions, e.g. \draw[red!50!white], obviously in the unknown key handler. Why doesn't handle red there too?

  • found in \pgfutil@colorlet{<name>}{<color>}

Imho the xcolor documentation is confusing here. It uses named both for the special named color model useful only with postscript and for the general concept of a color name like red or Goldenrod. Imho pgf wants here to force a color conversion into a target model (in case this has been set with \selectcolormodel). Using [named] for this works but is imho conceptuelly wrong. The correct things would be imho \colorlet{#1}[\XC@tgt@mod{}]{#2}. (\XC@tgt@mod{} expands either to empty or to a color model). Imho we should here provide a command which expands to the target model, similar to \l_color_fixed_model_tl in l3color. Any suggestion about a good name.

  • redefinition of \set@color and \reset@color

It could be that this is related to the missing support of fill and stroke colors in xcolor and could be resolved by switching to l3color as underlying color system. But that requires first to get rid of the use of \string\color@XXX

@josephwright any comments on the names for the public interfaces?