4

Background

Looking to parameterize a horizontal rule (for headers and footers) that has three colour stops (A to B to C) and a variable thickness. For example:

Thick rule

Thin rule

Single Stop Code

The following code draws lines in the document header and footer using linear_shade:

\setupcolors[pagecolormodel=auto]

% Colour Stop #1
\definecolor[BrandTertiaryColour][h=FF6A00]
% Colour Stop #2
\definecolor[BrandPrimaryColour][h=E61739]
% Colour Stop #3
\definecolor[BrandSecondaryColour][h=991F3D]

% Define the graphics for the header and footer separator line
\startMPinclusions
  % t is the line thickness (in points)
  def create_line( expr t ) =
    path pbegan;
    pbegan := envelope pensquare of ((0, 0) -- (OverlayWidth, 0)) yscaled t;

    linear_shade(
      pbegan, 0, \MPcolor{BrandTertiaryColour}, \MPcolor{BrandPrimaryColour}
    );
  enddef;
\stopMPinclusions

\startuseMPgraphic{HeaderSeparator}
  create_line( 2pt );
\stopuseMPgraphic

\startuseMPgraphic{FooterSeparator}
  create_line( 0.5pt );
\stopuseMPgraphic

\defineoverlay[HeaderSeparatorOverlay][
  \useMPgraphic{HeaderSeparator}
]

\defineoverlay[FooterSeparatorOverlay][
  \useMPgraphic{FooterSeparator}
]

% Draw the header and footer rules.
\setupbackgrounds[header][text][
  background=HeaderSeparatorOverlay,
]

\setupbackgrounds[footer][text][
  background=FooterSeparatorOverlay,
]

\starttext
  \input knuth
\stoptext

Questions

I am wondering:

  • Where do I find the prototype definition for linear_shade (i.e., does it support multiple colour stops)?
  • How can the duplicate Overlays (HeaderSeparatorOverlay, FooterSeparatorOverlay) be eliminated?

Ideas

I was thinking of two calls to linear_shade:

    path pbegan;
    path pended;
    pbegan := envelope pensquare of ((0, 0) -- ((OverlayWidth/2), 0)) yscaled t;
    pended := envelope pensquare of (((OverlayWidth/2), 0) -- (OverlayWidth, 0)) yscaled t;

    linear_shade(
      pbegan, 0, \MPcolor{BrandTertiaryColour}, \MPcolor{BrandPrimaryColour}
    );

    linear_shade(
      pended, 0, \MPcolor{BrandPrimaryColour}, \MPcolor{BrandSecondaryColour}
    );

But there's probably a way to write it with fewer lines of--or less brittle--code.

Related

Dave Jarvis
  • 11,809
  • 1
    [\kpsexpand '$TEXMFCONTEXT'`/metapost/context/base/mpiv/mp-mlib.mpiv`](https://github.com/contextgarden/context-mirror/blob/52c5102fab837626fe9bb359b3eda6066e1a968e/metapost/context/base/mpiv/mp-mlib.mpiv#L875-L884) – Henri Menke Jan 12 '18 at 23:20
  • So no formal Javadoc-like documentation for these functions, then, that describe the behaviour, inputs, and outputs. – Dave Jarvis Jan 13 '18 at 00:11
  • Unfortunately not. All documentation for the Metafun macros is in the Metafun manual, which can be quite incomplete at times. – Henri Menke Jan 13 '18 at 00:13

1 Answers1

6

See Metafun manual section 8.3 “Shading”, subsection 8.3.3 “The new method”.

Add as many withshadestep as you want.

% Colour Stop #1
\definecolor[BrandTertiaryColour][h=FF6A00]
% Colour Stop #2
\definecolor[BrandPrimaryColour][h=E61739]
% Colour Stop #3
\definecolor[BrandSecondaryColour][h=991F3D]

\startuseMPgraphic{Separator}
fill fullsquare xyscaled (OverlayWidth, OverlayLineWidth)
    withshademethod "linear"
    withshadevector (0,1)
    withshadestep (
        withshadefraction .5
        withshadecolors (\MPcolor{BrandTertiaryColour}, \MPcolor{BrandPrimaryColour})
    )
    withshadestep (
        withshadefraction 1
        withshadecolors (\MPcolor{BrandPrimaryColour}, \MPcolor{BrandSecondaryColour})
    )
\stopuseMPgraphic

\defineoverlay
  [SeparatorOverlay]
  [\useMPgraphic{Separator}]

% Draw the header and footer rules.
\setupbackgrounds
  [header][text]
  [background=SeparatorOverlay,
   rulethickness=2pt]

\setupbackgrounds
  [footer][text]
  [background=SeparatorOverlay,
   rulethickness=0.5pt]

\starttext
  \input knuth
\stoptext

enter image description here

Henri Menke
  • 109,596