FlyingTeller
FlyingTeller

Reputation: 20590

PS easy way to color text

I have a postscript file that contains a phylogenetic tree outputted by njplot. It basically consists of lines and labels at the end of the line. Right now it is in black and white but I would like to mark differences between different trees:

Below is a short extract from one of my files with only three of the labels.

a) What do I have to do to make e.g. "B. ovis 25840" be displayed in red?

b) How can I make a box around "B. suis 23445" and "B. Thomsen" (such as to mark that they are in the same group?)

/setpacking where {true setpacking} if
1 setlinecap 1 setlinejoin 1 setlinewidth 0 setgray
/basefont /Times-Roman findfont 12 scalefont def
/titlefont /Times-Roman findfont 12 scalefont def
/setclip {40 40 moveto 560 40 lineto 560 810 lineto 40 810 lineto closepath clip newpath} def
/title {titlefont setfont
40 815 moveto (brucella_conc_se_ani.out_nj.outtree   Mon Aug 14 14:52:28 2017
) show (  Page ) show show ( of 1) show
} def
%%EndProlog
%%Page: ? 1
(1) title setclip
0 0 translate
basefont setfont
50 50 translate
0.7 setgray -10 -10 moveto 510 -10 lineto 510 760 lineto -10 760 lineto closepath stroke 0 setgray 
359 8 moveto
(B. ovis 25840) show
298 67 moveto
(B. Thomsen) show
294 127 moveto
(B. suis 23445) show
showpage

Upvotes: 2

Views: 1191

Answers (1)

KenS
KenS

Reputation: 31199

Text is drawn (for text in fonts other than type 3) using the current color. So if you alter the current color before drawing the text, then it will be drawn in a different color.

There are a number of color operators in PostScript, the simplest are the setgray, setrgbcolor and setcmykcolor operators. setgray takes a value between 1 (white) and 0 (black) and sets the current color to that percentage of gray. setrgbcolor takes 3 parameters between 01 and 1 for each of R, G and B, and setcmykcolor does the same but with CMYK components.

So to answer question 'a' 1 0 0 setrgbcolor placed before the line with the text on it, would cause the text to be drawn in red.

Note that all subsequent operations would also be rendered in red, so we should consider now the gsave and grestore operators. gsave saves a copy of the graphics state, and grestore (surprise!) restores the graphics state from the most recently saved version.

You can use this to limit the effect of a graphics state change. For example:

gsave
1 0 0 setrgbcolor
(B. ovis 25840) show
grestore

would render the text in red, but the subsequent text would be in the colour in force before the gsave, presumably black.

Note that the current point on the page is part of the gstate! So :

gsave
1 0 0 setrgbcolor
359 8 moveto
(B. ovis 25840) show
298 67 moveto
grestore
(B. Thomsen) show

would cause the second 'show' to take place at the same position as the first one, overwriting it.

You don't 'draw a box', PostScript doesn't have graphics primitives like that. What you do is construct a path, and then stroke it (you could fill it instead for a filled rectangle).

For example:

0 0 moveto
0 100 lineto
100 100 lineto
100 0 liento
closepath
stroke

will construct a rectangular path with its bottom left corner at 0,0 it will be 100 units wide and tall (so a square). The 'stroke' operator then strokes the path using the current linewidth and the current colour.

If you want to dig deeper into PostScript then you will need a copy of the PostScript Language Reference Manual, which is available online in PDF format from the Adobe web site.

You might also like to look at the the 'Blue Book' which is also available there as the 'PostScript language tutorial and cookbook'

Upvotes: 4

Related Questions