Created
March 5, 2020 04:15
-
-
Save JosiahParry/99c58336bcd76c05abbe84f76b7b5f5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
title: "colorpalette.cinema" | |
output: html_document | |
--- | |
Fetch the color palettes from [colorpalette.cinema](https://www.instagram.com/colorpalette.cinema/). | |
```{r include=FALSE} | |
library(wesanderson) | |
library(tidyverse) | |
# function to create palette from image path | |
extract_palette <- function(fp) { | |
tmp <- tempfile() | |
download.file(fp, tmp, mode="wb") | |
pic <- jpeg::readJPEG(tmp) | |
file.remove(tmp) | |
img_dims <- dim(pic) | |
# identify height position of each color | |
col_y_pos <- img_dims[1] * .95 | |
# grab a horizontal position of each color in palette | |
col_x_pos <- seq(20, img_dims[2], by = img_dims[2]/10) | |
# convert rgb to hex | |
colors <- map_chr(col_x_pos, ~{ | |
rgb_vals <- pic[col_y_pos, .x,] | |
rgb(rgb_vals[1], rgb_vals[2], rgb_vals[3]) | |
}) | |
# pal <- structure(colors, class = "palette") | |
# pal | |
colors | |
} | |
make_palette <- function(pal, n, type = c("discrete", "continuous"), ...) { | |
type <- match.arg(type) | |
if (missing(n)) { | |
n <- length(pal) | |
} | |
index <- seq(1, length(pal), by = length(pal)/n) | |
if (type == "discrete" && n > length(pal)) { | |
stop("Number of requested colors greater than what palette can offer") | |
} | |
out <- switch(type, | |
continuous = grDevices::colorRampPalette(pal)(n), | |
discrete = pal[index] | |
) | |
structure(out, class = c("palette", "character"), ...) | |
} | |
``` | |
```{r fig.width=10} | |
img_path <- "https://i.pinimg.com/474x/3c/0d/57/3c0d5717bf971f27b294f188b1ec59fb.jpg" | |
knitr::include_graphics(img_path) | |
``` | |
```{r} | |
(pride <- extract_palette("https://i.pinimg.com/474x/3c/0d/57/3c0d5717bf971f27b294f188b1ec59fb.jpg")) | |
``` | |
```{r} | |
(pride_pal <- make_palette(pride, n = 10, type = "continuous")) | |
``` | |
```{r} | |
ggplot(heatmap, aes(x = X2, y = X1, fill = value)) + | |
geom_tile() + | |
scale_fill_gradientn(colours = pride_pal) + | |
scale_x_discrete(expand = c(0, 0)) + | |
scale_y_discrete(expand = c(0, 0)) + | |
coord_equal() | |
``` | |
# https://git.io/JvVcg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment