thomasp85 / farver

High Performance Colourspace Manipulation in R
https://farver.data-imaginist.com
Other
124 stars 14 forks source link

farver converts hcl differently to R #9

Closed hadley closed 4 years ago

hadley commented 4 years ago
h <- 15
c <- 100
l <- 65
grDevices::hcl(h, c, l)
#> [1] "#F8766D"

lch <- cbind(l, c, h)
rgb <- farver::convert_colour(lch, "lch", "rgb")
rgb(pmin(pmax(round(rgb), 0), 255), maxColorValue = 255)
#> [1] "#FF0076"

Created on 2019-11-05 by the reprex package (v0.3.0)

statibk commented 4 years ago

(Commenting here because Thomas pointed me to this issue in a DM. Maybe the following remarks are helpful. If not simply ignore...)

grDevices::hcl() regards HCL as the color space you obtain when taking polar coordinates in the CIELUV color space while farver::convert_colour() employs polar coordinates in the CIELAB color space. In principle, both are feasible and are also used in other implementations called HCL. Other implementations are more verbose and call the same spaces CIELCH(uv) and CIELCH(ab), respectively.

In colorspace both versions are available as polarLUV() and polarLAB() respectively:

lch <- cbind(65, 100, 15)
colorspace::hex(colorspace::polarLUV(lch))
## "#F8766D"
colorspace::hex(colorspace::polarLAB(lch), fixup = TRUE)
## "#FF0076"

The grDevices::hcl() implementation was originally written by Ross (added to grDevices in 2005 in r32736) and goes back to his discussion in Ihaka (2003 DSC Proceedings). I think (although I never asked Ross explicitly about this) that his decision to adopt CIELCH(uv) as the HCL color model is due to this comment from the 2003 paper:

The two perceptually based spaces introduced by the CIE in 1976 are the CIELUV and CIELAB spaces. The CIELUV space is generally preferred by those who work with emissive colour technologies (such as computer displays) and the CIELAB space is preferred by those working with dyes and pigments (such as in the printing and textile industries). I will confine my remarks to the CIELUV space. This is because the presentation graphics I will describe are typically created on computer displays and this is where colour experimentation takes place.

My impression is that overall the CIELCH(uv) model is a bit more popular or more widely used. But this might, of course, be a bit biased through R (and our own work). The Wikipedia page about the HCL color model also comments on these differences. Disclaimer: If you look at the history of that page you will see that some additions were made by me, but most parts have been written by other authors. It also points to yet another definition of HCL due to Sarifuddin and Missaou. However, that appears to be very rarely used in practice.

thomasp85 commented 4 years ago
h <- 15
c <- 100
l <- 65
grDevices::hcl(h, c, l)
#> [1] "#F8766D"

hcl <- cbind(h, c, l)
rgb <- farver::convert_colour(hcl, "hcl", "rgb")
rgb(pmin(pmax(round(rgb), 0), 255), maxColorValue = 255)
#> [1] "#F8766D"