Blend multiple colors

blend_colors(
  x,
  preset = c("ryb", "none", "dichromat", "rgb", "ryb2"),
  h1 = NULL,
  h2 = NULL,
  do_plot = FALSE,
  lens = 0,
  c_weight = 0.2,
  c_floor = 12,
  ...
)

Arguments

x

character vector of R colors in hex format, or list of color vectors, where each vector will be independently blended.

preset

character value indicating the color wheel preset, passed to colorjam::h2hwOptions().

do_plot

logical indicating whether to depict the color blend operation using jamba::showColors().

lens

numeric value used to influence the color saturation after averaging color wheel angles.

c_weight

numeric value used to weight the average color chroma (saturation) using the mean chroma values of the input colors. When c_weight=0 the chroma uses the radius returned by the mean color wheel angle.

c_floor

numeric value indicating the C chroma HCL value below which a color is considered to be "grey" and unsaturated. When this happens, the hue contribution is set to 0.001 relative to other colors being blended. This correction is done because every color is assigned one H hue value in HCL, even when the C chroma (saturation) is zero, therefore these colors effectively have no H hue.

...

additional arguments are ignored.

Value

character vector with blended color; when input x

is a list the returned vector will have length length(x).

Details

This function is intended to blend two or more colors, by default using "paint mixing" style, similar to subtractive color mixing. It accomplishes this goal by using a red-yellow-blue color wheel (very similar to cyan-yellow-magenta), then determines the average color hue with appropriate loss of color saturation.

This function also utilized color transparency, applied internally as relative color weights, during the color mixing process.

This function blends multiple colors, including several useful features:

  • color wheel red-yellow-blue, subtractive color mixing

  • can blend more than two colors at once

  • accounts for transparency of individual colors

The basic design guide was to meet these expectations:

  • red + yellow = orange

  • blue + yellow = green

  • red + blue = purple

  • blue + red + yellow = some brown/gray substance

The input x can be a vector of colors, or a list. When x is a list then the unique vectors are blended, returning a vector with length length(x).

The default additive color mixing, with red-green-blue colors used in electronic monitors, does not meet these criteria. (In no logical paint mixing exercise would someone expect that mixing red and green would make yellow; or that blue and yellow would make grey.)

In general the function performs well, with some exceptions where the color hue angle is not well-normalized opposite its complementary color, and therefore does not make the expected "brownish/grey" output. Examples include blend_colors(c("yellow", "purple")) which is closer to blue + yellow = green, because purple is also composed of blue with some red. Indeed, the R color hue for purple is 283; the hue for blue is 266; the hue for red is 12 (372); which means purple is substantially closer to blue than red. A suitable workaround in this case is to use blend_colors(c("yellow", "deeppink4")).

Examples

blend_colors(c("red", "yellow"), do_plot=TRUE)

#> [1] "#FEA54BFF"

blend_colors(c("blue", "gold"), do_plot=TRUE)

#> [1] "#5593D9FF"

blend_colors(c("blue", "red3"), do_plot=TRUE)

#> [1] "#983166FF"

blend_colors(c("dodgerblue", "springgreen3"), do_plot=TRUE)

#> [1] "#2DA9F4FF"

blend_colors(c("green", "dodgerblue"), do_plot=TRUE)

#> [1] "#00BFFFFF"

blend_colors(c("red", "gold", "blue"), do_plot=TRUE)

#> [1] "#9B885FFF"

blend_colors(c("green4", "red"), do_plot=TRUE)

#> [1] "#B46A00FF"

blend_colors(c("deeppink4", "gold"), do_plot=TRUE)

#> [1] "#A6857EFF"

blend_colors(c("blue4", "darkorange1"), do_plot=TRUE)

#> [1] "#795762FF"