R/getQuinsPal.R

perLightness = function(x){
  L = (100 * 0.5 * (max(col2rgb(x)) + min(col2rgb(x)))) / 255
  L
}

#' getQuinsPal
#'
#' Get the best colorscheme with three main colors at 45% luminance,
#' plus two greys at 25lum lighter and darker.
#'
#' @param vector only return a unnamed vector
#' @param plot Plot a simple plot to showcase the colors
#' @return A df:
#' \itemize{
#'  \item{name: }{The categories of the colours.}
#'  \item{hex: }{The hex-values of the colours.}
#'  \item{lightness: }{The perceived lightness of the colours.}
#' }
getQuinsPal = function(vector = T, plot = F){


  quinPal = c('blue' = '#2283c3', 'cherry' = '#dd085a',
              'green' = '#2eb84c',
              'light' = '#b3b3b3', 'dark' = '#333333')

  if(plot){
    df = data.frame(xx = rep(names(quinPal), each = 500),
                    yy = rep(c(1,1,1,1,1), each = 500),
                    state = factor(names(quinPal)[rep(c(1,2,3,4,5), each = 500)]))

    require(ggplot2)
    require(RHWlib)
    G = ggplot(df, aes(x=xx,y=yy,col = (state))) +
      RHWlib::RHWtheme() +
      scale_colour_manual(values = (quinPal))+
      geom_jitter() +
      guides(col = F) +
      theme( axis.text = element_text(),
             axis.title = element_blank(),
             axis.ticks = element_blank()) +
      ggtitle('Harlequins') +
      scale_y_continuous(breaks = NULL)
    print(G)

  }

  coldf = cbind(names(quinPal),
                as.data.frame(quinPal),unlist(lapply(quinPal, perLightness)))
  colnames(coldf) = c('name',  'hex','lightness')
  rownames(coldf) = NULL

  if(vector){
    return(as.character(unname(coldf$hex)))
  } else {
    return(coldf)
  }
}
robinweide/RHWlib documentation built on May 7, 2019, 8:03 a.m.