ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Florent Michel <florent.m42@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: [NTG-context] Functional shading with MetaPost / MetaFun
Date: Thu, 9 Jan 2025 21:44:14 +0000	[thread overview]
Message-ID: <CALOhoe+573GiRWTpC=iLC2Se_cDidfRaz4jvAtudxvr-VTrwzg@mail.gmail.com> (raw)


[-- 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
___________________________________________________________________________________

             reply	other threads:[~2025-01-09 21:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-09 21:44 Florent Michel [this message]
2025-01-10  0:17 ` [NTG-context] " Hans Hagen
2025-01-10  7:17   ` Florent Michel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CALOhoe+573GiRWTpC=iLC2Se_cDidfRaz4jvAtudxvr-VTrwzg@mail.gmail.com' \
    --to=florent.m42@gmail.com \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).