Color in MetaPost

From ConTeXt wiki
Jump to: navigation, search

MetaPost only supports the RGB color space, although with the help of MetaFun you can face transparency and CMYK colors. This article describes the use of colors in MetaPost and MetaFun.


The basics

Colors are defined as vectors with three components: a red, green and blue one. Like pens, colors have a with... command:

withcolor (.4,.5.,6)

You can define color variables like so:

color darkred ; darkred := (.625,0.0) ;
% use the color
withcolor darkred
% actually, red is already defined, so this works:
withcolor .625red

Because for MetaPost colors are just vectors, you can manipulate them like you would manipulate points. This is a color halfway between red and green:

withcolor .5[red,green]

Using colors defined in TeX

In ConTeXt you define colors with \definecolor. Instead of defining a color a second time in MetaPost, you can use the \MPcolor{colorname} command.

  \definecolor[darkred] [r=.625] % a RGB color
  \definecolor[darkgray][s=.625] % a gray scale
  \startuseMPgraphic{color demo}
    pickup pencircle scaled 1mm ;
    path p ; p := fullcircle xscaled 5cm yscaled 0.5cm ;
    fill p withcolor \MPcolor{darkgray} ;
    draw p withcolor \MPcolor{darkred} ;
  \bold{\color[darkgray]{This} is an \color[darkred]{ellipse}:}
  \useMPgraphic{color demo}

Transparent colors

You can use transparent colors with the transparent(method, factor, color) function. For example:

fill unitsquare scaled 1cm withcolor yellow;
fill unitsquare shifted (0.5,0.5)
  % method 1 = normal
  scaled 1cm withcolor transparent(1,0.5,red);


The available transparency methods are the twelve methods supported by PDF specification:

Troubleshooting transparency

If you see black colors rather than transparent ones, then you probably hit the 'missing specials' problem. This problem was around in 2006; people may not encounter it anymore, but the solution is mentioned here anyway. From the mailing list:

This all sounds like the 'missing specials' problem that is caused
by conflicting -progname= arguments when using the web2c version
of MetaPost.

Make sure you do not have conflicting memory settings for both
as well as

The best is to remove all trace of '.mpost' and '.metafun' memory
settings from your texmf.cnf, but at least make sure all the
'.mpost' and 'metafun' values are the same .

Then regenerate metafun using texexec --make, and all should be
well again.

The settings will be in tex/texmf/web2c/texmf.cnf or tex/texmf-context/web2c/context.cnf inside your ConTeXt tree. If you are using ConTeXt from the Debian/Ubuntu distribution of TeX, instead of via the recommended ConTeXt Standalone, you need to edit /etc/texmf/texmf.d/95NonPath.cnf, instead. The part you need to edit will look something like this.

main_memory = 1000000 % words of inimemory available; also applies to inimf&mp
main_memory.context = 1500000
main_memory.mpost = 1500000

See also


Personal tools
External Help