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

using optex color definitions in minim - not an issue - but help needed, thanks #2

Open
reuleaux opened this issue May 4, 2024 · 6 comments

Comments

@reuleaux
Copy link

reuleaux commented May 4, 2024

Hi, thanks for creating optex-minim - I am doing my first baby steps with optex + minim (your optex-minim i.e.) + metapost (while also looking at the context docs) etc. - having some experience with latex/context, but less so with plain tex - i.e. I am learning all these at the same time - so bear with me, please.

I can (partly) set the background of some pages in optex with metapost in color, like so:

\pgbackground={
  \directmetapost{
    beginfig(1);
    numeric w ; w := 210mm ;
    numeric h ; h := 297mm ;
    fill unitsquare xscaled .3w yscaled .122h withcolor (0.655, 0.741, 0.690);
    fill unitsquare xscaled .3w yscaled .878h yshifted .122h  withcolor (0.745, 0.855, 0.690) ;
    % and so on
    endfig;
  }
}

i.e. I want to use slightly more complicated colors - than the usual red/green/blue...

Now, ideally I would use my optex color definitions - there are various ways to do them:
(with \setrgbcolor, \setHEXcolor etc. - you know the optex docs)

\def \CornerDarkGreen {\setrgbcolor{0.655 0.741 0.690}}
\def \SideGreen {\setHEXColor{bedab0}}

etc. - now, I am having a hard time accessing / using such definitions in metapost
(and I am not sure: do I need to write a macro to do so? Can I directly access
optex definitions within minim - the minim docs seem to suggest so:
there is a mention of \mpcolor - well for latex, not optex - and then: a (very general)
section "running tex from within metapost" - but there are not really any examples given.

note that this works (but sets only a certain red level):

\def\mycolor{.625red}

\pgbackground={
  \directmetapost{
    beginfig(1);
    numeric w ; w := 210mm ;
    numeric h ; h := 297mm ;
    fill unitsquare xscaled .7w yscaled .122h xshifted .3w withcolor \mycolor ;
    endfig;
  }
}

I don't really know: maybe this is general TeX knowledge - but I am not aware: where does this syntax come from:
{.625red} - and more importantly: how to use it a little more generally:
this does not work

% not working!!
\def \myothercolor{.625red .8blue}

maybe I need a comma in between red and blue, maybe + - I think I have tried everything.

maybe I can just use \mpcolor{\CornerDarkGreen} in the metapost above - I think I have tried this, without success!

Maybe you could provide some hints - or ideally even add an example (to your examples directory)? Thanks!

@vlasakm
Copy link
Owner

vlasakm commented May 5, 2024

Hello!

optex-minim in this repository is more or less obsolete - I should maybe archive the repository. It is not needed at all, as it has been integrated into OpTeX itself - i.e. there are minim .opm files in https://github.com/olsak/OpTeX/tree/master/optex/pkg. I hope you are using that! Because of that I think it would be best to discus things there, as the any potential change needs to happen there.

\setHEXcolor

Actually I don't know that one. Where does it come from?

Anyways, unfortunately I wouldn't even call myself a Metapost novice, since I have hardly used it (funnily enough, since I integrated it with optex through minim), but I think I can fill some details for you.

My impression from the Metapost manual and a tugboat article is, that withcolor expects to be followed by a color expression, which in turn is a "normal metapost expression". This is kind of subtle, because there is a trick - colors in metapost are essentially triplets (in case of rgb colors, i.e. (R, G, B) where R G and B are numbers between 0 and 1), quadruplets (n case of cmyk colors, i.e. (C, M, Y, K) where C M Y and K are numbers between 0 and 1) or plain numbers (in n case of grey, i.e. G where G is a number between 0 and 1).

So either of this rows is a basic metapost expression, that may be interpreted as a color:

1 % black
(0,0,0) % rgb black
(1,0,0,0) % cyan

In fact I vaguely remember, that since Metapost has just pairs for 2D coordinates, people abuse RGB colors for 3D coordinates, since they are essentially triplets built into Metapost. And it seems that Metapost really handles colors specially only after it has been instructed to handle a triplet or quadruplet or number as a color - only then it actually "clamps" the components to the range 0 - 1. But maybe there are more details that I haven't yet come by.

With these essentially normal tuples or number we can form expressions, just like for pairs or ordinary numbers:

230/255 % light grey
(1,0,0) + (0,1,0) % yellow
.8 [(1,0,0), (0,0,1)] % purple, closer to blue than red

I.e. what works for points works for mixing colors as well. Even linear equations with variables. But since I am no Metapost expert, I restrained myself to these, since these seem good for basic color mixing.

Of course it would be a bit inconvenient to only use raw numbers everywhere, so Metapost defines this:

% color constants
color black, white, red, green, blue, background;
black = (0,0,0);
white = (1,1,1);
red = (1,0,0);
green = (0,1,0);
blue = (0,0,1);
background = white;   % The user can reset this

So it seems that these "names" are not that special, and defining new ones in Metapost is not a problem.

Our problem is then how to make something defined in OpTeX as:

\def \CornerDarkGreen {\setrgbcolor{0.655 0.741 0.690}}

expand in Metapost to:

(0.655, 0.741, 0.690)

This is just a matter of separaating the numbers by whitespace, anad inserting commas between them. Minim conveniently recently introduced a way to add definitions before and after each \directmetapost with \everymp. So then I think we can do this:

\fontfam[lm]

\load[minim-mp]

\_let\_optex_rgb=\_setrgbcolor
\_let\_optex_cmyk=\_setcmykcolor
\_let\_optex_grey=\_setgreycolor

\_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}

\expandafter\toksapp\everymp0{pre}{tex}{
  \_def\_setrgbcolor#1{\_mp_rgb #1 ;}%
  \_def\_setcmykcolor#1{\_mp_cmyk #1 ;}%
  \_def\_setgreycolor#1{\_mp_grey #1 ;}%
}

\expandafter\toksapp\everymp0{post}{tex}{
  \_let\_setrgbcolor=\_optex_rgb
  \_let\_setcmykcolor=\_optex_cmyk
  \_let\_setgreycolor=\_optex_grey
}

{\Red before in red}

\directmetapost{
beginfig(1)
  draw fullcircle scaled 10mm shifted (0mm, 0) withcolor \Green;
  draw fullcircle scaled 20mm shifted (15mm,0) withcolor .1\Green;
  draw fullcircle scaled 5mm  shifted (30mm,0) withcolor .8[\Red,\Blue];
endfig;
}

{\Blue after in blue}

\bye

I set \_setrgbcolor etc. to my definitions before \directmetapost through the pre hook of \everymp, then because \directmetapost doesn't seem to be a group, I need to revert these definitons in the post hook (both are "tex" and both are apply to instance 0, which is special instance number meaning all instances. The reparsing is relatively straightforward, though I am not sure whether it is better or not to include a \space after the colors.

I can then use OpTeX colors inside \directmetapost, but after \directmetapost the colors revert to their original meaning of "set TeX color", instead of "expand to text that metapost understands as color".

Unfortunately, mixing colors has some subtle problems. E.g. the following fails:

draw fullcircle scaled 20mm shifted (55mm,0) withcolor .1[\White,\Red];

This is because I am trying to do an arithmetic on white (which in OpTeX is defined as grey color) and red (which in OpTeX is defined as CMYK color), and Metapost doesn't know how to do arithmetic on CMYK and number:

Not implemented: (cmykcolor)-(known numeric).

Also, metapost definitions like white are mostly based on RGB (and RGB is the default color space), while in OpTeX it is CMYK (and grey for white / black), so mixing them is not possible out of the box. Use of \onlyrgb would in theory help, but this breaks, since it redefines \_setcmykcolor - a change my macros have no idea about.

There is probably a better solution for handling colors, so I will probably post an issue to the OpTeX repository asking for feedback, and possibilities on what to do with colors.

By the way, this works:

draw fullcircle scaled 20mm shifted (55mm,0) withcolor .625red + .8blue;

just as well as this:

\def\mycolor{.625red + .1blue}
[...]
draw fullcircle scaled 20mm shifted (55mm,0) withcolor \mycolor;

Metapost sees only the first text in both cases. The macro \mycolor gets expanded to .625red + .1blue before Metapost even runs. The syntax \def\mycolor{.625red + .1blue} is "primitive" (i.e. most basic way on which e.g. \newcommand in LaTeX is built upon), which allows to define a macro. It means that "\mycolor should from now on expand to .625red + .1blue. The brackets ({, }) are just delimiters that designate start and end of what is should be the meaning of the macro. Because you could also have:

\def \mycolor {  .625red + .1blue    }

Where the spaces sometimes are and sometimes aren't significant - the good old weird TeX.

And yes indeed, you are right that this deserves a case in examples after I figure out a good way to implement usage of OpTeX colors in minim Metapost - the above is just a rough idea (though you can probably use it in the mean time, since from above you know the limitations).

@reuleaux
Copy link
Author

reuleaux commented May 5, 2024

Hi, and many thanks for your detailed answers (this one: #2 - and
olsak/OpTeX#177). - I will have to take some time
to work through your answers :-) ... and I will get back later then.

some bits were my bad, I think, like e.g.
\setHEXcolor was from an older(?) version of opmac
tricks: https://petr.olsak.net/opmac-tricks-e.html#setrgbcolor
but apparently this does not work any more (or does it?), and so on.

and yes: I am using minim as integrated with optex (will continue
the discussion there with #177, I guess)

again: I will have to do this (work through this) step by step - and then will hopefully have some more detailed remarks - anyway: enough food to think about for now - thanks again!

@reuleaux
Copy link
Author

reuleaux commented May 9, 2024

OK - sorry for the delay: i was side tracked, but am back to optex/metapost/minim now - i.e. I am experimenting...

And the color arithemtic is easy enough to understand (thanks again for your detailed explanations).

Likewise the simple macros: \_mp_rgb etc. - separating the rgb values by spaces, and inserting commas etc.

Now, when I try the \expandafter\toksapp\everymp0{...} bits above, I get an error msg:

! Undefined control sequence.
l.33 \expandafter\toksapp\everymp
                               0{pre}{tex}{
? 

I assume, that's because \everymp was introduced only recently (as you explained), and while I am using a recent debian linux testing system, with optex (minim) installed from [debian pkg] texlive-luatex - this is showing its age: this is texlive (2023.20240207-1) - cf https://packages.debian.org/trixie/texlive-luatex
I am not quite sure, how to read these version numbers, but I guess the texlive snapshot is from 2023 sometime, with debian pkg adjustments from February this year[?], corresponding to:

zless /usr/share/doc/texlive-doc/optex/base/README.gz

which shows that I have optex 1.13 from Nov 2023

I see there is a more recent texlive-luatex pkg in the works: i.e. in experimental already: https://packages.debian.org/experimental/texlive-luatex
(2024.20240401-1), which will at some point then trickle into sid, and later testing. (this would be a texlive snapshot from 2024 sometime, as I assume - and thus with optex 1.14 - and newer minim)

It would be nice, of course, to have these more recent packages - and I could install this one from experimental (with apt pinning) - and have done so for other packages in the past - or I could try to install optex by hand (...) - but usually these efforts are not worth it: just waiting some time is easier usually, and safer (in the worst case they could break my system in some way - it's called experimental for a reason).

so, I guess, I will for now help myself with some simple number arithmetic (having some color tuples directly in my metapost code), and then get back to these everymp bits later (there is enough for me to learn still from traditional plain tex)

I guess, I will see: if I have more optex questions (and issues possibly): then it makes sense of course, to have a more recent version installed (so that I don't have to bother you with my outdated package questions).

Thanks again.

vlasakm added a commit to vlasakm/mmtex that referenced this issue May 12, 2024
@vlasakm
Copy link
Owner

vlasakm commented May 12, 2024

Now, when I try the \expandafter\toksapp\everymp0{...} bits above, I get an error msg [...] I assume, that's because \everymp was introduced only recently (as you explained), and while I am using a recent debian linux testing system, with optex (minim) installed from [debian pkg] texlive-luatex - this is showing its age: this is texlive (2023.20240207-1) - cf https://packages.debian.org/trixie/texlive-luatex I am not quite sure, how to read these version numbers, but I guess the texlive snapshot is from 2023 sometime, with debian pkg adjustments from February this year[?] [...]

I see there is a more recent texlive-luatex pkg in the works: i.e. in experimental already: https://packages.debian.org/experimental/texlive-luatex (2024.20240401-1), which will at some point then trickle into sid, and later testing. (this would be a texlive snapshot from 2024 sometime, as I assume - and thus with optex 1.14 - and newer minim)

Yes, this seems right. Though while with TeX Live 2024 (2024.20240401-1) you will get new OpTeX (https://git.texlive.info/texlive/tree/Master/texmf-dist/tex/optex/base/optex.ini?h=tags/texlive-2024.0 says 1.14), the minim-mp version with \everymp isn't in that initial TeX Live 2024 - that minim version was released on 2024-04-04, while TeX Live 2024 was 2024-03-14.

It would be nice, of course, to have these more recent packages - and I could install this one from experimental (with apt pinning) - and have done so for other packages in the past - or I could try to install optex by hand (...) - but usually these efforts are not worth it: just waiting some time is easier usually, and safer (in the worst case they could break my system in some way - it's called experimental for a reason).

You can try mmtex, my attempt to make a TeX distribution (i.e. an "alternative" to TeX Live) that is "minimal and modern" (i.e. omits most of the things, and includes only OpTeX as a minimal and modern TeX format).

It should be possible to install it on Debian through the Open build service, which contains prebuilt packages in the .deb format for various Debian versions - you can either install the .deb manually, or add an APT repository (and GPG key), and then you will have the possibility to get updates as well.

I just realesed an overdue version updating all the included packages (not much - mostly just OpTeX + dependencies and goodies like PGF/TikZ and minim) to the most recent versions.

The advantage of mmtex is, that it is small, self contained, and should work alongside any other TeX install you might have (since the binary is mmoptex, not optex, you can have both in $PATH). The disadvantage is, that it doesn't have any package manager, and using any non-provided CTAN package requires manual steps - if you need it, let me know and I'll try to put up instructions. Notably no fonts are included in mmtex, with the exception of the Latin Modern fonts, which will be installed thanks to dependency on fonts-lmodern in Debian. But with mmtex you should be able to use any system or installed fonts - so try apt install the fonts you need.

If you are interested, try it, and let me know if it works for you - I used to test new releases on Debian, but no longer do so regularly.

@vlasakm
Copy link
Owner

vlasakm commented May 12, 2024

Forgot to mention that I actually included the \everymp setup (mentioned above for getting OpTeX colors working with minim) in the latest release of mmtex, at least temporarily unil there is something in OpTeX itself.

@reuleaux
Copy link
Author

Thanks again for your kind support - and your mmtex package certainly sounds interesting (I and shall give it a try some time - and if not immediately, then because I am busy with other bits).

On a different - but related - matter: I have suggested to Petr some time ago to create an optex mailing list - and while he doesn't want to do it (he needs no help with optex, obviously), he did suggest that someone else may do it - and (having his consent thus - and having somehow taken my mouth full) I told him that I may set up it indeed, when I have some time. - Now I am looking around, considering options: I don't really want to pay (for an account with sourcehut e.g. - just to set up a mailing list i.e.) - nor do I necessarily want to set up a google group. - I don't really know, who would be willing to host this (tugboat https://tug.org/mailman/listinfo maybe?). - Maybe you have some idea? (And if so, you can reach me at my private email address: rx at a-rx dot info) Thanks, Andreas

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