Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

farver converts hcl differently to R #9

Closed
hadley opened this issue Nov 5, 2019 · 2 comments
Closed

farver converts hcl differently to R #9

hadley opened this issue Nov 5, 2019 · 2 comments

Comments

@hadley
Copy link

hadley commented Nov 5, 2019

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)

hadley added a commit to r-lib/scales that referenced this issue Nov 5, 2019
farver converts rather differently, see thomasp85/farver#9 for details.
@statibk
Copy link

statibk commented Nov 5, 2019

(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
Copy link
Owner

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants