lualatex / luamplib

generic TeX package - including MetaPost code in LuaTeX documents
http://ctan.org/pkg/luamplib
17 stars 11 forks source link

Feature request: allow \mpcolor to define color #112

Closed chupinmaxime closed 4 months ago

chupinmaxime commented 1 year ago

\mpcolor only works with withcolor. Is that possible to allow it to define color? In my LuaLaTeX code, I wrote a function with xcolor to get the specs (\extractcolorspecs) and convert (\convertcolorspec) any LaTeX color to rgb metapost color codage.

chupinmaxime commented 7 months ago

up?

dohyunkim commented 7 months ago

You can make your own macro like this:

\documentclass{article}
\usepackage{luamplib,xcolor}
\def\mympcolor#1{%
  runscript" %
    tex.runtoks(function() %
      tex.sprint[[\string\extractcolorspecs{#1}\string\tmpa\string\tmpb %
      \string\convertcolorspec{\string\tmpa}{\string\tmpb}{rgb}\string\tmpc]] %
    end) %
    return string.format('(\csstring\%s)', token.get_macro'tmpc') %
  "
}
\begin{document}
\begin{mplibcode}
  color reddish;
  reddish := \mympcolor{red!50} ;
  beginfig(0);
  fill fullcircle scaled 20 withcolor reddish ;
  endfig;
\end{mplibcode}
\end{document}
chupinmaxime commented 7 months ago

Tank you, I’ve done something like that too, but I think that it would be nice to adapt the macro \mpcolor.

dohyunkim commented 5 months ago

It's not possible to adapt \mpcolor without redefining withcolor operator. Instead, v2.29 provides mplibtexcolor and mplibrgbtexcolor operators which can be used in color definitions (or other places that accept color expressions). I hope the latter one fits your request. If you feel the name is too long, you can declare let mpcolor = mplibrgbtexcolor; somewhere in the metapost code and use mpcolor thereafter.

chupinmaxime commented 4 months ago

That’s great thank you.