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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.