Package {monochromeR}


Title: Easily Create, View and Use Monochrome Colour Palettes
Version: 0.3.0
Description: Generate a monochrome palette from a starting colour for a specified number of colours, going lighter, darker, both ways, or blending towards a second colour. The package can also be used to display colour palettes in the plot window, with or without hex codes, colour labels and contrast ratios, to check the palette's accessibility against the Web Content Accessibility Guidelines.
License: MIT + file LICENSE
Encoding: UTF-8
URL: https://cararthompson.github.io/monochromeR/, https://github.com/cararthompson/monochromeR
BugReports: https://github.com/cararthompson/monochromeR/issues
Maintainer: Cara Thompson <packages@cararthompson.com>
Depends: R (≥ 4.1.0)
Imports: cli (≥ 3.6.0), ggplot2, lifecycle (≥ 1.0.3), magrittr, rlang (≥ 1.1.0), systemfonts (≥ 1.0.0), utils
Suggests: covr, ragg, testthat (≥ 3.1.5), vdiffr, withr
Config/testthat/edition: 3
Config/roxygen2/version: 8.0.0
NeedsCompilation: no
Packaged: 2026-12-02 21:06:51 UTC; Cara
Author: Cara Thompson ORCID iD [aut, cre], The Karla Project Authors [cph] (Karla font, bundled in inst/fonts/karla under the SIL Open Font License 1.1)
Repository: CRAN
Date/Publication: 2026-12-03 09:00:08 UTC

monochromeR: Easily Create, View and Use Monochrome Colour Palettes

Description

logo

Generate a monochrome palette from a starting colour for a specified number of colours, going lighter, darker, both ways, or blending towards a second colour. The package can also be used to display colour palettes in the plot window, with or without hex codes, colour labels and contrast ratios, to check the palette's accessibility against the Web Content Accessibility Guidelines.

Author(s)

Maintainer: Cara Thompson packages@cararthompson.com (ORCID)

Authors:

Other contributors:

See Also

Useful links:


Pipe operator

Description

See magrittr::%>% for details.

Usage

lhs %>% rhs

Arguments

lhs

A value or the magrittr placeholder.

rhs

A function call using the magrittr semantics.

Value

The result of calling rhs(lhs).


Check a colour and convert it to RGB values

Description

[Deprecated]

check_colour_return_rgb() is deprecated as of monochromeR 0.3.0 and will be removed in a future version. It was designed for use inside the package. To get the red, green and blue values of a colour, use grDevices::col2rgb() instead.

Usage

check_colour_return_rgb(colour, colour_variable_name)

Arguments

colour

A recognised colour name (e.g. "white"), a hex colour code (e.g. "#ffffff") or a vector of length 3 (red value, green value, blue value), with all values between 0 and 255.

colour_variable_name

The name of the variable, for readability of error messages.

Value

A named numeric vector, c(r = , g = , b = ). An error if the colour can't be interpreted.

Before monochromeR 0.3.0, colour names and hex codes returned a one-column matrix. If you need that format, use as.matrix() on the result.

Examples

# Instead of check_colour_return_rgb("white", "my_colour"):
grDevices::col2rgb("white")

Calculate the contrast ratio between colours

Description

Calculates the WCAG 2 contrast ratio between each colour in a palette and a single comparison colour, for example to check whether text in that colour would be readable on each colour in the palette.

Usage

contrast_ratio(colour, against = "white", ...)

Arguments

colour

The colours to check. Either a vector of recognised colour names and/or hex colour codes (e.g. c("white", "#0F4B63")), such as the output of generate_palette(), or a single vector of length 3 (red value, green value, blue value, e.g. c(15, 75, 99)), with all values between 0 and 255.

against

The colour to compare against. Defaults to white. Must be a single recognised colour name (e.g. "black"), hex colour code (e.g. "#000000") or vector of length 3 (red value, green value, blue value), with all values between 0 and 255.

...

color, the US spelling of colour. Other argument names give an error.

Details

WCAG guidance recommends a contrast ratio of at least 4.5:1 for normal text (3:1 for large text) to meet level AA, and at least 7:1 (4.5:1 for large text) to meet level AAA. Values are returned unrounded; if you round them before comparing with these thresholds, round down.

Value

A numeric vector of contrast ratios, one per colour in colour, between 1 (no contrast) and 21 (black vs white). Names of colour are kept.

Examples

contrast_ratio("#767676")

contrast_ratio(
  c("unripe" = "#89973d", "ripe" = "#e8b92f", "overripe" = "#a45e41"),
  against = "black"
)

contrast_ratio(generate_palette("steelblue", "go_both_ways", n_colours = 5))

contrast_ratio(c(15, 75, 99), against = c(255, 255, 255))


Generate a monochrome palette

Description

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
)

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

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


Converts Hex codes values to RGB vectors

Description

Converts Hex codes values to RGB vectors

Usage

hex_to_rgb(x)

Arguments

x

A hex colour code or recognised colour name, or a vector of them

Value

For a single colour, a named numeric vector, c(r = , g = , b = ). For several colours, a matrix with one row per colour and columns r, g and b. The rows are named after the names of x, if it has any. Any alpha channel in the hex codes is dropped.

Examples

hex_to_rgb("purple")
hex_to_rgb("#fafafa")
hex_to_rgb(c("unripe" = "#89973d", "ripe" = "#e8b92f"))

# The output can be passed straight to other functions, e.g. to get a
# lighter version of a colour, as if it were 70% opaque on white
rgba_to_hex(c(hex_to_rgb("#9E0142"), 0.7))


Converts RGB values to hex colour code

Description

Converts RGB values to hex colour code

Usage

rgb_to_hex(x)

Arguments

x

Red, green and blue values between 0 and 255: either a vector of length 3 (e.g. c(15, 75, 99)) or the matrix returned by rgba_to_rgb().

Value

A corresponding hex colour code

Examples

temp_rgb_matrix <- rgba_to_rgb(c(52, 46, 39, 0.8))
rgb_to_hex(temp_rgb_matrix)

rgb_to_hex(c(15, 75, 99))


Converts RGBA to a hex colour code

Description

Converts RGBA to a hex colour code

Usage

rgba_to_hex(colour_rgba, background_colour = "#ffffff", ...)

Arguments

colour_rgba

A vector of length 4: ⁠c(red value, green value, blue value, alpha)⁠. All colour values must be between 0 and 255. Alpha must be between 0 and 1.

background_colour

Defaults to white. Users can specify a different colour to get the hex code for their original colour blended with a specified background colour. background_colour must either be 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), with all values between 0 and 255. The default value is white ("#ffffff").

...

US spellings of the arguments above: color_rgba and background_color. Other argument names give an error.

Value

Returns the corresponding hex colour code

Examples

rgba_to_hex(c(52, 46, 39, 0.8))

rgba_to_hex(c(52, 46, 39, 0.8), "blue")

rgba_to_hex(c(52, 46, 39, 0.8), "#032cfc")


Converts RGBA to RGB (helper function)

Description

Converts RGBA to RGB (helper function)

Usage

rgba_to_rgb(colour_rgba, background_colour = "#ffffff", ...)

Arguments

colour_rgba

A vector of length 4: ⁠c(red value, green value, blue value, alpha)⁠. All colour values must be between 0 and 255. Alpha must be between 0 and 1.

background_colour

Defaults to white. Users can specify a different colour to get the hex code for their original colour blended with a specified background colour. background_colour must either be 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), with all values between 0 and 255. The default value is white ("#ffffff").

...

US spellings of the arguments above: color_rgba and background_color. Other argument names give an error.

Value

A matrix of red, green and blue values

Examples

rgba_to_rgb(c(52, 46, 39, 0.8))

rgba_to_rgb(c(52, 46, 39, 0.8), "blue")

rgba_to_rgb(c(52, 46, 39, 0.8), "#032cfc")


Easy way to view the created palette

Description

Easy way to view the created palette

Usage

view_palette(
  monochrome_palette,
  view_labels = TRUE,
  view_contrast = TRUE,
  contrast_colours = c("black", "white"),
  ...
)

Arguments

monochrome_palette

Vector of hex colour codes and/or colour names, or a generate_palette() call.

view_labels

Logical. Whether or not the hex colour codes are shown on the palette displayed in the plot window.

view_contrast

Logical. If view_labels is TRUE, view_contrast = TRUE (the default) adds the WCAG contrast ratio to each label: each label shows the colour's contrast ratio with the colour the label is written in (e.g. "vs black"). A title explains what to aim for: a contrast over 7:1 for text and over 3:1 for shapes. See contrast_ratio(). Ratios are rounded down to one decimal place, so they never overstate the contrast (e.g. 4.49:1 is shown as 4.4:1, not 4.5:1). Changing view_contrast or contrast_colours from their defaults with view_labels = FALSE gives a message, as they have no effect.

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"). The first colour's labels are shown above the middle of each colour, and the second's below. Labels say "vs" and the colour as you gave it (e.g. "vs #2C3D4F"), or its name if you name the vector, e.g. c(text = "#2C3D4F", background = "#F5F0E6"). As contrast works both ways, a label written in your plot's background colour shows how well shapes in each colour would stand out against that background.

...

contrast_colors, the US spelling of contrast_colours. Other argument names give an error.

Value

A plot showing all the colours in the palette on the same row

Font

Text is shown in Karla, which is bundled with the package, when the plot is created for a graphics device that supports it: ragg devices (including RStudio's AGG backend) and svglite. On other devices, such as pdf(), the device's default font is used. To choose the font yourself, set options(monochromeR.font = "Karla"), or options(monochromeR.font = "") for the device default. If you save a Karla plot as a PDF, use ggplot2::ggsave(..., device = grDevices::cairo_pdf).

Examples

view_palette(c("#464E69", "#8C90A1", "#D1D2D9"))
view_palette(
  c("unripe" = "#89973d", "ripe" = "#e8b92f", "overripe" = "#a45e41")
)
view_palette(generate_palette("pink", "go_darker", n_colours = 3))
view_palette(c("#464E69", "#8C90A1", "#D1D2D9"), view_contrast = FALSE)
view_palette(
  c("#464E69", "#8C90A1", "#D1D2D9"),
  contrast_colours = c(text = "#2C3D4F", background = "#F5F0E6")
)