Skip to contents

monochromeR (development version)

monochromeR 0.3.0

Breaking changes

  • check_colour_return_rgb() is deprecated and will be removed in a future version. It was designed for use inside the package; use grDevices::col2rgb() to get a colour’s red, green and blue values instead. It also now always returns a named numeric vector, c(r = , g = , b = ). Previously, colour names and hex codes returned a one-column matrix; use as.matrix() on the result if you need the old format.

  • hex_to_rgb() now returns numbers rather than text, so its output can be used directly in other functions, e.g. rgba_to_hex(c(hex_to_rgb("#9E0142"), 0.7)). A single colour gives a named vector, c(r = , g = , b = ), instead of the string "r = , g = , b = ". Several colours give a matrix with one row per colour and columns r, g and b, with rows named after the colours’ names.

  • generate_palette(modification = "go_both_ways") with an even n_colours now splits the starting colour, giving n_colours / 2 colours either side of it. Odd n_colours give the same palettes as before. Palettes never contain NAs, even for very light or dark starting colours.

  • Arguments are now checked up front, with helpful errors: n_colours must be a whole number of 1 or more; view_palette, view_labels and view_contrast must be TRUE or FALSE; alpha values must be between 0 and 1; and NA is no longer read as a colour (previously transparent white). Code that relied on invalid values happening to work will now error.

  • Misspelled or unknown argument names now give an error, with a suggestion for likely typos (e.g. n_colurs = 5 suggests n_colours). Previously, they were silently ignored. US spellings (color, n_colors, blend_color, color_rgba, background_color) still work, but supplying both spellings of the same argument (e.g. background_colour and background_color) is now an error rather than one silently taking priority.

New features

  • generate_palette() can blend from one colour to another by giving the second colour as the modification, e.g. generate_palette("red", "blue"). blend_colour and modification = "blend" still work. The “Because you supplied a blend_colour” message now only appears when blend_colour overrides a “go_darker”, “go_lighter” or “go_both_ways” modification.

  • New contrast_ratio() calculates the WCAG 2 contrast ratio between each colour in a palette and a comparison colour (white by default), e.g. contrast_ratio(generate_palette("steelblue", "go_darker", 5), "black").

  • view_palette() and generate_palette() gain view_contrast. When TRUE, each label shows the colour’s WCAG contrast ratio with its text colour, labelled “vs black” (black text, above) and “vs white” (white text, below). A “Contrast check” title explains what to aim for: over 7:1 for text, over 3:1 for shapes (otherwise, consider adding an outline). Ratios are rounded down, so accessibility thresholds are never overstated.

  • view_palette() and generate_palette() gain contrast_colours, to check the palette’s contrast against your own colours rather than black and white, e.g. view_palette(palette, contrast_colours = c("#2C3D4F", "#F5F0E6")). Give one or two colours: the labels are written in those colours and show the contrast ratio against each (e.g. ‘vs #2C3D4F’), or the colours’ names if the vector is named (e.g. c(text = , background = )). Using your plot’s background colour shows how well shapes in each colour would stand out against it.

  • Palettes are now displayed in Karla, which is bundled with the package (SIL Open Font License 1.1), on graphics devices that support it (ragg, including RStudio’s AGG backend, and svglite). Other devices, such as pdf(), use their default font. Set options(monochromeR.font = ) to choose.

Changes to defaults

Improvements

  • generate_palette() warns when the palette can’t go in the requested direction, e.g. going lighter from white, darker from black, or blending a colour with itself, as the palette’s colours would be identical.

  • Clearer errors when modification is missing, when modification = "blend" is used without a blend_colour, and when modification isn’t recognised (with a suggestion for likely typos, e.g. “go_lighte”).

  • Changing view_labels, view_contrast or contrast_colours from their defaults where they have no effect (e.g. view_labels = FALSE without view_palette = TRUE) now gives a short message. Giving the default value (e.g. view_labels = TRUE) never does, as it doesn’t change anything.

  • Out-of-range (including negative) RGB values now give a helpful error.

  • The error for unrecognised colour names is now shown whatever the language R is running in.

  • Errors, warnings and messages now use cli formatting and have classes, so they can be caught by type, e.g. monochromeR_error_colour_name or monochromeR_warning_no_room. Every condition also has a package-wide class (monochromeR_error, monochromeR_warning or monochromeR_message). Errors name the function you called, e.g. generate_palette().

Internal changes

  • The package has been refactored following tidyverse conventions, without changing the palettes it creates or how they’re displayed: generate_palette() and view_palette() are now built from small, single-purpose internal helpers, palette colours are calculated in a vectorised way, and the code is formatted with air and checked with lintr.

  • Added a test suite, including visual tests of view_palette() with vdiffr, and GitHub Actions for R CMD check and test coverage.

  • Requires R 4.1.0 or later.