Continuing on with my “slight?” obsession with colours… I love colours in “Material Colour Palette”. There various website that will lets you grab the colours by clicking, such as this one, but I just wanted to have little handy cheet sheet for myself, so I’ve decided I’ll do that using R &amp; my favourite ggplot2.
Getting colours out of image using package “imager”
After quick search, I came across image with all the material colour, so first things I’ve tried is to get colours out of image using imager.
## Load up packages we'll use
library(tidyverse)
library(imager)
library(patchwork)
im %
as.data.frame(wide=&quot;c&quot;) %__%
rename(h=c.1, s=c.2, v=c.3)
## Convert Image to Data Frame with RGB value
im_rgb % as.data.frame(wide=&quot;c&quot;) %__%
rename(red=c.1,green=c.2,blue=c.3) %__%
mutate(hexvalue = rgb(red,green,blue)) ## you can create hexvalue using red, green blue value!
## Might as well conver to grayscale, and get luminance. ## I;ll use luminance value to decide if I'll put black text vs white text later.
im_grayscale % grayscale() %__%
as.data.frame() %__%
rename(luminance=value)
## I want to grab pixel from about middle of each cell
mat_color %
filter(x %in% as.integer(round(seq(1,19)*(400/19))-10) &amp;
y %in% as.integer(round(seq(1,11)*(225/11))-10) &amp; y__10) %__%
left_join(im_hsv) %__%
left_join(im_grayscale) %__%
mutate_at(c(&quot;x&quot;,&quot;y&quot;), dense_rank) %__%
arrange(x,y) col_group % write_csv(&quot;MaterialColour.csv&quot;)
Creating Material Colour Palette Cheat Sheet
Now that I have the colours out of image in data frame, I can do fun stuff, plotting!! I should print this with colour printer, and have it as one of cheat sheet collection.
I’ve used luminance value of each colour to decide if I should place black text or white text over the colour. (I couldn’t figure out if there’s good rules to follow, but seems like luminance does the trick?!)
mat_color %__% ggplot(aes(x=hue_group,y=shade)) + ## I could also use x=x,y=y
geom_tile(aes(fill=hexvalue),color=&quot;white&quot;, size=0.1) + ## i want to have very fine white line around each tiles.
scale_fill_identity(guide=&quot;none&quot;) + theme_void(base_family=&quot;Roboto Condensed&quot;) +
## print out color hue name and shade
geom_text(aes(label=paste0(hue_group,&quot;-&quot;,shade), color=ifelse(luminance__0.5,&quot;#000000&quot;,&quot;#ffffff&quot;)), ## about 48% opacity
family=&quot;Roboto Condensed&quot;, size=3, vjust=-2, lineheight=0.8, alpha=0.48) +
## print out hesvalue - I'll use this the most, so print it with higher transparency
geom_text(aes(label=hexvalue, color=ifelse(luminance__0.5,&quot;#000000&quot;,&quot;#ffffff&quot;)), ## about 80% opacity
family=&quot;Roboto Condensed&quot;, fontface=&quot;bold&quot;,vjust=0, alpha=0.8) +
## print out RGB &amp; HSV
geom_text(aes(label=paste0(&quot;\n(&quot;,round(red*255),&quot;,&quot;,round(blue*255),&quot;,&quot;,round(green*255),&quot;)\n(&quot;,
round(h),&quot;,&quot;,round(s,1),&quot;,&quot;,round(v,1),&quot;)&quot;), color=ifelse(luminance__0.5,&quot;#000000&quot;,&quot;#ffffff&quot;)), ## about 67% opacity
family=&quot;Roboto Condensed&quot;, size=3, vjust=1, lineheight=0.8, alpha=0.67) +
scale_color_identity() +
labs(x=&quot;&quot;,y=&quot;&quot;,title=&quot;&quot;, caption=&quot;Material Colour Palettes Hexvalue + RGB + HSV&quot;)
## I can save as PNG file too with below line
#ggsave(&quot;MaterialColorCheatSheet.png&quot;, width=16, height=9)
More Parrrty Time with Colours…
While plotting colour in rectangular is good…. I thought it’s a lot nicer to plot them as “Colour Wheel”.
# Colour Wheel! wheel_base % filter(!hue_group %in% c(&quot;brown&quot;,&quot;grey&quot;,&quot;blue grey&quot;)) %__% ## exclude brown, grey and blue grey group.
ggplot(aes(x=x, y=y)) +
geom_tile(aes(fill=hexvalue), color=&quot;white&quot;, size=0.1) +
scale_fill_identity(guide=&quot;none&quot;) +
coord_polar() + ## Converting to polar coordinate does the trick!
theme_void(base_family=&quot;Roboto Condensed&quot;) +
labs(caption=&quot;Color Wheel using Material Design Colours&quot;)
## Just experimenting with smaller strips on each colour tiles..
wheel_base_w %
ggplot(aes(x=sqrt(t) * cos(t), y=sqrt(t) * sin(t))) +
geom_point(aes(color=hexvalue, size=luminance)) +
scale_color_identity() +
coord_fixed() +
theme_void(base_family=&quot;Roboto Condensed&quot;) +
scale_size_continuous(range=c(3,8), guide=&quot;none&quot;) +
labs(caption=&quot;Sort by Hue Group, Then Shade, Luminance as Size&quot;)
## ggplot2 has shape 1-25
## 0-15 &amp; 20-24 are NOT filled shape
flower3%
arrange(shade, hue_group) %__% mutate(t=row_number()) %__%
ggplot(aes(x=sqrt(t) * cos(t), y=sqrt(t) * sin(t))) +
geom_point(aes(color=hexvalue, shape=ifelse(x% ggplot(aes(x=x,y=y)) + geom_tile(aes(fill=hexvalue, width=s*10, height=v*10, alpha=luminance)) +
scale_fill_identity() +
scale_alpha_continuous(guide=&quot;none&quot;, range=c(0,0.6)) +
theme_void() +
annotate(x=(max(mat_color$x)/2)+0.05,y=(max(mat_color$y)/2)-0.05, label=&quot;The End&quot;,
geom=&quot;text&quot;, family=&quot;Roboto Condensed&quot;, size=22, color=&quot;#000000de&quot;) +
annotate(x=max(mat_color$x)/2,y=max(mat_color$y)/2, label=&quot;The End&quot;,
geom=&quot;text&quot;, family=&quot;Roboto Condensed&quot;, size=22, color=&quot;#ffffffde&quot;) +
coord_cartesian(xlim=c(0,19), ylim=c(3,7))