I’m looking for suggestions & tips on a sensible way to automatically select a font color, black or white in my case, for place names to be written on top of a colorized surface (I.e.made with grdimage). I’m using the color scale, roma, and the deeper reds and blues (at both ends of the color scales) are where white lettering would work best, and the lighter greens, yellows and powder blue hues (in the middle of the scale) is where black lettering would work best.
Given that the -T specification is known (used in makecpt), and given the location of the text, all that needs doing is to use grdtrack to sample the grid for the elevation (or Z parameter), then pass it through a simple decision tree that would choose to write each place name in a black or white font color
Does that seem to be a reasonable approach? Just need to study the Roma scale carefully to set how far into the scale it would need to be in order to switch from white to black, and from black back to white at the higher end of the color scale.
Interesting question - my first guess would likewise be to determine which Z values should correspond to white/black and then pipe the pstext string with its location values through grdtrack to then use awk to determine color depending.
In my case, it makes sense to first select place names (which are distributed globally) that fall inside the map boundaries. Then, send those coordinates through grdtrack to get elevations (Z-values), then run that list through an awk command which does the decision making before routing to pstext. Should be fast and compact!
I’ve been in similar situations and adopted a different approach – to use the “outline” facility in pstext which draws the outline of the characters. So do that with a white pen a few points wise, then repeat drawing the characters normally, an extract of the code:
That is a nice alternative, too, JJ. Thanks for the suggestion. Very professional appearance. Very nice how the black line continues through the inside of the lower lobe of the B.
I’ve used repeated calls to pstext to imitate a drop shadow.
In the present case, adapting your suggestion would work, but I’d prefer not to obscure things any more than I have to. Just want the text to show up better in light and dark regions of the map. White text looks great on dark backgrounds, but is too hard to discern, or nearly disappears, in light shades.
Would also apply a divergent-grayscale cpt to the text work?
Edit: sorry, I had a dynamic visualization in mind instead of a surface
Edit2 : isn’t there a way to « clip » the letter and apply a surface color on it? The doc says that it is possible to do so with the textbox at least… but maybe « clip » can go further ?
plus this
the first grdcontour call can be modified with -W1.5p,white so these black contours also get some sort of white outline and become more visible in the deeper areas.
Funny, tastes. That hurts my eyes and I remember the discussions I had with Paul to allow contour labels to follow the contours and not to draw on a white box, which is how it was at the beginning.
yeah white background maybe looks like too much contrast. But it does not have to be white. Some sort of light grey, light blue, whatever fits the selected color scale.
Playing around with the text-outline option and looking into the filled_font.sh test, I found that with a ~ (is it documented?) we can do it in a single pstext call. It needs a -F+f and the outline setting like in this example (where we can still drop the 48p,Helvetica-Bold, if we want to use defaults).
echo 7 7 48p,Helvetica-Bold,black=~3p,green blue | gmt text -R0/18/0/15 -Jx1c -B5g1 -BWSne -F+f+jBL -png lixo
There is some documentation here text — GMT 6.5.0 documentation : To draw outline fonts you append =pen to the font specification
no idea why ~ is needed, the below code produces visually very similar result without ~ with pen size reduced to 1p (with ~3p,green the outline looks approx 3x thicker):
echo 7 7 48p,Helvetica-Bold,black=1p,green blue | gmt text -R0/18/0/15 -Jx1c -B5g1 -BWSne -F+f+jBL -png lixo
UPD oh, the ~ seems drawing the black letters on top of the outline, thus adding outline only outside the black letters, while without ~ the outline is drawn on top of the text party (or completely) covering the black letters:
echo 7 7 48p,Helvetica,black=20p,green blue | gmt text -R0/18/0/15 -Jx1c -B5g1 -BWSne -F+f+jBL -png lixo