R/create_random_logo.R

Defines functions plot_logo create_random_logo

Documented in create_random_logo plot_logo

#' Generate random Package hex logo
#' @param path_to_save path to save image
#' @param devices image file extension, can be a
#' @inheritParams setup_package
#' @export
 <- function(path_to_save = NULL,
                               pkg_name = NULL,
                               project_dir = NULL,
                               devices = c("png", "svg")) {
  # Colors integrated from these palettes:
  # wesanderson::wes_palette("Zissou1", type = "discrete")
  # wesanderson::wes_palette("Cavalcanti1", type = "discrete")
  # wesanderson::wes_palette("GrandBudapest1", type = "discrete")
  # wesanderson::wes_palettes$Cavalcanti1
  palette_vector <- c(
    "#3B9AB2", "#78B7C5", "#EBCC2A", "#E1AF00", "#F21A00",
    "#D8B70A", "#02401B", "#A2A475", "#81A88D", "#972D15",
    "#F1BB7B", "#FD6467", "#5B1A18", "#D67236", "#000000"
  )
  sample_colors <- sample(palette_vector, size = 3, replace = FALSE)
  plot_obj <- (
    pkg_name = pkg_name, border_size = 1.2,
    fill = sample_colors[1], color = sample_colors[2],
    text_color = sample_colors[3]
  )
  if (is.null(pkg_name) && is.null(project_dir)) {
    return(plot_obj)
  } else {
    if (is.null(path_to_save)) {
      logo_dir <- fs::path(project_dir, pkg_name, "man", "figures")
    } else {
      logo_dir <- path_to_save
    }
    if (!fs::file_exists(logo_dir)) {
      fs::dir_create(logo_dir)
    }
    # save both svg and png by default
    for (device in devices) {
      usethis::ui_done("saving {device} file")
      filepath_to_save <- fs::path(logo_dir, "logo", ext = device)
      ggplot2::ggsave(
        filename = filepath_to_save, plot = plot_obj,
        width = 20, height = 23.34, units = "mm", device = device,
        dpi = 300, bg = "transparent"
      )
    }
    return(invisible(TRUE))
  }
}

#' Plot Hex Logo
#' @param border_size border thickness
#' @param fill fill color aesthetic
#' @param color color of border
#' @param text_color color of Package name text
#' @inheritParams setup_package
#' @importFrom rlang .data
#' @return a `ggplot2` object
 <- function(pkg_name = NULL, border_size = 1.2,
                      fill = "#1881C2", color = "#87B13F",
                      text_color = "#FFFFFF") {
  if (is.null(pkg_name)) {
    pkg_name <- ""
  }
  hex_df <- tibble::tibble(
    x = 1 + c(rep(-sqrt(3) / 2, 2), 0, rep(sqrt(3) / 2, 2), 0),
    y = 1 + c(0.5, -0.5, -1, -0.5, 0.5, 1)
  )
  hex_df <- dplyr::bind_rows(hex_df, dplyr::slice(hex_df, 1))
  hex_plot <- ggplot2::ggplot() +
    ggplot2::geom_polygon(
      mapping = ggplot2::aes(x = .data$x, y = .data$y),
      data = hex_df, size = border_size, fill = fill, color = color,
      inherit.aes = FALSE
    ) +
    ggplot2::annotate(
      "text",
      x = 1, y = 1.4, label = pkg_name, color = "#ffffff", size = 3.8
    ) +
    ggplot2::annotate(
      "text",
      x = 1, y = 1.4, label = pkg_name, color = text_color, size = 3.75
    ) +
    ggplot2::theme_void()
  return(hex_plot)
}
luciorq/luciolib documentation built on Dec. 18, 2020, 11:43 a.m.