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

L*a*b* <-> sRGB conversions? #39

Closed
mceachen opened this issue Jan 25, 2018 · 5 comments
Closed

L*a*b* <-> sRGB conversions? #39

mceachen opened this issue Jan 25, 2018 · 5 comments

Comments

@mceachen
Copy link

mceachen commented Jan 25, 2018

Most importantly: Thanks for your work on d3! 🎉 , ❤️ and 🥇!

I was trying to use d3-color for sRGB to CIE-L*a*b* conversions today:

$ node
> const d3 = require("d3-color")
undefined
> d3.lab(0,100,100).rgb()
Rgb {
  r: 109.26297015067321,
  g: -126.7018621426472,
  b: -236.7921286790844,
  opacity: 1 }
> d3.lab(50,-50,-50).rgb()
Rgb {
  r: -868.5123725460351,
  g: 143.16534084117143,
  b: 203.4499609542193,
  opacity: 1 }

You expect L*a*b* values to be [[0-100], [-100,100], [-100,100]], correct? What colorspace are these RGB values in? I was expecting [0,255].

Am I using the API incorrectly?

(This is with node v8.9.0, fwiw)

@mceachen mceachen changed the title L*a*b <-> sRGB conversions? L*a*b* <-> sRGB conversions? Jan 25, 2018
@danburzo
Copy link
Contributor

Hi @mceachen, not all colors that can be described in L* a* b* are displayable, and it's expected for the R/G/B channels to go out of the normal bounds. color.displayable() will let you know when that's the case. (What you can do is clamp the R/G/B channels to [0, 255])

This color picker can give you a sense of when the colors get clipped.

@mbostock
Copy link
Member

Yep, think this should be considered duplicate of #33. Also, it’s my understanding there’s no strict range to the allowed values in Lab space since it depends on your display gamut.

@mceachen
Copy link
Author

mceachen commented Mar 31, 2018

OK, I'm clamping values. FWIW it seems that, with clamping, the RGB results match other implementations:

https://www.wolframalpha.com/input/?i=CIE+Lab+0,100,100
24-bit RGB | red 105 | green 0 | blue 0

https://www.wolframalpha.com/input/?i=CIE+Lab+50,-50,-50
24-bit RGB | red 0 | green 142 | blue 204

And from https://www.easyrgb.com/en/convert.php#inputFORM:

CIE-L*ab (D65/2°) = 50 -50 -50
sRGB   0-255 = -868.512  143.165  203.450    D65/2°

Perhaps this warrants a note in the README?

@danburzo
Copy link
Contributor

danburzo commented Mar 31, 2018

Actually I was just investigating this exact same issue. In the library I'm working on, I'm following these instructions to convert Lab to RGB, and I'm getting slightly different values than from d3-color, but they seem to match Wolfram Alpha's.

May be related to the choice of iluminant? D3 takes note of D65, but the W3C steps include a conversion from D50 (Lab) to D65 (sRGB) white point.

To make matters even more confusing, the W3C spec (and the ICC.1:2004) use Xn = 0.9642, Yn = 1.0000, and Zn = 0.8249 for D50, while Wikipedia says they're Xn = 0.966797, Yn = 1.0000, and Zn = 0.825188 (citation needed?)]

😅

@danburzo
Copy link
Contributor

danburzo commented Nov 5, 2018

Empirically, doing sRGB to Lab / LCh (now using the D50 standard illuminant as a reference white), gives me the following bounds:

lab

Channel Range Description
l [0, 100] Lightness
a [-79.2872, 93.55] Green–red component
b [-112.0294, 93.3884] Blue–yellow component

lch

Channel Range Description
l [0, 100] Lightness
c [0, 131.207] Chroma
h [0, 360] Hue

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

No branches or pull requests

3 participants