R/extract_color_pal_cinema.R

Defines functions extract_cpc_pal

Documented in extract_cpc_pal

#' Extract color palette from @colorpalette.cinema image
#' 
#' @param fp The path to a @colorpalette.cinema image 
#' 
#' @importFrom jpeg readJPEG
#' @importFrom purrr map_chr
#' 
#' @export
extract_cpc_pal<- function(fp) {
  
  if (!file.exists(fp)) {
    tmp <- tempfile()
    download.file(fp, tmp, mode="wb", quiet = TRUE)
    pic <- jpeg::readJPEG(tmp)
    file.remove(tmp)
  } else {
    pic <- jpeg::readJPEG(fp)
  }
  
  
  
  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 <- purrr::map_chr(col_x_pos, ~{
    rgb_vals <- pic[col_y_pos, .x,]
    rgb(rgb_vals[1], rgb_vals[2], rgb_vals[3])
  })
  
  new_palette(colors, length(colors), "discrete")
  
}
JosiahParry/cpcinema documentation built on March 20, 2021, 12:04 a.m.