Applies foreground and background color to text

Description

The function wraps the text x in the appropriate xterm 256 escape sequences to add background and foreground color

Usage

1
style(x, fg = NULL, bg = NULL, check.xterm = TRUE)

Arguments

x

text to style

fg

foreground color. See details below

bg

background color. See details below

check.xterm

logical. Do we check that the terminal is xterm. If the check is performed and the TERM environment variable is not xterm the text will be returned as is. This is a precaution so that the package can be used (but not be very useful) on other terminal without displaying irrelevant escape sequences.

Details

The colors used for the bg or fg argument can be integers between 0 and 255, which are mapped to the xterm colors (see http://frexx.de/xterm-256-notes/) or strings.

In the case of strings, a mapping is made uwing either the named colors colors or colors in hex representation, such as the output of the rgb function. The closest color from the requested is used. Distance is evaluated in the RGB space using an euclidean distance. See dist

Value

The text x possibly wrapped in escape sequences

Author(s)

Romain Francois <francoisromain@free.fr>

References

http://frexx.de/xterm-256-notes/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
	## Not run: 
		text <- c( "liberté", "égalité", "fraternité" )
		bg   <- c("blue", "white", "red" )
		line <- paste( 
			rep( " ", length.out = sum( nchar( text ) ) + 2 ), collapse = "")
		{
				cat( style( line, bg = "gray95" ), "\n" )
				cat( style( text, bg = bg), "\n", sep = " " )
				cat( style( line, bg = "gray95" ), "\n" )
		}
		
		cat( style( "hex", bg = "#cccccc", fg = "#0000ff" ), 
			"and", 
			style( "rgb", bg = rgb( 1, 0, 0), fg = rgb(1,1,1) ), 
			"are also supported", 
			"\n" ) 
		
		if( require( pixmap ) ){
			 rlogo <- read.pnm( system.file( "rlogo.pnm", package= "xterm256") )
			 rlogo.rgb <- data.frame( 
			 	red = as.integer(rlogo@red * 255), 
				green =  as.integer(rlogo@green * 255), 
				blue =  as.integer(rlogo@blue * 255) )	
			 cl <- closest(rlogo.rgb)
			 rlogo.text <- style( rep( " ", length(cl)), bg = cl )
			 dim( rlogo.text ) <- c( 38, 50 )
			 cat( apply( rlogo, 1, paste, collapse = ""), sep ="\n" )
			 
		}
		
		
	
## End(Not run)