An introduction to qualpalr

Johan Larsson

2016-10-09

Overview

qualpalr generates distinct qualitative color palettes, primarily for use in R graphics. Given n (the number of colors to generate), along with a subset in the hsl color space (a cylindrical representation of the RGB color space) qualpalr attempts to find the n colors in the provided color subspace that maximize the smallest pairwise color difference. This is done by projecting the color subset from the HSL color space to the DIN99d space. DIN99d is (approximately) perceptually uniform, that is, the euclidean distance between two colors in the space is proportional to their perceived difference.

Examples

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

library(qualpalr)
pal <- qualpal(n = 5, list(h = c(0, 360), s = c(0.4, 0.6), l = c(0.5, 0.85)))

# Adapt the color space to deuteranopia
pal <- qualpal(n = 5, colorspace = "pretty", colorblind = "deutan")

The resulting object, pal, is a list with several color tables and a distance matrix based on the CIEDE2000 formula.

pal
#> $HSL
#>            Hue Saturation Lightness
#> #7474CD 242.02       0.49      0.62
#> #CACA75  64.83       0.48      0.62
#> #D9D9E9 189.29       0.49      0.84
#> #94948D 341.16       0.45      0.62
#> #AAAADD 237.80       0.46      0.76
#> 
#> $RGB
#>          Red Green Blue
#> #7474CD 0.45  0.45 0.80
#> #CACA75 0.79  0.79 0.46
#> #D9D9E9 0.85  0.85 0.91
#> #94948D 0.58  0.58 0.55
#> #AAAADD 0.67  0.67 0.87
#> 
#> $DIN99d
#>         L(99d) a(99d) b(99d)
#> #7474CD   56.2    3.9  -30.8
#> #CACA75   81.9   -5.2   29.2
#> #D9D9E9   88.9   -1.9   -8.7
#> #94948D   64.7   -3.1    4.6
#> #AAAADD   74.1    1.3  -21.9
#> 
#> $hex
#> [1] "#7474CD" "#CACA75" "#D9D9E9" "#94948D" "#AAAADD"
#> 
#> $de_DIN99d
#>         #7474CD #CACA75 #D9D9E9 #94948D
#> #CACA75      28                        
#> #D9D9E9      20      19                
#> #94948D      19      16      15        
#> #AAAADD      12      24      12      15
#> 
#> $min_de_DIN99d
#> [1] 11.75623
#> 
#> attr(,"class")
#> [1] "qualpal" "list"

Methods for pairs and plot have been written for qualpal objects to help visualize the results.

# Multidimensional scaling plot
plot(pal)

# Pairs plot in the Lab color space
pairs(pal, colorspace = "DIN99d")

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))

Details

qualpal begins by generating a point cloud out of the HSL color subspace provided by the user, using a quasi-random torus sequence from randtoolbox. Here is the color subset in HSL with settings h = c(-200, 120), s = c(0.3, 0.8), l = c(0.4, 0.9).

The program then proceeds by projecting these colors into the sRGB space.

It then continues projecting the colors, first into the XYZ space, then CIELab (not shown here), and then finally the DIN99d space.

The DIN99d color space (G. Cui et al. 2002) is a euclidean, perceptually uniform color space. This means that the difference between two colors is equal to the euclidean distance between them. We take advantage of this by computing a distance matrix on all the colors in the subset, finding their pairwise color differences. We then apply a power transformation (Huang et al. 2015) to fine tune these differences.

To select the n colors that the user wanted, we proceed greedily: first, we find the two most distanct, then we find the third point that maximizes the minimum distance to the previously selected points. This is repeated until n points are selected. These points are then returned to the user; below is an example using n = 5.

Color specifications

At the time of writing, qualpalr works selectively in the sRGB color space with the CIE Standard Illuminant D65 reference white.

Thanks

Bruce Lindbloom’s webpage has been instrumental in making qualpalr. Also thanks to i want hue, which inspired me to make qualpalr.

References

Cui, G., M. R. Luo, B. Rigg, G. Roesler, and K. Witt. 2002. “Uniform Colour Spaces Based on the DIN99 Colour-Difference Formula.” Color Research & Application 27 (4): 282–90. doi:10.1002/col.10066.

Huang, Min, Guihua Cui, Manuel Melgosa, Manuel Sánchez-Marañón, Changjun Li, M. Ronnier Luo, and Haoxue Liu. 2015. “Power Functions Improving the Performance of Color-Difference Formulas.” Optics Express 23 (1): 597. doi:10.1364/OE.23.000597.