chroma.js icon indicating copy to clipboard operation
chroma.js copied to clipboard

Type of RGB LAB conversion not documented

Open pnorman opened this issue 6 years ago • 2 comments

It's not possible to convert between RGB and CIELAB without knowing the RGB colour space and the illuminant for LAB.

These aren't listed in the documentation, nor do they appear to be selectable.

pnorman avatar Mar 05 '18 02:03 pnorman

I assume you are referring to the values defined in lab-constants.js? https://github.com/gka/chroma.js/blob/master/src/io/lab/lab-constants.js

Kn: 18,
Xn: 0.950470,
Yn: 1,
Zn: 1.088830,
t0: 0.137931034,  // 4 / 29
t1: 0.206896552,  // 6 / 29
t2: 0.12841855,   // 3 * t1 * t1
t3: 0.008856452, // t1 * t1 * t1

they are in fact not selectable through the public API of chroma-js at this point.

However, you can now change them when using chroma.js in Node:

> const chroma = require('chroma-js/index');

> chroma('red').lab();
[ 53.24079414130722, 80.09245959641109, 67.20319651585301 ]

> const LAB = require('./src/io/lab/lab-constants');
{ Kn: 18,
  Xn: 0.95047,
  Yn: 1,
  Zn: 1.08883,
  t0: 0.137931034,
  t1: 0.206896552,
  t2: 0.12841855,
  t3: 0.008856452 }

> LAB.Xn = 0.9
0.9

> chroma('red').lab()
[ 53.24079414130722, 87.04013581691311, 67.20319651585301 ]

gka avatar Dec 12 '18 00:12 gka

@pnorman is correct that these are not documented. The answers are sRGB and D65, respectively.

The RGB colorspace used can be inferred from the code:

    const x = xyz_lab((0.4124564 * r + 0.3575761 * g + 0.1804375 * b) / LAB_CONSTANTS.Xn);
    const y = xyz_lab((0.2126729 * r + 0.7151522 * g + 0.0721750 * b) / LAB_CONSTANTS.Yn);
    const z = xyz_lab((0.0193339 * r + 0.1191920 * g + 0.9503041 * b) / LAB_CONSTANTS.Zn);

This is the linear-RGB to XYZ conversion matrix for sRGB.

It would be useful to document this and to allow conversion to/from other RGB colorspaces such as ProPhoto RGB, display P3, Adobe 1998 RGB, and so on.

Likewise the illuminant can be inferred from Xn, Yn, Zn:

  Xn: 0.95047,
  Yn: 1,
  Zn: 1.08883,

These are the values for D65, which is also the white-point for sRGB (and indeed most other RGB colorspaces, with the notable exceptions of ProPhoto RGB and DCI P3). This can also be inferred from the absence of any chromatic adaptation step in the XYZ to Lab conversion.

Again, it would be helpful to document this.

Note that many spectrophotometers report Lab referenced to a D50 whitepoint ("M1 measurement condition"). For a D65 measurement condition, it may be necessary to perform the spectral reflectance data to XYZ and then Lab conversion yourself, if the software does not provide it. This will be more accurate than doing chromatic adaptation.

svgeesus avatar Dec 20 '19 19:12 svgeesus