Why do viridis and hcl.color yield different colors?

60 views Asked by At

I expected the calls below to viridis and hcl.color to output the same three colors:

> viridis::viridis(9) %>% sub("FF$", "", .)
[1] "#440154" "#472D7B" "#3B528B" "#2C728E" "#21908C" "#27AD81" "#5DC863" "#AADC32" "#FDE725"
> viridisLite::viridis(9) %>% sub("FF$", "", .)
[1] "#440154" "#472D7B" "#3B528B" "#2C728E" "#21908C" "#27AD81" "#5DC863" "#AADC32" "#FDE725"
> grDevices::hcl.colors(9, palette = "viridis")
[1] "#4B0055" "#403173" "#00588B" "#007B97" "#009B95" "#00B785" "#53CC67" "#B2DC3C" "#FDE333"
> colorspace::sequential_hcl(9, "Viridis")
[1] "#4B0055" "#403173" "#00588B" "#007B97" "#009B95" "#00B785" "#53CC67" "#B2DC3C" "#FDE333"

However, these functions generate slightly different colors: enter image description here

Is this expected? What am I missing about the differences between these functions? Thanks!

1

There are 1 answers

1
nicholaspooran On

The differences in colors between viridis and hcl.colors with palette = "viridis" are expected because they operate in different color spaces, and the mapping between these spaces may not perfectly preserve the original colors. If you want to use the "viridis" palette, it's recommended to directly use the viridis function to ensure consistent and perceptually uniform colors.

Edit:

viridis typically generates colors in the HSL (Hue, Saturation, Lightness) color space.

hcl.color generates colors in the HCL (Hue, Chroma, Luminance) color space.