# Overview

Looking to randomize the colours of squares of a checkerboard so that some are slightly lighter and some are slightly darker, relative to another colour.

# Problem

The following sets a base colour and a lighter version:

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


The following code in MetaPost fails:

lightColour := \MPcolor{BaseColourLighter};


# Code

Here is the code:

\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


# Error

The error message is:

Equation cannot be performed (color=numeric).
withprescript


# Transparency

The following uses transparency:

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);


However, I really want something like:

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


To achieve something like: Or like: Or with any shape, really. Hence setting an initial background colour and using MetaPost.

# Question

How do you use relatively coloured colours in ConTeXt/MetaPost?

# Related

1) You don't need MetaPost to set the page colour, you can use colours and backgrounds together:

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


2) You can shorten the the checkerboard squares:

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

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


3) Why are you using fill followed by draw? Consider using filldraw instead.

### The code

Here is my solution:

\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 As you can see, I did not define any MetaPost colours, instead I used the definitions from ConTeXt using \MPcolor.

• In general there's a rule: If you duplicate code, there's probably a better way to do it. And simply ignore the comments which are not applicable or irrelevant, I just wanted to show some other (and sometimes better) ways. Oct 5, 2013 at 19:03
• I agree completely, Marco. How do you lighten or darken a colour without using transparency (that was the question I meant to ask)? Oct 5, 2013 at 19:06
• Use withcolor uniformdeviate .5*\MPcolor{BaseColour} Oct 5, 2013 at 19:24