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

commit 4a64535d7641b8076c37dcba7a4e34160d6f1b7e
parent 708361796bcd0dc533d51bba10dffc232ebcc0dd
Author: Eamon Caddigan <eamon.caddigan@gmail.com>
Date:   Wed,  8 Sep 2021 06:20:36 -0400

A simpler approach to choosing dissimilar colors, with advantages and disadvantages

Diffstat:
Mdemo.Rmd | 11++++++-----
Aseq_dissim_colors.R | 35+++++++++++++++++++++++++++++++++++
2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/demo.Rmd b/demo.Rmd @@ -17,7 +17,7 @@ library(grDevices) ``` ```{r} source('dissimilarity.R') -source('max_dissim.R') +source('seq_dissim_colors.R') source('color_utils.R') ``` ```{r} @@ -33,6 +33,8 @@ Next let's grab some random colors and show them. some_colors <- c('red', 'black', 'white', 'khaki', 'gray50', '#a6cee3', '#1f78b4', '#b2df8a', '#33a02c') some_colors <- paste0("gray", round(seq(10, 90, length.out = 13))) +some_colors <- colorRampPalette(c("gray10", "gray90"), space = "Lab")(6) +some_colors <- sample(some_colors) compare_colors(some_colors) ``` @@ -45,14 +47,13 @@ compare_colors(some_colors, function(x) colorspace::deutan(x, 0.5)) And here's the dissimilarity matrix of the colors. ```{r} -dissim_mat <- cvd_dissimilarity(some_colors) +dissim_mat <- cvd_dissimilarity(some_colors, "none") image(dissim_mat) ``` Let's look at sets of dissimilar colors from the palette. ```{r} -lapply(max_dissim_colors(dissim_mat), unlist) +colorspace::swatchplot(some_colors, + some_colors[seq_dissim_colors(dissim_mat)]) ``` - -No, this isn't doing the right thing! :( diff --git a/seq_dissim_colors.R b/seq_dissim_colors.R @@ -0,0 +1,35 @@ +#' Choose maximally dissimilar colors sequentially +#' +#' This function chooses colors greedily, staring with the most dissimilar pair +#' and then choosing the color that is most dissimilar from the previously +#' chosen ones. +#' +#' @param dissim_mat A dissimilarity matrix +#' +#' @return The element IDs in decreasing similarity +#' @export +seq_dissim_colors <- function(dissim_mat) { + color_sequence <- rep(NA, ncol(dissim_mat)) + + # First, we'll find the column that has the pair of colors with the maximum + # dissimilarity and save that column as the "first" color we picked and store + # this column as a vector. + color_sequence[1] <- which.max(dissim_mat) %/% nrow(dissim_mat) + dissim_vec <- dissim_mat[, color_sequence[1]] + + for (i in seq(2, length(dissim_vec))) { + # Find the index of the maximum dissimilarity of the current vector and save + # it in the list of colors. + color_sequence[i] <- which.max(dissim_vec) + + # Replace each element in this vector with the minimum of that element and the + # element with the same index in the newly selected color. + dissim_vec <- pmin(dissim_vec, dissim_mat[, color_sequence[i]]) + } + + # Add the color names if they're present in the dissimilarity matrix + if (!is.null(colnames(dissim_mat))) + names(color_sequence) <- colnames(dissim_mat)[color_sequence] + + color_sequence +}