Skip to content

Instantly share code, notes, and snippets.

@noerw
Last active December 3, 2015 10:09
Show Gist options
  • Save noerw/d4f77ba94d972a3dbf33 to your computer and use it in GitHub Desktop.
Save noerw/d4f77ba94d972a3dbf33 to your computer and use it in GitHub Desktop.
create a color ramp, which assigns every RGBInt value it's true color
# i know, this is a really stupid hack, but "necessary" for
# this issue: https://github.com/rstudio/leaflet/issues/212
#' @describe creates a list of RGB values in ascending order,
#' with the blue channel beeing the least significant,
#' and the red channel beeing the most significant.
#' @param bitdepth The bitdepth of the values to create.
#' defaults to 8bit, and creates ~16kk values
#' @return a matrix containing the RGB values as integers.
rgbIntMatrix <- function(bitdepth = 8) {
numOfValues <- 2^bitdepth
max <- numOfValues - 1
# eg for bitdepth = 1: 00001111
red <- rep(c(0:max), each = numOfValues)
red <- rep(red, each = numOfValues)
# eg for bitdepth = 1: 00110011
green <- rep(c(0:max), each = numOfValues)
green <- rep(green, numOfValues)
# eg for bitdepth = 1: 01010101
blue <- rep(c(0:max), numOfValues^2)
matrix(c(red, green, blue), ncol = 3, nrow = numOfValues^3)
}
# create RGB strings with the above function
# resulting object has a size of ~900MB...
palette <- rgb(rgbIntMatrix(8), maxColorValue = 255)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment