Skip to content

Instantly share code, notes, and snippets.

@chichacha
Last active December 27, 2018 07:27
Show Gist options
  • Save chichacha/e5c2baca147be46285b474092fd69cd6 to your computer and use it in GitHub Desktop.
Save chichacha/e5c2baca147be46285b474092fd69cd6 to your computer and use it in GitHub Desktop.
Colour Bars with geom_tile
library(tidyverse)
library(imager)
library(TSP)
im <- load.image("https://static.highsnobiety.com/wp-content/uploads/2018/12/06095023/pantone-living-coral-color-2019-01-480x320.jpg")
im.df <- im %>%
imager::RGBtoHSV() %>%
as.data.frame(wide="c") %>%
rename(hue =c.1,s=c.2,v=c.3) %>%
mutate(h=rescale(hue))
im.df.mini <-im.df %>% sample_n(1000) %>% mutate(hex=hsv(h,s,v)) %>%
mutate(x=(row_number()-1)%%100,y=floor((row_number()-1)/100))
im.df.mini %>%
ggplot(aes(x=x,y=y)) +
geom_raster(aes(fill=hex)) +
#geom_tile(aes(fill=hex, height=v, width=s, alpha=h+v)) +
scale_fill_identity() +
theme_void() +
scale_alpha_continuous(range=c(0.5,1), guide="none") +
theme(plot.background = element_rect(fill="#ffffffde"))
ggsave("LivingCoralPresorted.png",width=16*2,height=9*2)
tsp <-im.df.mini %>% select(hue,s,v) %>% dist() %>% TSP()
#glimpse(tsp)
tour <- solve_TSP(tsp, method="nn")
im.df.mini_sorted <- im.df.mini[tour,] %>%
mutate(x=(row_number()-1)%%100,y=floor((row_number()-1)/100))
im.df.mini_sorted %>%
ggplot(aes(x=x,y=y)) +
geom_raster(aes(fill=hex)) +
#geom_tile(aes(fill=hex, height=v, width=s, alpha=h+v)) +
scale_fill_identity() +
theme_void() +
scale_alpha_continuous(range=c(0.5,1), guide="none") +
theme(plot.background = element_rect(fill="#ffffffde"))
ggsave("LivingCoralSorted.png",width=16*2,height=9*2)
im.df.mini %>%
ggplot(aes(x=x,y=y)) +
geom_tile(aes(fill=hex), alpha=1) +
geom_tile(aes(fill=hex, height=1-v, width=1-s, alpha=1),data=im.df.mini_sorted) +
scale_fill_identity() +
theme_void() +
scale_alpha_continuous(range=c(0.5,1), guide="none") +
theme(plot.background = element_rect(fill="#ffffffde"))
#coord_polar()
ggsave("LivingCoralUnsorted.png",width=16*2,height=9*2)
library(tidyverse)
library(imager)
im <- load.image("https://farm4.staticflickr.com/3769/14123426389_9c3a296fe4_z.jpg")
im.df <- im %>%
imager::RGBtoHSV() %>%
as.data.frame(wide="c") %>%
rename(h=c.1,s=c.2,v=c.3) %>%
mutate(h=rescale(h))
im.df.mini <-im.df %>% sample_n(1000) %>% mutate(hex=hsv(h,s,v)) %>%
mutate(dist=sqrt((h-lag(h,default=0))^2 + (s-lag(s,default=0))^2 + (v-lag(v,default=0))^2))
im.df.mini <- im.df.mini %>% arrange(dist) %>%
mutate(x=(row_number()-1)%%100,y=floor((row_number()-1)/100))
im.df.mini %>%
ggplot(aes(x=x,y=y)) +
geom_tile(aes(fill=hex, height=v, width=s, alpha=1-v)) +
scale_fill_identity() +
theme_void() +
scale_alpha_continuous(range=c(0.5,1), guide="none") +
theme(plot.background = element_rect(fill="#000000de")) +
coord_polar(theta="x")
ggsave("ColourBars.png",width=16,height=16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment