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 oddn_coloursputs the starting colour in the middle of the palette; an evenn_colourssplits it, givingn_colours / 2lighter andn_colours / 2darker 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 asgenerate_palette("red", "blue"). If you also give a "go_darker", "go_lighter" or "go_both_ways"modification,blend_colourtakes 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 = TRUEdisplays the palette in the plot window.- view_labels
Logical. If
view_paletteis set to TRUE,view_labelsdetermines whether or not the hex colour codes are shown on the palette displayed in the plot window. Changingview_labels,view_contrastorcontrast_coloursfrom their defaults when they have no effect (e.g. withview_palette = FALSE) gives a message.- view_contrast
Logical. If
view_paletteandview_labelsare TRUE,view_contrast = TRUE(the default) adds each colour's contrast ratio with the label colours to the labels. Seeview_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"). Seeview_palette().- ...
US spellings of the arguments above:
color,n_colors,blend_colorandcontrast_colors. Other argument names give an error.
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"
