TextContrastColor
returns either black or white depending on which has the better contrast.TextContrastColor(col, method = c("glynn", "sonego"))
"glynn"
or "sonego"
. See details.L <- c(0.2, 0.6, 0) %*% col2rgb(color)/255
and returns #000060 if L >= 0.2 and #FFFFA0 else.# works fine for grays
PlotArea( y=matrix(rep(1, times=3, each=8), ncol=8), x=1:3,
col=gray(1:8 / 8), ylab="", xlab="", axes=FALSE )
text( x=2, y=1:8-0.5, levels(d.pizza$driver),
col=TextContrastColor(gray(1:8 / 8)))
# and not so fine, but still ok, for colors
par(mfrow=c(1,2))
PlotArea( y=matrix(rep(1, times=3, each=12), ncol=12), x=1:3,
col=rainbow(12), ylab="", xlab="", axes=FALSE, main="method = Glynn" )
text( x=2, y=1:12-0.5, levels(d.pizza$driver),
col=TextContrastColor(rainbow(12)))
PlotArea( y=matrix(rep(1, times=3, each=12), ncol=12), x=1:3,
col=rainbow(12), ylab="", xlab="", axes=FALSE, main="method = Sonego" )
text( x=2, y=1:12-0.5, levels(d.pizza$driver),
col=TextContrastColor(rainbow(12), method="sonego"))
Run the code above in your browser using DataLab