# Introducing qualpalr

With the advent of colorbrewer there now exists good options to generate color palettes for sequential, diverging, and qualitative data. In R, these palettes can be accessed via the popular RColorBrewer package. Those palettes, however, are limited to a fixed number of colors. This isn’t much of a problem for sequential of diverging data since we can interpolate colors to any range we desire:

```
pal <- RColorBrewer::brewer.pal(4, "PuBuGn")
color_ramp <- colorRampPalette(pal, space = "Lab")
```

There is not, however, an analogue for qualitative color palettes that will get you beyond the limits of 8–12 colors of colorbrewer’s qualitative color palettes.

There is also no customization in colorbrewer. Other R packages, such as colorspace offer this, but they are primarily adapted to sequential and diverging data – not qualitative data.

This is where qualpalr comes in. qualpalr provides the user with a convenient way of generating 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

- a list of numeric vectors
`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. - a character vector specifying one of the predefined color subspaces, which at the time of writing are
*pretty*,*pretty_dark*,*rainbow*, and*pastels*.

```
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 din99d color difference formula.

`pal`

```
## $HSL
## Hue Saturation Lightness
## #91919D 327.71 0.45 0.60
## #E1E1B7 62.97 0.45 0.80
## #92926E 1.82 0.45 0.61
## #7777CD 236.37 0.49 0.62
## #DBDBE7 181.91 0.47 0.83
##
## $RGB
## Red Green Blue
## #91919D 0.57 0.57 0.61
## #E1E1B7 0.88 0.88 0.72
## #92926E 0.57 0.57 0.43
## #7777CD 0.47 0.47 0.80
## #DBDBE7 0.86 0.86 0.91
##
## $DIN99d
## L(99d) a(99d) b(99d)
## #91919D 64.0 -1.3 -7.5
## #E1E1B7 90.0 -4.8 18.9
## #92926E 63.5 -4.0 17.9
## #7777CD 57.2 3.7 -30.2
## #DBDBE7 89.3 -2.2 -7.0
##
## $hex
## [1] "#91919D" "#E1E1B7" "#92926E" "#7777CD" "#DBDBE7"
##
## $de_DIN99d
## #91919D #E1E1B7 #92926E #7777CD
## #E1E1B7 19
## #92926E 14 14
## #7777CD 14 26 23
## #DBDBE7 14 14 18 20
##
## $min_de_DIN99d
## [1] 13.53182
##
## 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 din99d color space
pairs(pal, colorspace = "DIN99d")
```

The colors are normally used in R by fetching the `hex`

attribute of the palette.

```
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 function then proceeds by projecting these colors into the sRGB space.

It then continues by 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 distant points, 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 only works in the sRGB color space with the CIE Standard Illuminant D65 reference white.

## Future directions

The greedy search to find distinct colors is crude. Particularly when searching for few colors, the greedy algorithm will lead to sub-optimal results. Other solutions to finding points that maximize the smallest pairwise distance among them are welcome.

## 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 Res. Appl.* 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.

## Comments