Skip to content

Instantly share code, notes, and snippets.

@rich-iannone
Last active August 29, 2015 14:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rich-iannone/18686171762100747edd to your computer and use it in GitHub Desktop.
Save rich-iannone/18686171762100747edd to your computer and use it in GitHub Desktop.
R function that loads in chromatography.js, constructs a string of JS code, passes that code for evaluation (using the 'V8' package), and returns a palette of visually different colors.
library(V8)
# Create function to generate palette of visually different colors
hex_palette <- function(no_colors,
hue_range = NULL,
chroma_range = NULL,
lightness_range = NULL){
pal <- function(col, border = "light gray", ...){
n <- length(col)
plot(0, 0, type = "n", xlim = c(0, 1), ylim = c(0, 1),
axes = FALSE, xlab = "", ylab = "", ...)
rect(0:(n-1)/n, 0, 1:n/n, 1, col = col, border = border)
}
js_call <- paste0("var colors = createPalette.generate(",
no_colors, ", function(color){ var hcl = color.hcl(); return hcl[0]>=",
hue_range[1], " && hcl[0]<=",
hue_range[2], "&& hcl[1]>=",
chroma_range[1], "&& hcl[1]<=",
chroma_range[2], "&& hcl[2]>=",
lightness_range[1], "&& hcl[2]<=",
lightness_range[2], "; }, false, 20);",
"var colors = createPalette.diffSort(colors); colors;")
ct <- new_context("window")
invisible(ct$source('https://raw.githubusercontent.com/WeAreVisualizers/chromatography/master/chromatography.js'))
hex_colors <- unlist(strsplit(ct$eval(js_call), ","))
pal(hex_colors)
return(hex_colors)
}
# Generate a palette of 5 colours of low chroma and high lightness
colors <- hex_palette(no_colors = 5,
hue_range = c(0, 360),
chroma_range = c(0, 0.9),
lightness_range = c(1.0, 1.5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment