color_dissimilarity

Measure the similarity of colors in a palette, and choose dissimilar colors from them.
git clone https://git.eamoncaddigan.net/color_dissimilarity.git
Log | Files | Refs | README | LICENSE

seq_dissim_palette.R (1001B)


      1 
      2 #' Sequential dissimilarity palette
      3 #'
      4 #' Returns a palette function that, given a number `n` will return a sequence of
      5 #' decreasingly dissimilar colors. If `n` is greater than the number of colors
      6 #' specified, it falls back to a color ramp (in the same order used for the
      7 #' sequence of dissimilar colors). If you definitely want a color ramp it's best
      8 #' to call that directly.
      9 #'
     10 #' @param cols A list of color names
     11 #' @param cvd A color vision deficiency
     12 #'
     13 #' @return A palette function
     14 #' @export
     15 #'
     16 #' @examples
     17 #' seq_dissim_palette(c('orangered2', 'dodgerblue4', 'black', 'white'))(3)
     18 #'
     19 #' seq_dissim_palette(c('orangered2', 'dodgerblue4', 'black', 'white'))(6)
     20 seq_dissim_palette <- function(cols, cvd = 'deuteranomaly') {
     21   dissim_mat <- cvd_dissimilarity(cols, cvd)
     22   color_sequence <- seq_dissim_colors(dissim_mat)
     23 
     24   function(n) {
     25     if (n <= length(cols))
     26       cols[color_sequence[seq(n)]]
     27     else
     28       grDevices::colorRampPalette(cols[color_sequence], space = 'Lab')(n)
     29   }
     30 }