ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] Functional shading with MetaPost / MetaFun
@ 2025-01-09 21:44 Florent Michel
  2025-01-10  0:17 ` [NTG-context] " Hans Hagen
  0 siblings, 1 reply; 3+ messages in thread
From: Florent Michel @ 2025-01-09 21:44 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 3517 bytes --]

Hi,

I was trying to see if I could use MetaFun / MetaPost as my main tool for
generating figures. I really like its close integration with ConTeXt,
speed, and expressivity making it, even for a beginner like me, a  pleasure
to work with.

One difficulty I encountered is for generating 2D functional shadings. For
instance, I am not sure what is the best way to reproduce the following
pgfplots picture in MetaPost:

____________________________________________________________
\usemodule[pgfplots]
\pgfplotsset{width=10cm,compat=1.18}
\usepgfplotslibrary{patchplots}

\starttext

\startTEXpage
\starttikzpicture
\startaxis[
    hide axis=true,
    axis lines=none,hide axis,
    enlargelimits=false,
    scale only axis,
    clip bounding box=upper bound,
    clip=true,
]
    \addplot[
        patch,
        shader=interp,
        mesh/color input=explicit,
        data cs=polar,
    ]
    coordinates {
        (90,4) [color=red]
        (210,4) [color=green]
        (-30,4) [color=blue]
    };
\stopaxis
\stoptikzpicture
\stopTEXpage
\stoptext
____________________________________________________________

One idea is to draw a sequence of triangles, each with a linear gradient:
____________________________________________________________
\startMPpage
  def LinearIntTriangle(expr pa, pb, pc, cola, colb, colc, n_iter) =
    for i = n_iter step -1 until 1 :
      path p;
      x := i / n_iter;
      y := (i - 0.5) / n_iter;
      p := pa -- (pa + x * (pb - pa)) -- (pa + x * (pc - pa)) -- cycle;
      sh := define_linear_shade(pa + y * (pb - pa), pa + y * (pc - pa),
                                (1 - y) * cola + y * colb, (1 - y) * cola +
y * colc);
      fill p withshade sh;
    endfor
  enddef;

  color cola, colb, colc;
  cola := (0,1,0);
  colb := (1,0,0);
  colc := (0,0,1);

  LinearIntTriangle((0,0), (50,50*(3**0.5)), (100,0), cola, colb, colc, 30);
\stopMPpage
____________________________________________________________

or to use a slight variation (decomposing the triangle in three so that the
color is ‘right’) along each edge:
____________________________________________________________
\startMPpage
  def LinearIntTriangleB(expr pa, pb, pc, cola, colb, colc, n_iter) =
    color centre_color;
    centre_color := (cola + colb + colc) / 3;
    LinearIntTriangle((pa + pb + pc) / 3, pa, pb, centre_color, cola, colb,
n_iter);
    LinearIntTriangle((pa + pb + pc) / 3, pb, pc, centre_color, colb, colc,
n_iter);
    LinearIntTriangle((pa + pb + pc) / 3, pc, pa, centre_color, colc, cola,
n_iter);
  enddef;

  color cola, colb, colc;
  cola := (0,1,0);
  colb := (1,0,0);
  colc := (0,0,1);

  LinearIntTriangleB((0,0), (50,50*(3**0.5)), (100,0), cola, colb, colc,
10);
\stopMPpage
____________________________________________________________

This approach gives ‘good enough’ results for large values of n_iter.
However, the size of the generated pdf is somewhat larger than the pgfplots
version: for instance, the first version above generates a 5KiB pdf versus
3.8KiB for the pgfplots version, while being somewhat less accurate.
Increasing n_iter to 100 gives a result which is (to my not-very-good eyes)
very close to the pgfplots version but the pdf size increases to about
20KiB, and takes significantly longer to render using MuPDF.

I'd be grateful if someone could point me to a better way to generate such
shadings in MetaFun / MetaPost.

Cheers,
Florent

[-- Attachment #1.2: Type: text/html, Size: 3870 bytes --]

[-- Attachment #2: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-01-10  7:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-09 21:44 [NTG-context] Functional shading with MetaPost / MetaFun Florent Michel
2025-01-10  0:17 ` [NTG-context] " Hans Hagen
2025-01-10  7:17   ` Florent Michel

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).