Convert colors to contrasted gray level for black and white rendering

Description

This function converts color codes (given in hexadecimal format) to constrasted gray levels. This is useful to enhance readability when printing in black and white. The conversion from color to gray levels is performed using the luminosity method (0.21R + 0.72G + 0.07B), then levels are linearly scaled to [contrast.min;contrast.max].

Usage

1
2
color2blackwhite(x, increase.contrast = TRUE, contrast.min = 0.02,
  contrast.max = 0.98)

Arguments

x

a character, the vector of color codes given in hexadecimal format (ex "#21AD5C").

increase.contrast

a logical, if TRUE the scaling is performed.

contrast.min

the minimal gray value to use in the scaling (0 = white, 1 = black).

contrast.max

the maximal gray value to use in the scaling (0 = white, 1 = black).

Examples

1
2
3
4
5
6
7
mycols = c("#BA364E", "#32BAA1", "#12AA91")
color2blackwhite(mycols)

barplot(1:3, axes=FALSE, col=mycols)
barplot(1:3, axes=FALSE, col=color2blackwhite(mycols, increase.contrast = FALSE))
barplot(1:3, axes=FALSE, col=color2blackwhite(mycols))
barplot(1:3, axes=FALSE, col=color2blackwhite(mycols, contrast.min = 0, contrast.max = 1))

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.