| 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 |
| Repository: | CRAN |
| Date/Publication: | 2026-12-03 09:00:08 UTC |
monochromeR: Easily Create, View and Use Monochrome Colour Palettes
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.
Author(s)
Maintainer: Cara Thompson packages@cararthompson.com (ORCID)
Authors:
Cara Thompson packages@cararthompson.com (ORCID)
Other contributors:
The Karla Project Authors (Karla font, bundled in inst/fonts/karla under the SIL Open Font License 1.1) [copyright holder]
See Also
Useful links:
Report bugs at https://github.com/cararthompson/monochromeR/issues
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
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. |
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. |
... |
|
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.
|
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 |
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; |
view_palette |
Logical. |
view_labels |
Logical. If |
view_contrast |
Logical. If |
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 |
... |
US spellings of the arguments above: |
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. |
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:
|
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. |
... |
US spellings of the arguments above: |
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:
|
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. |
... |
US spellings of the arguments above: |
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 |
view_labels |
Logical. Whether or not the hex colour codes are shown on the palette displayed in the plot window. |
view_contrast |
Logical. If |
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 |
... |
|
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")
)