olsak / OpTeX

OpTeX - LuaTeX format with extended Plain TeX macros
http://petr.olsak.net/optex/
33 stars 14 forks source link

Colors and overriding them #177

Open vlasakm opened 2 months ago

vlasakm commented 2 months ago

In OpTeX colors are defined like this:

\_def\Blue {\_setcmykcolor{1 1 0 0}}
\_def\White {\_setgreycolor{1}}

Macros like \_setcmykcolor or \_setgreycolor expand to uses of the \_setcolor macro with the PDF operators added:

\_def\_setcmykcolor#1{\_setcolor{#1}kK}
\_def\_setrgbcolor#1{\_setcolor{#1}{rg}{RG}}
\_def\_setgreycolor#1{\_setcolor{#1}gG}

and \_setcolor sets the current TeX color on the very primitive TeX level (now with attributes, previously with PDF color groups if I remember correctly).

Now, for normal typesetting this is perfectly fine, but I have a two use cases where we may want colors (like \Blue or use of the macros \setrgbcolor documented in user documentation) to do something else than "change TeX color", and even make them expandable.

First use case I have in mind is use of the colors in packages like TikZ and Metapost both of which have integration with OpTeX.

The second is in my pdfextra package, where I want to allow the user to say in key value argument: background=\White and it expands to space separated RGB triplet (R G B) behind the scenes, due to the way the PDF format needs colors to be present - but I still want OpTeX users to be able to use their defined colors, mixed with the \colordef syntax, etc.

@reuleaux recently reminded me about the first use case in https://github.com/vlasakm/optex-minim/issues/2, since he naturally wants to be able to use OpTeX colors in \directmetapost (implemented through "optex-minim", now part of OpTeX as minim-mp.opm). But I actually realized a couple of problems, where I am not sure what is the best thing to do.

E.g. I can set:

\_def\_mp_rgb #1 #2 #3 ;{(#1, #2, #3)\_space}
\_def\_mp_cmyk #1 #2 #3 #4 ;{(#1, #2, #3, #4)\_space}
\_def\_mp_grey #1 ;{#1\_space}

\_def\_setrgbcolor#1{\_mp_rgb #1 ;}%
\_def\_setcmykcolor#1{\_mp_cmyk #1 ;}%
\_def\_setgreycolor#1{\_mp_grey #1 ;}%

To parse the OpTeX 1 0 0 into (1, 0, 0) etc. that metapost expects, fully expandably.

But what if user does \onlyrgb or \onlycmyk? They can also do it both before and after they \load[minim-mp]. Is the user allowed to redefine \_setrbgcolor? What about \_setrbgcolor? Should they be kept in sync? What if they are not?

I can think of some ways on how to do the checks, and if the user doesn't play with \_setrbgcolor and \setrbgcolor, they should work. But I was interested on what is the general stance about this, since I would like to implement it for both pdfextra and minim-mp.opm. Also, are there any use other cases we may want to consider for colors? E.g. what if I want \colortohex\Red, which would produce 0xFF0000, what is the recommended way to do it? I will probably check whether TikZ can also benefit from colors.