概述

概述

概述

希望将棋盘格的颜色随机化,使得相对于另一种颜色,有些方格略浅,有些方格略深。

问题

下面设置了基本颜色和较浅的版本:

\definecolor[BaseColour][h=66CEF1]
\definespotcolor[BaseColourLighter][BaseColour][a=1,t=.5]

MetaPost 中的以下代码失败:

lightColour := \MPcolor{BaseColourLighter};

代码

以下是代码:

\setupcolors[state=start]

\definecolor[BaseColour][h=66CEF1]
\definespotcolor[BaseColourLighter][BaseColour][a=1,t=.5]

\startuseMPgraphic{ThemeBase}
  fill Page withcolor blue;
\stopuseMPgraphic

\startuseMPgraphic{page:ThemeElement}
  tiles_x := 8;
  tiles_y := 13;
  dim_x := OverlayWidth / tiles_x;
  dim_y := dim_x;
  color baseColour;
  color lightColour;
  baseColour := \MPcolor{BaseColour};
  lightColour := \MPcolor{BaseColourLighter};

  path s;
  s := (0, 0) -- (dim_x, 0) -- (dim_x, dim_y) -- (0, dim_y) -- cycle;

  for x = 0 upto tiles_x:
    for y = 0 upto tiles_y:
      path p;
      p := s shifted( x * dim_x, y * dim_y );

      if (x mod 2) = (y mod 2):
        fill p withcolor baseColour;
        draw p withcolor baseColour; 
      else:
        def lightColour = transparent( 1, .55-.25uniformdeviate(1), baseColour ) enddef;

        fill p withcolor lightColour;
        draw p withcolor lightColour;
      fi
    endfor;
  endfor;

  setbounds currentpicture to
    boundingbox currentpicture shifted( -dim_x/2, dim_y*(tiles_x/tiles_y)/2 );
\stopuseMPgraphic

\defineoverlay[ThemeBase][\uniqueMPgraphic{ThemeBase}]
\defineoverlay[page:ThemeElement][\uniqueMPgraphic{page:ThemeElement}]

\starttext
  \setupbackgrounds[page][background={ThemeBase,page:ThemeElement}]
  \startchapter[title=One]
  \input knuth
  \stopchapter
\stoptext

错误

错误信息是:

Equation cannot be performed (color=numeric).
<to be read again> 
                   withprescript

透明度

以下使用透明度:

fill s shifted( x * dim_x, y * dim_y ) withcolor transparent(1,0.5,baseColour);
draw s shifted( x * dim_x, y * dim_y ) withcolor transparent(1,0.5,baseColour);

然而,我真正想要的是:

transparent( 1, .55-.25uniformdeviate(1), baseColour )

为了实现如下目标:

棋盘图案

或者像:

棋盘圆圈图案

或者实际上任何形状。因此设置初始背景颜色并使用 MetaPost。

问题

如何在 ConTeXt/MetaPost 中使用相对彩色的颜色?

有关的

答案1

一些注释和评论

1)您不需要 MetaPost 来设置页面颜色,您可以一起使用颜色和背景:

\setupbackgrounds
  [page]
  [background={color, page:ThemeElement},
   backgroundcolor=blue]

2)你可以缩短棋盘格的尺寸:

%% from
%% s := (0, 0) -- (dim_x, 0) -- (dim_x, dim_y) -- (0, dim_y) -- cycle;

%% to
s := unitsquare xyscaled (dim_x, dim_y);

fill3) 为什么后面要使用draw?请考虑使用filldraw

代码

以下是我的解决方案:

\definecolor
  [BaseColour]
  [h=66CEF1]

\startuseMPgraphic{page:ThemeElement}
  tiles_x := 8;
  tiles_y := 13;
  dim_x   := OverlayWidth / tiles_x;
  dim_y   := dim_x;

  path s; s := unitsquare xyscaled (dim_x, dim_y);

  for x = 0 upto tiles_x:
    for y = 0 upto tiles_y:
      filldraw s shifted( x * dim_x, y * dim_y )
        if (x mod 2) = (y mod 2):
          withcolor \MPcolor{BaseColour};
        else:
          withcolor transparent(1, uniformdeviate .5, \MPcolor{BaseColour});
        fi
    endfor;
  endfor;

  setbounds currentpicture to
    boundingbox currentpicture shifted( -dim_x/2, dim_y*(tiles_x/tiles_y)/2 );
\stopuseMPgraphic

\defineoverlay
  [page:ThemeElement]
  [\uniqueMPgraphic{page:ThemeElement}]

\setupbackgrounds
  [page]
  [background={page:ThemeElement}]

\starttext\null
\stoptext

截屏

如您所见,我没有定义任何 MetaPost 颜色,而是使用了 ConTeXt 中的定义\MPcolor

相关内容