Benutzer:Andreas Plank/Funktionen
R (Statistikprogramm)
Textfarben
textWithBGColor <- function(
text, # the text
type="rainbow", # types: "rainbow", "gray", "bw"
addWhiteSpace=TRUE,
... # pass along to other functions
){
if(missing(text)) # stop here with example
<span style="font-weight: bold;color: #000000;">stop</span>("Stop here 'text = \"some text\"' is missing. Usage:\ntextWithBGColor(text='My text.')\ntextWithBGColor(text='My text.', type=\"gray\", s=0.3)\n # see also Help pages for ?rainbow() and ?gray.colors() (-> for additionals: saturation etc.)")
<span style="color: #0000ff;">if</span>(addWhiteSpace){# addWhiteSpace if needed
text <- paste(" ", text, " ", sep="")
}
ntext <- nchar(text) # number of characters
ncolrange <- 1:ntext # 1, 2, …, ntext
<span style="color: #0000ff;">for</span>(icol in ncolrange){# run for all index-colors
cat(
<span style="font-weight: bold;color: #000000;">sprintf</span>(# compose HTML-tag <span>…</span>
'<span style="background-color:%1$7.7s;%3$s">%2$s</span>',
switch(type, # first argument %1
rainbow = rainbow(ntext, ...)[icol],
gray = gray.colors(ntext, ...)[icol],
grey = gray.colors(ntext, ...)[icol],
bw = if(icol <= ntext %/% 2 ) "#000000" else "#FFFFFF",
rainbow(ntext, ...)[icol] # default
),# end switch(type)
# second argument %2
sub(" ", " ", substring(text, icol, icol )),
# third argument %3
switch(type, bw = if(icol <= ntext %/% 2 ) " color:#FFFFFF;" else " color:#000000;", "")
),# end compose HTML-tag <span>…</span>
sep="" #no space here
)# end cat print properly to prompt
}# end for 1:ntext
cat("\n") # add a line break
} # end textWithBGColor()
textWithBGColor(text='Farbe?', s=0.7) Farbe?
textWithBGColor(text='Farbe?', s=0.7, start= 0.4, end = 0.7) Farbe?
textWithBGColor(text='Grau?', type="gray", s=0.3) Grau?
textWithBGColor(text='SW', type="bw") SW