R
contains several tools that produce high-quality sequential and diverging color palettes, but few that are dedicated to qualitative color palettes. ´qualpalr´ is an attempt to fill that gap by providing an easy-to-use interface to generate qualitative color palettes of any size and from whatever range of colors the user prefers.
Provided n
(the desired number of colors to generate), and a subset of the hsl color space, qualpalr
uses a Nelder-Mead optimizer (nmkb
from dfoptim to find the configuration of n
colors that maximize the smallest pairwise CIEDE2000 color difference.
qualpalr
relies on one basic function, qualpal()
, which takes as its input n
(the number of colors to generate) and colorspace
, which can be either
h
(hue from -360 to 360), s
(saturation from 0 to 1), and l
(lightness from 0 to 1), all of length 2, specifying a min and max.library(qualpalr)
set.seed(1) # The optimizer uses random initial parameters
pal <- qualpal(n = 5, list(h = c(0, 360), s = c(0.4, 0.6), l = c(0.5, 0.85)))
pal <- qualpal(n = 4, colorspace = "pretty")
The resulting object, pal2
, is a list with several color tables and a distance matrix based on the CIEDE2000 formula.
pal
## $hsl
## h s l
## #66C1CC 186.46249 0.4998636 0.6000005
## #7F66CC 254.83670 0.4999805 0.6000000
## #E5B1B1 360.00000 0.4999754 0.7961098
## #CCCC66 59.99993 0.5000000 0.6000426
##
## $RGB
## Red Green Blue
## #66C1CC 0.4000553 0.7568743 0.7999457
## #7F66CC 0.4989153 0.4000078 0.7999922
## #E5B1B1 0.8980499 0.6941697 0.6941697
## #CCCC66 0.8000213 0.8000208 0.4000639
##
## $Lab
## L a b
## #66C1CC 73.04362 -24.37183 -13.987743
## #7F66CC 49.97467 33.42222 -49.637485
## #E5B1B1 76.89821 18.95620 7.372052
## #CCCC66 80.17907 -13.77859 50.253256
##
## $hex
## [1] "#66C1CC" "#7F66CC" "#E5B1B1" "#CCCC66"
##
## $ciede2000
## #66C1CC #7F66CC #E5B1B1
## #7F66CC 35.40279
## #E5B1B1 49.13179 35.40280
## #CCCC66 35.40279 68.09980 35.40279
##
## $min_ciede2000
## [1] 35.40279
##
## attr(,"class")
## [1] "qualpal" "list"
For more descriptive versions of what colors were generated, methods for pairs
and plot
have been written for qualpal
objects:
# Multidimensional scaling plot
plot(pal)
# Pairs plot in the Lab color space
pairs(pal, colorspace = "Lab")
The colors are most easily used in R by accessing pal$hex
library(maps)
map("france", fill = TRUE, col = pal$hex, mar = c(0, 0, 0, 0))
Control parameters can be passed to nmkb
via ...
in qualpal
, of which maxfeval
(default: 5000) and tol
(default: 1e-6) are probably of primary interest. See the documentation to dfoptim for details.
qualpalr
was not designed for speed. It is slow, particularly for large n
, and I strongly recommend aginst using it in other functions.