Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions packages/color-helpers/dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function multiplyMatrices(_,t){return[_[0]*t[0]+_[1]*t[1]+_[2]*t[2],_[3]*t[0]+_[
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
* @see http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
*/function Lab_to_XYZ(_){const t=24389/27,o=216/24389,e=(_[0]+16)/116,r=_[1]/500+e,a=e-_[2]/200;return[(Math.pow(r,3)>o?Math.pow(r,3):(116*r-16)/t)*n[0],(_[0]>8?Math.pow((_[0]+16)/116,3):_[0]/t)*n[1],(Math.pow(a,3)>o?Math.pow(a,3):(116*a-16)/t)*n[2]]}
*/function Lab_to_XYZ(_){const t=24389/27,o=216/24389,e=_[0]/116,r=_[1]/500,a=_[2]/200,i=e+16/116,l=i+r,u=i-a;return[(Math.pow(l,3)>o?Math.pow(l,3):(e+r)*(116/t))*n[0],(_[0]>8?Math.pow((_[0]+16)/116,3):_[0]/t)*n[1],(Math.pow(u,3)>o?Math.pow(u,3):116/t*(e-a))*n[2]]}
/**
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
Expand Down Expand Up @@ -108,8 +108,11 @@ function XYZ_to_OKLab(_){const t=multiplyMatrices(i,_);return multiplyMatrices(l
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
*/function XYZ_to_lin_sRGB(_){return multiplyMatrices(Y,_)}
/**
* Convert an array of linear-light rec2020 RGB in the range 0.0-1.0
* to gamma corrected form ITU-R BT.2020-2 p.4
* Convert an array of linear-light rec2020 RGB in the range 0.0-1.0
* to gamma corrected form.
* Reference electro-optical transfer function from Rec. ITU-R BT.1886 Annex 1
* with b (black lift) = 0 and a (user gain) = 1
* defined over the extended range, not clamped
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
Expand Down Expand Up @@ -149,7 +152,9 @@ function XYZ_to_OKLab(_){const t=multiplyMatrices(i,_);return multiplyMatrices(l
/**
* Convert an array of rec2020 RGB values in the range 0.0 - 1.0
* to linear light (un-companded) form.
* ITU-R BT.2020-2 p.4
* Reference electro-optical transfer function from Rec. ITU-R BT.1886 Annex 1
* with b (black lift) = 0 and a (user gain) = 1
* defined over the extended range, not clamped
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
Expand Down
2 changes: 1 addition & 1 deletion packages/color-helpers/scripts/hashes.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
"384d52db3fcd98381e18743e1eba590a",
"a69bf3dc21cfcc876c80ee311ca85f90",
"00493f00572bf1f5c2303f948422c1c7",
"094c9859b0960c4e394947cc4832b54f",
"c9e2f2a3b2cba543a01cb8aa5d77c04a",
"7aba804ac76ce5d733956b4251194344",
Expand Down
7 changes: 5 additions & 2 deletions packages/color-helpers/src/conversions/gam-2020.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { Color } from '../types/color';

/**
* Convert an array of linear-light rec2020 RGB in the range 0.0-1.0
* to gamma corrected form ITU-R BT.2020-2 p.4
* Convert an array of linear-light rec2020 RGB in the range 0.0-1.0
* to gamma corrected form.
* Reference electro-optical transfer function from Rec. ITU-R BT.1886 Annex 1
* with b (black lift) = 0 and a (user gain) = 1
* defined over the extended range, not clamped
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
Expand Down
15 changes: 10 additions & 5 deletions packages/color-helpers/src/conversions/lab-to-xyz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ export function Lab_to_XYZ(Lab: Color): Color {
const κ = 24389 / 27; // 29^3/3^3
const ε = 216 / 24389; // 6^3/29^3

// Keep L out of L+16 for near-black precision
const lightness116 = Lab[0] / 116;
const a500 = Lab[1] / 500;
const b200 = Lab[2] / 200;

// compute f, starting with the luminance-related term
const f1 = (Lab[0] + 16) / 116;
const f0 = Lab[1] / 500 + f1;
const f2 = f1 - Lab[2] / 200;
const f1 = lightness116 + (16 / 116);
const f0 = f1 + a500;
const f2 = f1 - b200;

// compute xyz
return [
(Math.pow(f0, 3) > ε ? Math.pow(f0, 3) : (116 * f0 - 16) / κ) * D50[0],
(Math.pow(f0, 3) > ε ? Math.pow(f0, 3) : (lightness116 + a500) * (116 / κ)) * D50[0],
(Lab[0] > κ * ε ? Math.pow((Lab[0] + 16) / 116, 3) : Lab[0] / κ) * D50[1],
(Math.pow(f2, 3) > ε ? Math.pow(f2, 3) : (116 * f2 - 16) / κ) * D50[2],
(Math.pow(f2, 3) > ε ? Math.pow(f2, 3) : (lightness116 - b200) * (116 / κ)) * D50[2],
];
}
4 changes: 3 additions & 1 deletion packages/color-helpers/src/conversions/lin-2020.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type { Color } from '../types/color';
/**
* Convert an array of rec2020 RGB values in the range 0.0 - 1.0
* to linear light (un-companded) form.
* ITU-R BT.2020-2 p.4
* Reference electro-optical transfer function from Rec. ITU-R BT.1886 Annex 1
* with b (black lift) = 0 and a (user gain) = 1
* defined over the extended range, not clamped
*
* @license W3C https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
* @copyright This software or document includes material copied from or derived from https://github.com/w3c/csswg-drafts/blob/main/css-color-4/conversions.js. Copyright © 2022 W3C® (MIT, ERCIM, Keio, Beihang).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from 'node:assert';
import { reducePrecisionWholeValue } from '../util/serialize.mjs';
import { color, computedValue } from '@csstools/css-color-parser';
import { parse } from '../util/parse.mjs';

{
const tests = [
['lch(from color(display-p3 0.1893689 0.18937814 0.18937561) l c h)', 'lch(19.99999973 0 none)'],
['color-mix(in lch, color(display-p3 0.1893689 0.18937814 0.18937561), lch(11 33 44))', 'lch(15.49999987 16.5 44)'],
['color-mix(in lch, color(display-p3 0.1893689 0.18937814 0.18937561))', 'lch(19.99999973 0 none)'],
['oklch(from color(display-p3 0.08610937692934956 0.08610188202042188 0.08610387292083356) l c h)', 'oklch(0.2 0.000004 359.99999992)'],
['color-mix(in oklch, color(display-p3 0.08610937692934956 0.08610188202042188 0.08610387292083356), oklch(0.11 0.33 0.44))', 'oklch(0.155 0.165002 0.21999996)'],
['color-mix(in oklch, color(display-p3 0.08610937692934956 0.08610188202042188 0.08610387292083356))', 'oklch(0.2 0.000004 359.99999992)'],
];

for (const test of tests) {
assert.deepStrictEqual(
reducePrecisionWholeValue(computedValue(color(parse(test[0])))),
test[1],
`"${test[0]}" : ${test[1]}`,
);
}
}
Loading
Loading