Skip to content

Commit

Permalink
feat: Initial conversion to JSDoc types
Browse files Browse the repository at this point in the history
  • Loading branch information
MysteryBlokHed committed May 26, 2024
1 parent f062623 commit 7a82736
Show file tree
Hide file tree
Showing 37 changed files with 1,101 additions and 515 deletions.
99 changes: 57 additions & 42 deletions src/CATs.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import hooks from "./hooks.js";
import {multiplyMatrices} from "./util.js";
import {WHITES} from "./adapt.js";
import { multiplyMatrices } from "./util.js";
import { WHITES } from "./adapt.js";

// Type "imports"
/** @typedef {import("./types.js").White} White */
/** @typedef {import("./types.js").CAT} CAT */

/** @type {Record<string, CAT>} */
export const CATs = {};

hooks.add("chromatic-adaptation-start", env => {
hooks.add("chromatic-adaptation-start", (env) => {
if (env.options.method) {
env.M = adapt(env.W1, env.W2, env.options.method);
}
});

hooks.add("chromatic-adaptation-end", env => {
hooks.add("chromatic-adaptation-end", (env) => {
if (!env.M) {
env.M = adapt(env.W1, env.W2, env.options.method);
}
});

export function defineCAT ({id, toCone_M, fromCone_M}) {
/**
* @param {CAT} param0
*/
export function defineCAT({ id, toCone_M, fromCone_M }) {

Check warning on line 27 in src/CATs.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
// Use id, toCone_M, fromCone_M like variables
CATs[id] = arguments[0];
}

export function adapt (W1, W2, id = "Bradford") {
/**
*
* @param {White} W1
* @param {White} W2
* @param {string} id
* @returns {number[]}
*/
export function adapt(W1, W2, id = "Bradford") {

Check warning on line 39 in src/CATs.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
// adapt from a source whitepoint or illuminant W1
// to a destination whitepoint or illuminant W2,
// using the given chromatic adaptation transform (CAT)
Expand All @@ -33,29 +48,29 @@ export function adapt (W1, W2, id = "Bradford") {

// all practical illuminants have non-zero XYZ so no division by zero can occur below
let scale = [
[ρd / ρs, 0, 0 ],
[0, γd / γs, 0 ],
[0, 0, βd / βs],
[ρd / ρs, 0, 0],
[0, γd / γs, 0],
[0, 0, βd / βs],
];
// console.log({scale});

let scaled_cone_M = multiplyMatrices(scale, method.toCone_M);
let adapt_M = multiplyMatrices(method.fromCone_M, scaled_cone_M);
let adapt_M = multiplyMatrices(method.fromCone_M, scaled_cone_M);
// console.log({scaled_cone_M, adapt_M});
return adapt_M;
}

defineCAT({
id: "von Kries",
toCone_M: [
[ 0.4002400, 0.7076000, -0.0808100 ],
[ -0.2263000, 1.1653200, 0.0457000 ],
[ 0.0000000, 0.0000000, 0.9182200 ],
[0.40024, 0.7076, -0.08081],
[-0.2263, 1.16532, 0.0457],
[0.0, 0.0, 0.91822],
],
fromCone_M: [
[ 1.8599363874558397, -1.1293816185800916, 0.21989740959619328 ],
[ 0.3611914362417676, 0.6388124632850422, -0.000006370596838649899 ],
[ 0, 0, 1.0890636230968613 ],
[1.8599363874558397, -1.1293816185800916, 0.21989740959619328],
[0.3611914362417676, 0.6388124632850422, -0.000006370596838649899],
[0, 0, 1.0890636230968613],
],
});

Expand All @@ -64,68 +79,68 @@ defineCAT({
// Convert an array of XYZ values in the range 0.0 - 1.0
// to cone fundamentals
toCone_M: [
[ 0.8951000, 0.2664000, -0.1614000 ],
[ -0.7502000, 1.7135000, 0.0367000 ],
[ 0.0389000, -0.0685000, 1.0296000 ],
[0.8951, 0.2664, -0.1614],
[-0.7502, 1.7135, 0.0367],
[0.0389, -0.0685, 1.0296],
],
// and back
fromCone_M: [
[ 0.9869929054667121, -0.14705425642099013, 0.15996265166373122 ],
[ 0.4323052697233945, 0.5183602715367774, 0.049291228212855594 ],
[ -0.00852866457517732, 0.04004282165408486, 0.96848669578755 ],
[0.9869929054667121, -0.14705425642099013, 0.15996265166373122],
[0.4323052697233945, 0.5183602715367774, 0.049291228212855594],
[-0.00852866457517732, 0.04004282165408486, 0.96848669578755],
],
});

defineCAT({
id: "CAT02",
// with complete chromatic adaptation to W2, so D = 1.0
toCone_M: [
[ 0.7328000, 0.4296000, -0.1624000 ],
[ -0.7036000, 1.6975000, 0.0061000 ],
[ 0.0030000, 0.0136000, 0.9834000 ],
[0.7328, 0.4296, -0.1624],
[-0.7036, 1.6975, 0.0061],
[0.003, 0.0136, 0.9834],
],
fromCone_M: [
[ 1.0961238208355142, -0.27886900021828726, 0.18274517938277307 ],
[ 0.4543690419753592, 0.4735331543074117, 0.07209780371722911 ],
[ -0.009627608738429355, -0.00569803121611342, 1.0153256399545427 ],
[1.0961238208355142, -0.27886900021828726, 0.18274517938277307],
[0.4543690419753592, 0.4735331543074117, 0.07209780371722911],
[-0.009627608738429355, -0.00569803121611342, 1.0153256399545427],
],
});

defineCAT({
id: "CAT16",
toCone_M: [
[ 0.401288, 0.650173, -0.051461 ],
[ -0.250268, 1.204414, 0.045854 ],
[ -0.002079, 0.048952, 0.953127 ],
[0.401288, 0.650173, -0.051461],
[-0.250268, 1.204414, 0.045854],
[-0.002079, 0.048952, 0.953127],
],
// the extra precision is needed to avoid roundtripping errors
fromCone_M: [
[ 1.862067855087233, -1.0112546305316845, 0.14918677544445172 ],
[ 0.3875265432361372, 0.6214474419314753, -0.008973985167612521 ],
[ -0.01584149884933386, -0.03412293802851557, 1.0499644368778496 ],
[1.862067855087233, -1.0112546305316845, 0.14918677544445172],
[0.3875265432361372, 0.6214474419314753, -0.008973985167612521],
[-0.01584149884933386, -0.03412293802851557, 1.0499644368778496],
],
});

Object.assign(WHITES, {
// whitepoint values from ASTM E308-01 with 10nm spacing, 1931 2 degree observer
// all normalized to Y (luminance) = 1.00000
// Illuminant A is a tungsten electric light, giving a very warm, orange light.
A: [1.09850, 1.00000, 0.35585],
A: [1.0985, 1.0, 0.35585],

// Illuminant C was an early approximation to daylight: illuminant A with a blue filter.
C: [0.98074, 1.000000, 1.18232],
C: [0.98074, 1.0, 1.18232],

// The daylight series of illuminants simulate natural daylight.
// The color temperature (in degrees Kelvin/100) ranges from
// cool, overcast daylight (D50) to bright, direct sunlight (D65).
D55: [0.95682, 1.00000, 0.92149],
D75: [0.94972, 1.00000, 1.22638],
D55: [0.95682, 1.0, 0.92149],
D75: [0.94972, 1.0, 1.22638],

// Equal-energy illuminant, used in two-stage CAT16
E: [1.00000, 1.00000, 1.00000],
E: [1.0, 1.0, 1.0],

// The F series of illuminants represent fluorescent lights
F2: [0.99186, 1.00000, 0.67393],
F7: [0.95041, 1.00000, 1.08747],
F11: [1.00962, 1.00000, 0.64350],
F2: [0.99186, 1.0, 0.67393],
F7: [0.95041, 1.0, 1.08747],
F11: [1.00962, 1.0, 0.6435],
});
59 changes: 39 additions & 20 deletions src/adapt.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,70 @@
import hooks from "./hooks.js";
import {multiplyMatrices} from "./util.js";
import { multiplyMatrices } from "./util.js";

// Type "imports"
/** @typedef {import("./types.js").White} White */

/** @type {Record<string, White>} */
export const WHITES = {
// for compatibility, the four-digit chromaticity-derived ones everyone else uses
D50: [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585],
D65: [0.3127 / 0.3290, 1.00000, (1.0 - 0.3127 - 0.3290) / 0.3290],
D50: [0.3457 / 0.3585, 1.0, (1.0 - 0.3457 - 0.3585) / 0.3585],
D65: [0.3127 / 0.329, 1.0, (1.0 - 0.3127 - 0.329) / 0.329],
};

export function getWhite (name) {
/**
*
* @param {string | White} name
* @returns {White}
*/
export function getWhite(name) {

Check warning on line 19 in src/adapt.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
if (Array.isArray(name)) {
return name;
}

return WHITES[name];
}

// Adapt XYZ from white point W1 to W2
export default function adapt (W1, W2, XYZ, options = {}) {
/**
* Adapt XYZ from white point W1 to W2
* @param {White} W1
* @param {White} W2
* @param {[number, number, number]} XYZ
* @param {{ method?: string | undefined }} options
* @returns {[number, number, number]}
*/
export default function adapt(W1, W2, XYZ, options = {}) {

Check warning on line 35 in src/adapt.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
W1 = getWhite(W1);
W2 = getWhite(W2);

if (!W1 || !W2) {
throw new TypeError(`Missing white point to convert ${!W1 ? "from" : ""}${!W1 && !W2 ? "/" : ""}${!W2 ? "to" : ""}`);
throw new TypeError(
`Missing white point to convert ${!W1 ? "from" : ""}${
!W1 && !W2 ? "/" : ""
}${!W2 ? "to" : ""}`

Check warning on line 43 in src/adapt.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing trailing comma
);
}

if (W1 === W2) {
// Same whitepoints, no conversion needed
return XYZ;
}

let env = {W1, W2, XYZ, options};
let env = { W1, W2, XYZ, options };

hooks.run("chromatic-adaptation-start", env);

if (!env.M) {
if (env.W1 === WHITES.D65 && env.W2 === WHITES.D50) {
env.M = [
[ 1.0479297925449969, 0.022946870601609652, -0.05019226628920524 ],
[ 0.02962780877005599, 0.9904344267538799, -0.017073799063418826 ],
[ -0.009243040646204504, 0.015055191490298152, 0.7518742814281371 ],
[1.0479297925449969, 0.022946870601609652, -0.05019226628920524],
[0.02962780877005599, 0.9904344267538799, -0.017073799063418826],
[-0.009243040646204504, 0.015055191490298152, 0.7518742814281371],
];
}
else if (env.W1 === WHITES.D50 && env.W2 === WHITES.D65) {

} else if (env.W1 === WHITES.D50 && env.W2 === WHITES.D65) {

Check warning on line 63 in src/adapt.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Closing curly brace appears on the same line as the subsequent block
env.M = [
[ 0.955473421488075, -0.02309845494876471, 0.06325924320057072 ],
[ -0.0283697093338637, 1.0099953980813041, 0.021041441191917323 ],
[ 0.012314014864481998, -0.020507649298898964, 1.330365926242124 ],
[0.955473421488075, -0.02309845494876471, 0.06325924320057072],
[-0.0283697093338637, 1.0099953980813041, 0.021041441191917323],
[0.012314014864481998, -0.020507649298898964, 1.330365926242124],
];
}
}
Expand All @@ -55,8 +73,9 @@ export default function adapt (W1, W2, XYZ, options = {}) {

if (env.M) {
return multiplyMatrices(env.M, env.XYZ);
}
else {
throw new TypeError("Only Bradford CAT with white points D50 and D65 supported for now.");
} else {

Check warning on line 76 in src/adapt.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Closing curly brace appears on the same line as the subsequent block
throw new TypeError(
"Only Bradford CAT with white points D50 and D65 supported for now."

Check warning on line 78 in src/adapt.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing trailing comma
);
}
}
35 changes: 19 additions & 16 deletions src/angles.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import * as util from "./util.js";

export function constrain (angle) {
/**
* Constrain an angle to 360 degrees
* @param {number} angle
* @returns {number}
*/
export function constrain(angle) {

Check warning on line 8 in src/angles.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
if (typeof angle !== "number") {
return angle;
}

return ((angle % 360) + 360) % 360;
}

export function adjust (arc, angles) {
/**
* @param {"raw" | "increasing" | "decreasing" | "longer" | "shorter"} arc
* @param {[number, number]} angles
* @returns {[number, number]}
*/
export function adjust(arc, angles) {

Check warning on line 21 in src/angles.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing space before function parentheses
let [a1, a2] = angles;

let none1 = util.isNone(a1);
let none2 = util.isNone(a2);

if (none1 && none2) {
return [a1, a2];
}
else if (none1) {
} else if (none1) {
a1 = a2;
}
else if (none2) {
} else if (none2) {
a2 = a1;
}

Expand All @@ -37,27 +45,22 @@ export function adjust (arc, angles) {
if (angleDiff < 0) {
a2 += 360;
}
}
else if (arc === "decreasing") {
} else if (arc === "decreasing") {
if (angleDiff > 0) {
a1 += 360;
}
}
else if (arc === "longer") {
} else if (arc === "longer") {
if (-180 < angleDiff && angleDiff < 180) {
if (angleDiff > 0) {
a1 += 360;
}
else {
} else {
a2 += 360;
}
}
}
else if (arc === "shorter") {
} else if (arc === "shorter") {
if (angleDiff > 180) {
a1 += 360;
}
else if (angleDiff < -180) {
} else if (angleDiff < -180) {
a2 += 360;
}
}
Expand Down
Loading

0 comments on commit 7a82736

Please sign in to comment.