2.20.2016

Getting palettes from color-hex.com

Quite simple function, useful if you want to try some palettes from color-hex.com but don’t want to manually type hex numbers.

library(rvest)
getPalette <- function(id){
    paste0("http://www.color-hex.com/color-palette/",id) %>%
    read_html() %>%
    html_nodes(xpath = "//div[@class = 'palettecontainer']/div/@title") %>%
    html_text()
}

For example, you want to get Google’s palette. It’s page on the site is color-hex.com/color-palette/1872, so id is 1872.

pal <- getPalette(1872)
pal
## [1] "#008744" "#0057e7" "#d62d20" "#ffa700" "#ffffff"

To display the palette, you can use the code below.

displayPalette <- function(pal){
    barplot(rep(1,length(pal)),col = pal,axes = F)
}
displayPalette(pal)

No comments:

Post a Comment