-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add color space xyz and xyz-d65 to color() function #27
- Loading branch information
Showing
17 changed files
with
395 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// from https://www.w3.org/TR/css-color-4/multiply-matrices.js | ||
/** | ||
* Simple matrix (and vector) multiplication | ||
* Warning: No error handling for incompatible dimensions! | ||
* @author Lea Verou 2020 MIT License | ||
*/ | ||
// A is m x n. B is n x p. product is m x p. | ||
function multiplyMatrices(A, B) { | ||
let m = A.length; | ||
if (!Array.isArray(A[0])) { | ||
// A is vector, convert to [[a, b, c, ...]] | ||
A = [A]; | ||
} | ||
if (!Array.isArray(B[0])) { | ||
// B is vector, convert to [[a], [b], [c], ...]] | ||
B = B.map((x) => [x]); | ||
} | ||
let p = B[0].length; | ||
let B_cols = B[0].map((_, i) => B.map((x) => x[i])); // transpose B | ||
let product = A.map((row) => B_cols.map((col) => { | ||
if (!Array.isArray(row)) { | ||
return col.reduce((a, c) => a + c * row, 0); | ||
} | ||
return row.reduce((a, c, i) => a + c * (col[i] || 0), 0); | ||
})); | ||
if (m === 1) { | ||
product = product[0]; // Avoid [[a, b, c, ...]] | ||
} | ||
if (p === 1) { | ||
return product.map((x) => x[0]); // Avoid [[a], [b], [c], ...]] | ||
} | ||
return product; | ||
} | ||
|
||
export { multiplyMatrices }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function roundWithPrecision(value, original) { | ||
return +value.toFixed(original.toString().split('.')[1]?.length ?? 0); | ||
} | ||
|
||
export { roundWithPrecision }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { multiplyMatrices } from './utils/matrix.js'; | ||
import { roundWithPrecision } from './utils/round.js'; | ||
import { gam_sRGB } from './rgb.js'; | ||
|
||
function XYZ_to_sRGB(x, y, z) { | ||
// @ts-ignore | ||
return gam_sRGB(...XYZ_to_lin_sRGB(x, y, z)); | ||
} | ||
function XYZ_to_lin_sRGB(x, y, z) { | ||
// convert XYZ to linear-light sRGB | ||
const M = [ | ||
[12831 / 3959, -329 / 214, -1974 / 3959], | ||
[-851781 / 878810, 1648619 / 878810, 36519 / 878810], | ||
[705 / 12673, -2585 / 12673, 705 / 667], | ||
]; | ||
const XYZ = [x, y, z]; // convert to XYZ | ||
return multiplyMatrices(M, XYZ).map((v, index) => roundWithPrecision(v, XYZ[index])); | ||
} | ||
function XYZ_D50_to_sRGB(x, y, z) { | ||
// @ts-ignore | ||
return gam_sRGB(...XYZ_to_lin_sRGB(...D50_to_D65(x, y, z))); | ||
} | ||
function D50_to_D65(x, y, z) { | ||
// Bradford chromatic adaptation from D50 to D65 | ||
const M = [ | ||
[0.9554734527042182, -0.023098536874261423, 0.0632593086610217], | ||
[-0.028369706963208136, 1.0099954580058226, 0.021041398966943008], | ||
[0.012314001688319899, -0.020507696433477912, 1.3303659366080753] | ||
]; | ||
const XYZ = [x, y, z]; | ||
return multiplyMatrices(M, XYZ).map((v, index) => roundWithPrecision(v, XYZ[index])); | ||
} | ||
|
||
export { D50_to_D65, XYZ_D50_to_sRGB, XYZ_to_lin_sRGB, XYZ_to_sRGB }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.