Sha256: 5742d65ece05e435cef6719eb1745f09398adbd6e0ccc3247fc8befb31264eb3

Contents?: true

Size: 1.82 KB

Versions: 26

Compression:

Stored size: 1.82 KB

Contents

import { epsilon, kappa, wd50X, wd50Y, wd50Z, matrix } from './util';

/* Convert between Lab and XYZ
/* ========================================================================== */

export function lab2xyz(labL, labA, labB) {
	// compute f, starting with the luminance-related term
	const f2 = (labL + 16) / 116;
	const f1 = labA / 500 + f2;
	const f3 = f2 - labB / 200;

	// compute pre-scaled XYZ
	const [ initX, initY, initZ ] = [
		Math.pow(f1, 3) > epsilon ? Math.pow(f1, 3)                : (116 * f1 - 16) / kappa,
		labL > kappa * epsilon    ? Math.pow((labL + 16) / 116, 3) : labL / kappa,
		Math.pow(f3, 3) > epsilon ? Math.pow(f3, 3)                : (116 * f3 - 16) / kappa
	];

	const [ xyzX, xyzY, xyzZ ] = matrix(
		// compute XYZ by scaling pre-scaled XYZ by reference white
		[ initX * wd50X, initY * wd50Y, initZ * wd50Z ],
		// calculate D65 XYZ from D50 XYZ
		[
			[ 0.9555766, -0.0230393,  0.0631636],
			[-0.0282895,  1.0099416,  0.0210077],
			[ 0.0122982, -0.0204830,  1.3299098]
		]
	);

	return [ xyzX, xyzY, xyzZ ];
}

export function xyz2lab(xyzX, xyzY, xyzZ) {
	// calculate D50 XYZ from D65 XYZ
	const [ d50X, d50Y, d50Z ] = matrix([ xyzX, xyzY, xyzZ ], [
		[ 1.0478112,  0.0228866, -0.0501270],
		[ 0.0295424,  0.9904844, -0.0170491],
		[-0.0092345,  0.0150436,  0.7521316]
	]);

	// calculate f
	const [ f1, f2, f3 ] = [
		d50X / wd50X,
		d50Y / wd50Y,
		d50Z / wd50Z
	].map(
		value => value > epsilon ? Math.cbrt(value) : (kappa * value + 16) / 116
	);

	const [ labL, labA, labB ] = [
		116 * f2 - 16,
		500 * (f1 - f2),
		200 * (f2 - f3)
	];

	return [ labL, labA, labB ];
}

/*

References
----------

- https://www.w3.org/TR/css-color-4/#rgb-to-lab
- https://www.w3.org/TR/css-color-4/#color-conversion-code
- https://www.easyrgb.com/en/math.php

/* ========================================================================== */

Version data entries

26 entries across 25 versions & 8 rubygems

Version Path
optimacms-0.1.61 spec/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.18.0 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.18.2 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.16.1 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.15.2 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.18.4 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.18.1 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.12.7.pre.puma.pre.3 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.14.0 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
disco_app-0.13.6.pre.puma.pre.3 test/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
tang-0.2.1 spec/tang_app/node_modules/@csstools/convert-colors/lib/lab-xyz.js
groonga-client-model-6.0.0 test/apps/rails6.0.3.5/node_modules/@csstools/convert-colors/lib/lab-xyz.js
groonga-client-model-6.0.0 test/apps/rails6.1.3/node_modules/@csstools/convert-colors/lib/lab-xyz.js
ruby2js-4.0.4 lib/tasks/testrails/node_modules/@csstools/convert-colors/lib/lab-xyz.js
ruby2js-4.0.3 lib/tasks/testrails/node_modules/@csstools/convert-colors/lib/lab-xyz.js
tang-0.2.0 spec/tang_app/node_modules/@csstools/convert-colors/lib/lab-xyz.js
tang-0.1.0 spec/tang_app/node_modules/@csstools/convert-colors/lib/lab-xyz.js
tang-0.0.9 spec/tang_app/node_modules/@csstools/convert-colors/lib/lab-xyz.js
enju_library-0.3.8 spec/dummy/node_modules/@csstools/convert-colors/lib/lab-xyz.js
jester-data-8.0.0 node_modules/@csstools/convert-colors/lib/lab-xyz.js