Skip to contents

Make color contrast with background colors

Usage

make_color_contrast(
  x,
  y = NULL,
  bg = "white",
  L_threshold = 65,
  C_floor = 90,
  L_hi = 95,
  L_lo = 40,
  blend_preset = "ryb",
  do_plot = FALSE,
  cex = 2,
  ...
)

Arguments

x

character vector of text colors

y

character vector of fill colors for which the colors in x should be adjusted for visual contrast.

bg

character background color, default 'white', used when colors in y contain transparency.

L_threshold

numeric threshold Luminance (L) value which defines the point where the background color should have light or dark text for optimum visual contrast. The default 65 is well-discussed in data visualization forums, however may not be ideal for all viewing conditions.

C_floor

numeric to define the minimum Chroma (C) when the adjusted color Chroma is below an internal threshold 20.

L_hi, L_lo

numeric value to define the Luminance (L) used for 'hi' (bright) output colors, or 'lo' (dark) output colors. The default 'L_hi=95' is fairly high, and 'L_lo=40' is moderately dark. In future the recommended default may become 'L_hi=85' to improve the resulting color hue.

blend_preset

character string passed as preset to colorjam::blend_colors() to define the color wheel used during color blending operations. Default 'ryb' uses the red-yellow-blue color wheel. This adjustment only affects layering 'y' together with 'bg'.

do_plot

logical whether to create a visual plot illustrating the input and output colors, on top of the background.

cex

numeric text size adjustment used when do_plot=TRUE.

...

additional arguments are passed to internal functions, including colorjam::blend_colors().

Details

This function provides a simple method to ensure a color has adequate visual contrast with a background color, while retaining some color saturation. For example, red on red background will return something close to pink, so the pink retains the red color saturation but is visually distinct from the background color red.

Similarly, pink on pink will return something close to red.

See also

Examples

x <- c("firebrick", "dodgerblue", "gold", "pink", "white");
y <- rep("red3", 4);
make_color_contrast(x, y, do_plot=TRUE);

#> [1] "#FFCDCD" "#BDF1FF" "#FFEE64" "#FFC8E4" "#FFFFFF"

y <- rep("pink1", 4);
make_color_contrast(x, y, do_plot=TRUE);

#> [1] "#A73232" "#005FBF" "#7B5C00" "#AF2851" "#5E5E5E"

y <- rep("gold", 4);
make_color_contrast(x, y, do_plot=TRUE);

#> [1] "#A73232" "#005FBF" "#7B5C00" "#AF2851" "#5E5E5E"

y <- c("red4", "aquamarine4", "blue3", "yellow", "pink2")
make_color_contrast(x, y, do_plot=TRUE, C_floor=140);

#> [1] "#FFB6B6" "#87F2FF" "#FFED00" "#CC0048" "#5E5E5E"