3.5

3.5 | 2 ratings Rate this file 20 Downloads (last 30 days) File Size: 1.73 KB File ID: #27383
image thumbnail

textborder - Higher contrast text using a 1-pixel-thick border

by

 

27 Apr 2010 (Updated )

Draws text on a figure with a 1-pixel-thick border.

| Watch this File

File Information
Description

This function draws text on a figure with a 1-pixel-thick border, which may be useful when simple text would be hard to read due to low contrast.

TEXTBORDER(X, Y, STRING)
Creates text on the current figure with a one-pixel border around it. The default colors are white text on a black border, which provides high contrast in most situations.

TEXTBORDER(X, Y, STRING, TEXT_COLOR, BORDER_COLOR)
Optional TEXT_COLOR and BORDER_COLOR specify the colors to be used.

Optional properties for the native TEXT function (such as 'FontSize') can be supplied after all the other parameters.
Since usually the units of the parent axes are not pixels, resizing it may subtly change the border of the text out of position. Either set the right size for the figure before calling TEXTBORDER, or always redraw the figure after resizing it.

---

The screenshot was created using the following code. (The image is part of the Matlab Image Processing Toolbox demos.)

imshow(imresize(imread('pears.png'), 0.5))
textborder(35, 215, 'Some text')
textborder(120, 100, 'Some text','w','k','FontSize',22)
textborder(270, 215, 'Some text',[0.7 0.8 1])

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
24 Sep 2014 Sim

Works really nice. There a bug when using graphics with transparency (when the figure's render property is set to 'opengl'). It might be the same bug as Colin suffered. I fixed it by forcing the white text with an offset of 1 in its z coordinate, while the black text remains on the z=0 plane.

line 44 of the April 2010 version:
set(h, 'Position', [pos(1:2), 1])

23 Oct 2013 Joao Henriques

Hi Colin, I tried your code on R2012a but can't reproduce the bug.

I also developed the code on R2008b and used it with "surface" objects, of which "pcolor" is one example, and had no problems as well.

It must be a MATLAB bug in the specific version you're using. Which one is it?

17 Oct 2013 Colin

If anyone else has this trouble, here's the reason: When the function converts back to data units for the position of the offset text, for some reason Matlab sets the z value to 17.3205 (in my bug example). It doesn't do that when the entered values don't change the x and y positions, such as for the centered white text... My work around is to add a couple lines in the offset for-loop setting the z back to 0 after the unit conversion, like this:

pos = get(h(k),'Position');
set(h(k),'Position',[pos(1:2),0])

16 Oct 2013 Colin

This function works on a regular figure, but not if you are adding annotation over a pcolor plot. For some reason the four border text objects plot over the white centered one.

I've unsuccessfully tried the following modifications to the function:

1) adding an extra white centered one plotted first so that the black ones should be sandwhiched.

2) using uistack to explicitly try and tell Matlab which one to put on top.

Reversing the colors does not help. Something about the offsetting over a pcolor plot makes matlab force that text to stay on top in the plotting stack.

So unfortunately it's no use in the most likely situation I would want to add contrast to text.

Example of the bug:

pcolor(peaks)
textborder(30,30,'hello world')

Updates
28 Apr 2010

Changed the example and screenshot to more clearly demonstrate usage.

Contact us