Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created August 29, 2014 13:57
Show Gist options
  • Save hrbrmstr/52a1971773e1dbfb1959 to your computer and use it in GitHub Desktop.
Save hrbrmstr/52a1971773e1dbfb1959 to your computer and use it in GitHub Desktop.
Quick R function to get the Pantone "Color of the Day" color from https://www.pantone.com/pages/colorstrology/colorstrology.aspx
library(XML)
library(httr)
library(stringr)
pantone_cotd <- function(cotd_url = "https://www.pantone.com/pages/colorstrology/colorstrology.aspx") {
# have to fake a real user agent to get the color
agent <- "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.78.2 (KHTML, like Gecko) Version/7.0.6 Safari/537.78.2"
try(pantone <- GET(cotd_url, user_agent(agent)))
if (class(pantone) == "response") {
tmp <- content(pantone, as="parsed")
pantone.number <- str_extract(unlist(xpathApply(tmp, "//span[@class='numLogon']", xmlValue)), "[0-9\\-]+")
pantone.name <- unlist(xpathApply(tmp, "//span[@class='nameLogon']", xmlValue))
pantone.color <- str_match(as.character(getNodeSet(tmp, "//div[@id='ctl00_ctlDynamicControl7_plColorstrologyBackgroundPanel']/@style")), "background\\-color:(#[A-Za-z0-9]+)")[,2]
names(pantone.color) <- sprintf("%s %s", pantone.name, pantone.number)
return(pantone.color)
} else {
return(`random`=rgb(runif(1),runif(1),runif(1)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment