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; usegrDevices::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; useas.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 columnsr,gandb, with rows named after the colours’ names.generate_palette(modification = "go_both_ways")with an evenn_coloursnow splits the starting colour, givingn_colours / 2colours either side of it. Oddn_coloursgive the same palettes as before. Palettes never containNAs, even for very light or dark starting colours.Arguments are now checked up front, with helpful errors:
n_coloursmust be a whole number of 1 or more;view_palette,view_labelsandview_contrastmust beTRUEorFALSE; alpha values must be between 0 and 1; andNAis 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 = 5suggestsn_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_colourandbackground_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 themodification, e.g.generate_palette("red", "blue").blend_colourandmodification = "blend"still work. The “Because you supplied a blend_colour” message now only appears whenblend_colouroverrides 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()andgenerate_palette()gainview_contrast. WhenTRUE, 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()andgenerate_palette()gaincontrast_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. Setoptions(monochromeR.font = )to choose.
Changes to defaults
generate_palette()now gives 3 colours by default (n_colours = 3).view_palette()andgenerate_palette()now show contrast ratios by default (view_contrast = TRUE). Useview_contrast = FALSEfor the previous labels.
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
modificationis missing, whenmodification = "blend"is used without ablend_colour, and whenmodificationisn’t recognised (with a suggestion for likely typos, e.g. “go_lighte”).Changing
view_labels,view_contrastorcontrast_coloursfrom their defaults where they have no effect (e.g.view_labels = FALSEwithoutview_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_nameormonochromeR_warning_no_room. Every condition also has a package-wide class (monochromeR_error,monochromeR_warningormonochromeR_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()andview_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.
