randomcolour / randomcolourr

An R package generating random colour examples across R functionality.
1 stars 0 forks source link

Add 'crayon' version of randomcolour #35

Open mattmalin opened 4 years ago

mattmalin commented 4 years ago

Want to have a randomcolour_crayon() and randomcolour_message which colours the messages themselves either with colour as string or provided message.

May want to make the standard output of randomcolour() be coloured, though could add a strong dependency, so likely want it to be optional.

mattmalin commented 4 years ago

Will want to make the colouring optional to add to randomcolour() as coloured if crayon present, so may use of conditional following e.g. https://stackoverflow.com/questions/48266553/fallback-and-optional-dependencies-in-r-packages-the-cran-way

mattmalin commented 4 years ago

In progress example to allow randomcolour() itself to work whether crayon present or not:

randomcolour_better <- function()
{
  hex_value <- as.character.hexmode(sample(2^24, 1) - 1, 1)

  hex_colour <- paste0(
    "#",
    paste0(rep("0", times = 6 - nchar(hex_value)), collapse = ""),
    toupper(hex_value))

  if (requireNamespace("crayon", quietly=TRUE)) {
    cat(crayon::make_style(hex_colour)(hex_colour))
    return(invisible(hex_colour))
  } else {
    return(hex_colour)
  }
}