背景

背景

背景

希望参数化具有三个色标(A 到 B 到 C)和可变厚度的水平规则(用于页眉和页脚)。例如:

粗尺

细尺

单次停止代码

以下代码使用在文档页眉和页脚中绘制线条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

问题

我想知道:

  • 我在哪里可以找到原型定义linear_shade(即它是否支持多个颜色停止)?
  • 如何消除重复的 Overlays (HeaderSeparatorOverlay、FooterSeparatorOverlay)?

想法

我正在考虑拨打两个电话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}
    );

但可能有一种方法可以用更少的代码行或更简单的代码来编写它。

有关的

答案1

Metafun 手册第 8.3 节“阴影”,第 8.3.3 小节“新方法”。

withshadestep添加任意数量。

% 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

在此处输入图片描述

相关内容