Skip to content

Commit

Permalink
use httr2, fail gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
drmowinckels committed Sep 10, 2023
1 parent d0cf9e1 commit 6efbfb3
Show file tree
Hide file tree
Showing 15 changed files with 85 additions and 55 deletions.
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version: 0.1.4
Date: 2023-09-07 19:48:09 UTC
Date: 2023-09-07 19:55:56 UTC
SHA:
95f90928fdd0c60388ddfc326eb10be5c8149203
d0cf9e1372771e89b74a37c0ea69e75934b91d26
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ Imports:
ggplot2,
graphics,
grDevices,
httr2,
rvest
RoxygenNote: 7.2.3
URL: https://github.com/drmowinckels/colorhex
BugReports: https://github.com/drmowinckels/colorhex/issues
Suggests:
curl,
spelling,
scales
Language: en-US
13 changes: 13 additions & 0 deletions R/api.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
query_colorhex <- function(){
req <- httr2::request(colour_url())
req <- httr2::req_retry(req, backoff = ~ 10)
httr2::req_error(req, is_error = function(resp) FALSE)
}

colour_url <- function(full = TRUE){
url <- "www.color-hex.com"
if(!full)
return(url)
paste0("https://", url, "/")
}

58 changes: 32 additions & 26 deletions R/color.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
#' @export
#'
#' @examples
#' if(interactive()){
#' if(curl::has_internet()){
#' get_popular_colors()
#' }
get_popular_colors <- function(){
url <- paste0(colour_url(), "popular-colors.php")
resp <- rvest::read_html(url)
cols <- rvest::html_nodes(resp,
xpath = '//*[@class="colordva"]')
req <- httr2::request(colour_url())
req <- httr2::req_url_path_append(
req,
"popular-colors.php")

resp <- httr2::req_perform(req)
resp <- httr2::resp_body_html(resp)
cols <- rvest::html_nodes(
resp,
xpath = '//*[@class="colordva"]')
cols <- as.character(cols)
get_bkg_color(cols)
}
Expand All @@ -34,10 +40,6 @@ get_random_color <- function(){
maxColorValue = 255)
}

randcol <- function(){
sample(1:255, 1)
}

#' Get color information
#'
#' Get color information from www.color-hex.com
Expand All @@ -49,30 +51,34 @@ randcol <- function(){
#' @export
#'
#' @examples
#' if(interactive()){
#' if(curl::has_internet()){
#' get_color("#470f0f")
#' }
get_color <- function(hex){
hex <- fix_hex(hex)
stopifnot(is_hex(hex))

url <- paste0(colour_url(), "color/", gsub("#", "", hex))

resp <- rvest::read_html(url)
req <- query_colorhex()
req <- httr2::req_url_path_append(
req,
"color",
gsub("#", "", hex))

resp <- httr2::req_perform(req)
resp <- httr2::resp_body_html(resp)
tables <- rvest::html_nodes(resp, "table")

prim <- rvest::html_table(tables[1], fill = TRUE)[[1]]
prim <- as.data.frame(t(prim))
names(prim) <- as.character(unlist(prim[1,]))
row.names(prim) <- NULL
prim <- prim[-1,]

rows <- rvest::html_nodes(resp,
xpath = '//*[@class="colordvconline"]')
rows <- rvest::html_text(rows)
rows <- gsub(" \n", "", rows)
rows <- fix_hex(rows)

ret <- list(
hex = hex,
space = prim,
Expand All @@ -85,13 +91,13 @@ get_color <- function(hex){
related = rows[22:length(rows)],
palettes = get_pals(resp, "palettecontainerlist narrow")
)

if(length(tables) > 2){
ret$triadic = fix_hex(chartable(tables[3]))
ret$analogous = fix_hex(chartable(tables[4]))
ret$complementary = fix_hex(chartable(tables[5]))
}

colorhex(ret)
}

Expand All @@ -101,7 +107,7 @@ colorhex <- function(x){
"complementary", "analogous",
"triadic", "shades", "tints",
"related", "palettes"))

structure(
x,
class = "colorhex"
Expand Down Expand Up @@ -133,18 +139,18 @@ plot.colorhex <- function(x,
"analogous", "shades", "tints",
"related"),
labels = TRUE, ...){

type <- match.arg(type,
c("complementary", "triadic",
"analogous", "shades", "tints", "related"),
several.ok = TRUE)

x <- lapply(type, function(y) if(y != "hex") c(x$hex, x[[y]]) else x[[y]])
names(x) <- type

ncols <- length(type)
nrows <- max(sapply(x, length))+.5

oldpar <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(oldpar))
graphics::par(mar = c(0, 0, 0, 0))
Expand All @@ -153,7 +159,7 @@ plot.colorhex <- function(x,
type = "n", xlab = "", ylab = "",
axes = FALSE
)

for(i in 1:length(type)){
tmp <- x[[type[i]]]
graphics::text(1, i, type[i], cex = 1, pos = 2)
Expand All @@ -165,5 +171,5 @@ plot.colorhex <- function(x,
}
}
}

}
34 changes: 23 additions & 11 deletions R/palette.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#' @export
#'
#' @examples
#' if(interactive()){
#' if(curl::has_internet()){
#' get_latest_palettes()
#' }
get_latest_palettes <- function(){
url <- paste0(colour_url(), "color-palettes/")
resp <- rvest::read_html(url)
req <- query_colorhex()
req <- httr2::req_url_path_append(
req, "color-palettes/")
resp <- httr2::req_perform(req)
resp <- httr2::resp_body_html(resp)
get_pals(resp)
}

Expand All @@ -25,23 +28,32 @@ get_latest_palettes <- function(){
#' @export
#'
#' @examples
#' if(interactive()){
#' if(curl::has_internet()){
#' get_popular_palettes()
#' }
get_popular_palettes <- function(){
url <- paste0(colour_url(), "color-palettes/popular.php")
resp <- rvest::read_html(url)
req <- query_colorhex()
req <- httr2::req_url_path_append(
req,
"color-palettes",
"popular.php")
resp <- httr2::req_perform(req)
resp <- httr2::resp_body_html(resp)
get_pals(resp)
}

get_pal <- function(id){
url <- paste0(colour_url(), "color-palette/", id)
resp <- rvest::read_html(url)

req <- query_colorhex()
req <- httr2::req_url_path_append(
req,
"color-palette",
id)
resp <- httr2::req_perform(req)
resp <- httr2::resp_body_html(resp)

tables <- rvest::html_nodes(resp, "table")
tables <- rvest::html_table(tables[1], fill = TRUE)[[1]]


palettehex(
gsub(" Color Palette", "",
rvest::html_text(rvest::html_nodes(resp, "h1"))),
Expand All @@ -61,7 +73,7 @@ get_pal <- function(id){
#' @export
#'
#' @examples
#' if(interactive()){
#' if(curl::has_internet()){
#' get_palette(103107)
#'
#' # Lookup multiple palettes
Expand Down
2 changes: 1 addition & 1 deletion R/scale_colorhex.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#' @name scale-colorhex
#' @return a ggplot2-proto
#' @examples
#' if(interactive()){
#' if(curl::has_internet()){
#' library(ggplot2)
#'
#' x <- get_color("#008080")
Expand Down
2 changes: 1 addition & 1 deletion R/scale_palettehex.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#' @name scale-palettehex
#' @return ggplot2-proto
#' @examples
#' if(interactive()){
#' if(curl::has_internet()){
#' library(ggplot2)
#'
#' x <- get_popular_palettes()
Expand Down
11 changes: 4 additions & 7 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ is_hex <- function(x){
grepl("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$", x)
}

colour_url <- function(full = TRUE){
url <- "www.color-hex.com"
if(!full)
return(url)
paste0("https://", url, "/")
}


chartable <- function(table){
x <- rvest::html_table(table)[[1]]
Expand Down Expand Up @@ -60,3 +53,7 @@ nchar <- function(x){
j <- lapply(j, length)
unlist(j)
}

randcol <- function(){
sample(1:255, 1)
}
2 changes: 1 addition & 1 deletion man/get_color.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_latest_palettes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_palette.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_popular_colors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/get_popular_palettes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/scale-colorhex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/scale-palettehex.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6efbfb3

Please sign in to comment.