Skip to content

Instantly share code, notes, and snippets.

@andrewmoles2
Last active April 7, 2025 10:33
Show Gist options
  • Save andrewmoles2/ffa7f0bae82f9df88c6c1d9458d980a8 to your computer and use it in GitHub Desktop.
Save andrewmoles2/ffa7f0bae82f9df88c6c1d9458d980a8 to your computer and use it in GitHub Desktop.
Coolors function in R
# Function for generating coolors hex codes
coolors <- function(URL) {
#' Coolors hex generator
#'
#' Function for taking coolors URL that contains hex codes, and extracting that information so we can use the hex codes
#' Currently works for palettes in: palette generator, explore palettes, and gradients
#' @param URL a URL from https://coolors.co/ that contains hex codes, e.g. https://coolors.co/ddfcad-c8e087
URL <- sub(".*/", "", URL)
URL <- gsub("*-", ",", URL)
URL <- strsplit(URL, ",")
URL <- paste0("#", URL[[1]])
return(URL)
}
# test
#library(tidyverse)
#url <- "https://coolors.co/00a878-6ccd8c-d8f1a0-e6d98c-f3c178-f9905d-fe5e41-853221-0b0500"
#url2 <- "https://coolors.co/palette/f4f1de-e07a5f-3d405b-81b29a-f2cc8f"
#url3 <- "https://coolors.co/gradient/00ff87-60efff"
#url4 <- "https://coolors.co/gradient-maker/439cfb-f187fb?position=7,100&opacity=100,100&type=linear&rotation=90"
#url5 <- "https://coolors.co/gradient-maker/00ff87-60efff"
#coolors(url)
#coolors(url2)
#coolors(url3)
#coolors(url4)
#coolors(url5)
#scales::show_col(coolors(url))
#scales::show_col(coolors(url2))
#scales::show_col(coolors(url3))
#scales::show_col(coolors(url4))
#scales::show_col(coolors(url5))
#mtcars %>%
# ggplot2::ggplot(ggplot2::aes(x = mpg, y = hp, colour = factor(carb))) +
# ggplot2::geom_point() +
# ggplot2::scale_colour_manual(values = coolors(url))
#mtcars %>%
# ggplot2::ggplot(ggplot2::aes(x = mpg, y = hp, colour = factor(cyl))) +
# ggplot2::geom_point() +
# ggplot2::scale_colour_manual(values = coolors(url2)) +
# ggplot2::theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment