概述

概述

概述

希望创建随机的页面背景颜色,其中相同的背景颜色不会连续出现超过一次(即红色、绿色、蓝色、绿色、蓝色、红色等 - 永远不会是红色、红色、绿色、蓝色、蓝色)。

代码

显示问题的示例:

% Randomize the seed without having to delete the tuc file.
\ctxlua{math.randomseed( os.time() )}

\setupcolors[state=start]

% Dark green
\definecolor[ThemeColourGreen][h=8DC366]
% Pale blue
\definecolor[ThemeColourBlue][h=79C9EF]
% Yellowy-orange
\definecolor[ThemeColourYellow][h=FFD631]

\startMPinclusions
  vardef ThemeBase( expr base_colour ) =
    fill Page withcolor transparent(1, .85, base_colour );
  enddef;

  vardef ThemeStyle( expr base_colour ) = 
    draw unitcircle withpen pencircle scaled 1cm withcolor
      .5[base_colour,white];
  enddef
\stopMPinclusions

\startuseMPgraphic{page:ThemeBackground}
  color colours[];
  color base_colour;

  colours[1] := \MPcolor{ThemeColourGreen};
  colours[2] := \MPcolor{ThemeColourBlue};
  colours[3] := \MPcolor{ThemeColourYellow};

  base_colour := colours[ round( uniformdeviate( 2 ) + 1 ) ];

  ThemeBase( base_colour );
\stopuseMPgraphic

\startuseMPgraphic{page:ThemeStyle}
  ThemeStyle( base_colour );
\stopuseMPgraphic

\defineoverlay[page:ThemeBackground][\uniqueMPgraphic{page:ThemeBackground}]
\defineoverlay[page:ThemeStyle][\uniqueMPgraphic{page:ThemeStyle}]

\starttext
  \setupbackgrounds[page][background={page:ThemeBackground,page:ThemeStyle}]
  \startchapter[title=One]
  \input knuth
  \stopchapter

  \startchapter[title=Two]
  \input zapf
  \stopchapter

  \startchapter[title=Three]
  \input knuth
  \stopchapter

  \startchapter[title=Four]
  \input zapf
  \stopchapter
\stoptext

问题

有时所选的随机值与前一页生成的值相同,这导致连续两页具有相同的背景颜色。

请注意,在上面的代码中,圆圈与当前页面的背景颜色有关系。

问题

如何随机选择一个值,使得后续页面不再使用相同的值?

答案1

无环路解决方案:

% Randomize the seed without having to delete the tuc file.
%\ctxlua{math.randomseed( os.time() )}
\ctxlua{math.randomseed( 101)}

\setupcolors[state=start]

% Dark green
\definecolor[ThemeColourGreen][h=8DC366]
% Pale blue
\definecolor[ThemeColourBlue][h=79C9EF]
% Yellowy-orange
\definecolor[ThemeColourYellow][h=FFD631]

\startMPinclusions
  vardef ThemeBase( expr base_colour ) =
    fill Page withcolor transparent(1, .85, base_colour );
  enddef;

  vardef ThemeStyle( expr base_colour ) = 
    draw unitcircle withpen pencircle scaled 1cm withcolor
      .5[base_colour,white];
  enddef;

  color colours[];
  color base_colour;


  numeric id,idII;
  numeric indices[][];
  indices[1][1]=2;
  indices[1][2]=3;

  indices[2][1]=1;
  indices[2][2]=3;

  indices[3][1]=1;
  indices[3][2]=2;
  id := round( uniformdeviate( 2 ) + 1 );
\stopMPinclusions

\startuseMPgraphic{page:ThemeBackground}

  colours[1] := \MPcolor{ThemeColourGreen};
  colours[2] := \MPcolor{ThemeColourBlue};
  colours[3] := \MPcolor{ThemeColourYellow};

  idII:=indices[id][round( uniformdeviate( 1 ) + 1 )];
  base_colour := colours[ idII ];
  id:=idII;
  ThemeBase( base_colour );

\stopuseMPgraphic

\startuseMPgraphic{page:ThemeStyle}
  ThemeStyle( base_colour );
\stopuseMPgraphic

\defineoverlay[page:ThemeBackground][\uniqueMPgraphic{page:ThemeBackground}]
\defineoverlay[page:ThemeStyle][\uniqueMPgraphic{page:ThemeStyle}]

\starttext
  \setupbackgrounds[page][background={page:ThemeBackground,page:ThemeStyle}]
  \startchapter[title=One]
  \input knuth
  \stopchapter

  \startchapter[title=Two]
  \input zapf
  \stopchapter

  \startchapter[title=Three]
  \input knuth
  \stopchapter

  \startchapter[title=Four]
  \input zapf
  \stopchapter

  \startchapter[title=Five]
  \input knuth
  \stopchapter

  \startchapter[title=Six]
  \input zapf
  \stopchapter
\stoptext

编辑:这是更通用的无循环解决方案,并且不使用额外的数组: 编辑-2:添加了颜色数量的自动计数器。

% Randomize the seed without having to delete the tuc file.
%\ctxlua{math.randomseed( os.time() )}
\ctxlua{math.randomseed( 101)}

\setupcolors[state=start]

% Dark green
\definecolor[ThemeColourGreen][h=8DC366]
% Pale blue
\definecolor[ThemeColourBlue][h=79C9EF]
% Yellowy-orange
\definecolor[ThemeColourYellow][h=FFD631]
% Pink
\definecolor[ThemeColourPink][h=FF2764]
% Gray
\definecolor[ThemeColourGray][h=C3C3C3]

\startMPinclusions
  vardef ThemeBase( expr base_colour ) =
    fill Page withcolor transparent(1, .85, base_colour );
  enddef;

  vardef ThemeStyle( expr base_colour ) = 
    draw unitcircle withpen pencircle scaled 1cm withcolor
      .5[base_colour,white];
  enddef;

  color colours[];
  color base_colour;
  numeric n, id;

  colours[0] := \MPcolor{ThemeColourGreen};
  colours[1] := \MPcolor{ThemeColourBlue};
  colours[2] := \MPcolor{ThemeColourYellow};
  colours[3] := \MPcolor{ThemeColourPink};
  colours[4] := \MPcolor{ThemeColourGray};
  n:=0; forever : exitunless known(colours[n+1]); n:=n+1; endfor;   
  id:=0;
\stopMPinclusions

\startuseMPgraphic{page:ThemeBackground}

  id:=(id+1+(round( uniformdeviate(n-1) ))) mod (n+1);
  base_colour := colours[ id ];

  ThemeBase( base_colour );

\stopuseMPgraphic

\startuseMPgraphic{page:ThemeStyle}
  ThemeStyle( base_colour );
\stopuseMPgraphic

\defineoverlay[page:ThemeBackground][\uniqueMPgraphic{page:ThemeBackground}]
\defineoverlay[page:ThemeStyle][\uniqueMPgraphic{page:ThemeStyle}]

\starttext
  \setupbackgrounds[page][background={page:ThemeBackground,page:ThemeStyle}]
  \startchapter[title=One]
  \input knuth
  \stopchapter

  \startchapter[title=Two]
  \input zapf
  \stopchapter

  \startchapter[title=Three]
  \input knuth
  \stopchapter

  \startchapter[title=Four]
  \input zapf
  \stopchapter

  \startchapter[title=Five]
  \input knuth
  \stopchapter

  \startchapter[title=Six]
  \input zapf
  \stopchapter

  \startchapter[title=Seven]
  \input knuth
  \stopchapter

  \startchapter[title=Eight]
  \input zapf
  \stopchapter
\stoptext

相关内容