Skip to contents

This function allows users generate a monochrome colour palette containing any number of colours, starting from the colour they specify. The modification parameter can be set to make the palette go darker, lighter, or both ways from the starting colour. The function also allows users to create a palette that goes from one colour to another, by giving a colour as the modification (e.g. generate_palette("red", "blue")).

Usage

generate_palette(
  colour,
  modification,
  n_colours = 3,
  blend_colour = NULL,
  view_palette = FALSE,
  view_labels = TRUE,
  view_contrast = TRUE,
  contrast_colours = c("black", "white"),
  ...
)

Arguments

colour

The starting colour for the palette, which must be either a recognised colour name (e.g. "white"), a hex colour code (e.g. "#ffffff") or vector of length 3 (red value, green value, blue value, e.g. c(15, 75, 99)), with all values between 0 and 255.

modification

Which way to take the palette: "go_darker", "go_lighter", "go_both_ways", or a colour to blend towards (a recognised colour name, hex colour code or vector of length 3, e.g. "blue"). "blend" also works, together with a blend_colour. With "go_both_ways", an odd n_colours puts the starting colour in the middle of the palette; an even n_colours splits it, giving n_colours / 2 lighter and n_colours / 2 darker colours either side of it.

n_colours

Number of colours (levels) required in the palette: a whole number of 1 or more. Defaults to 3. A warning is given if the palette can't go in the requested direction (e.g. going lighter from white, or darker from black), as its colours would be identical.

blend_colour

Optional. Another way to give the colour to blend towards; generate_palette("red", blend_colour = "blue") is the same as generate_palette("red", "blue"). If you also give a "go_darker", "go_lighter" or "go_both_ways" modification, blend_colour takes priority, with a message. Can be either a recognised colour name (e.g. "white"), a hex colour code (e.g. "#ffffff") or vector of length 3 (red value, green value, blue value, e.g. c(15, 75, 99)), with all values between 0 and 255.

view_palette

Logical. view_palette = TRUE displays the palette in the plot window.

view_labels

Logical. If view_palette is set to TRUE, view_labels determines whether or not the hex colour codes are shown on the palette displayed in the plot window. Changing view_labels, view_contrast or contrast_colours from their defaults when they have no effect (e.g. with view_palette = FALSE) gives a message.

view_contrast

Logical. If view_palette and view_labels are TRUE, view_contrast = TRUE (the default) adds each colour's contrast ratio with the label colours to the labels. See view_palette().

contrast_colours

The colours the labels are written in, and that the contrast ratios are calculated against: one or two recognised colour names and/or hex colour codes. Defaults to c("black", "white"). See view_palette().

...

US spellings of the arguments above: color, n_colors, blend_color and contrast_colors. Other argument names give an error.

Value

A vector of hex colour codes making up the generated palette

Examples

generate_palette("red",
  modification = "go_lighter",
  n_colours = 5, view_palette = TRUE, view_labels = TRUE
)

#> [1] "#FF0000" "#FF3232" "#FF6666" "#FF9999" "#FFCCCC"

generate_palette(c(15, 75, 99),
  modification = "go_both_ways",
  n_colours = 12, view_palette = TRUE, view_labels = FALSE
)

#>  [1] "#CFDBDF" "#ACC0C9" "#89A6B2" "#668C9B" "#437285" "#20586E" "#0D455B"
#>  [8] "#0B3A4D" "#092F3E" "#072430" "#051922" "#030F13"

generate_palette("red", "blue", n_colours = 6, view_palette = TRUE)

#> [1] "#FF0000" "#D60028" "#AD0051" "#84007A" "#5B00A3" "#3300CC"