Published
Edited
Dec 12, 2018
7 stars
Insert cell
Insert cell
Insert cell
Insert cell
function rgb_jch(rgb) {
return cat02_jch(xyz_cat02(rgb_xyz(rgb)));
}
Insert cell
function jch_rgb(jch) {
return xyz_rgb(cat02_xyz(jch_cat02(jch)));
}
Insert cell
function rgb_xyz(rgb) {
return matrix_multiply_vector(matrix_lrgb_xyzd65, rgb_lrgb(rgb));
}
Insert cell
function xyz_rgb(xyz) {
return lrgb_rgb(matrix_multiply_vector(matrix_xyzd65_lrgb, xyz));
}
Insert cell
function xyz_cat02([x, y, z]) {
return [
( 0.7328 * x) + (0.4296 * y) - (0.1624 * z),
(-0.7036 * x) + (1.6975 * y) + (0.0061 * z),
( 0.0030 * x) + (0.0136 * y) + (0.9834 * z)
];
}
Insert cell
function cat02_xyz([l, m, s]) {
return [
( 1.096124 * l) - (0.278869 * m) + (0.182745 * s),
( 0.454369 * l) + (0.473533 * m) + (0.072098 * s),
(-0.009628 * l) - (0.005698 * m) + (1.015326 * s)
];
}
Insert cell
function cat02_hpe([l, m, s]) {
return [
( 0.7409792 * l) + (0.2180250 * m) + (0.0410058 * s),
( 0.2853532 * l) + (0.6242014 * m) + (0.0904454 * s),
(-0.0096280 * l) - (0.0056980 * m) + (1.0153260 * s)
];
}
Insert cell
function hpe_xyz([l, m, s]) {
return [
(1.910197 * l) - (1.112124 * m) + (0.201908 * s),
(0.370950 * l) + (0.629054 * m) - (0.000008 * s),
s
];
}
Insert cell
function aab_lms(A, aa, bb, nbb) {
const x = (A / nbb) + 0.305;
return [
(0.32787 * x) + (0.32145 * aa) + (0.20527 * bb),
(0.32787 * x) - (0.63507 * aa) - (0.18603 * bb),
(0.32787 * x) - (0.15681 * aa) - (4.49038 * bb)
];
}
Insert cell
function adapt(lms) {
return lms.map((c, i) => {
const p = Math.pow((CIECAM02_fl * c) / 100, 0.42);
return ((400 * p) / (27.13 + p)) + 0.1;
});
}
Insert cell
function invert_adapt(lms) {
return lms.map((c, i) => {
return (100 / CIECAM02_fl) * Math.pow((27.13 * Math.abs(c - 0.1)) / (400 - Math.abs(c - 0.1)), 1 / 0.42);
});
}
Insert cell
function transform(lms) {
return lms.map((c, i) => {
return c * (((D65_Y * CIECAM02_d) / D65_CAT02[i]) + (1 - CIECAM02_d));
});
}
Insert cell
function invert_transform(lms) {
return lms.map((c, i) => {
return c / (((D65_Y * CIECAM02_d) / D65_CAT02[i]) + (1 - CIECAM02_d));
});
}
Insert cell
function cat02_jch(lms) {
const lms_c = transform(lms);
const lms_p = cat02_hpe(lms_c);
const [lpa, mpa, spa] = adapt(lms_p);
const ca = lpa - ((12 * mpa) / 11) + (spa / 11);
const cb = (1 / 9) * (lpa + mpa - 2 * spa);
let h = (180 / Math.PI) * Math.atan2(cb, ca);
if (h < 0) h += 360;
const a = (2 * lpa + mpa + 0.05 * spa - 0.305 ) * CIECAM02_nbb;
const J = 100 * Math.pow(a / CIECAM02_aw, CIECAM02_c * CIECAM02_z);
const et = 0.25 * (Math.cos((h * Math.PI) / 180 + 2) + 3.8);
const t = ((50000 / 13) * CIECAM02_nc * CIECAM02_ncb * et * Math.sqrt(ca * ca + cb * cb)) / (lpa + mpa + (21 / 20) * spa);
const C = Math.pow(t, 0.9) * Math.sqrt(J / 100) * Math.pow(1.64 - Math.pow(0.29, CIECAM02_n), 0.73);
return [J, C, h];
}
Insert cell
function jch_cat02([J, C, h]) {
const t = Math.pow(C / (Math.sqrt(J / 100) * Math.pow(1.64 - Math.pow(0.29, CIECAM02_n), 0.73)), 1 / 0.9);
const et = 1 / 4 * (Math.cos(((h * Math.PI) / 180) + 2) + 3.8);
const a = Math.pow(J / 100, 1 / (CIECAM02_c * CIECAM02_z)) * CIECAM02_aw;
const p1 = ((50000 / 13) * CIECAM02_nc * CIECAM02_ncb) * et / t;
const p2 = (a / CIECAM02_nbb) + 0.305;
const p3 = 21 / 20;
let ca, cb;
const hr = (h * Math.PI) / 180;
const sr = Math.sin(hr);
const cr = Math.cos(hr);
if (Math.abs(sr) >= Math.abs(cr)) {
cb = (p2 * (2 + p3) * (460 / 1403)) / (p1 / sr + (2 + p3) * (220 / 1403) * (Math.cos(hr) / Math.sin(hr)) - (27 / 1403) + p3 * (6300 / 1403));
ca = cb * (Math.cos(hr) / Math.sin(hr));
} else {
ca = (p2 * (2 + p3) * (460 / 1403)) / (p1 / cr + (2 + p3) * (220 / 1403) - ((27 / 1403) - p3 * (6300 / 1403)) * (Math.sin(hr) / Math.cos(hr)));
cb = ca * (Math.sin(hr) / Math.cos(hr));
}
const lms_a = aab_lms(a, ca, cb, CIECAM02_nbb);
const lms_p = invert_adapt(lms_a);
const txyz = hpe_xyz(lms_p);
const lms_c = xyz_cat02(txyz);
return invert_transform(lms_c);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more