d3 / d3-color

Color spaces! RGB, HSL, Cubehelix, CIELAB, and more.
https://d3js.org/d3-color
ISC License
398 stars 91 forks source link

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

Closed mceachen closed 6 years ago

mceachen commented 6 years ago

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)

danburzo commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 5 years ago

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