Published
Edited
Jul 18, 2019
Importers
8 stars
Insert cell
Insert cell
Insert cell
Insert cell
gamma = (x) =>
(x > 0.0031308) * (1.055 * Math.pow(x, 0.4166666666666667) - 0.055) +
(x <= 0.0031308) * 12.92 * x
Insert cell
xyz_to_srgb = function xyz_to_srgb([X, Y, Z]) {
return [
gamma(+ 0.03241003232976359 *X - 0.015373989694887858*Y - 0.004986158819963629 *Z),
gamma(- 0.009692242522025166 *X + 0.01875929983695176 *Y + 0.00041554226340084706*Z),
gamma(+ 0.0005563941985197545*X - 0.0020401120612391 *Y + 0.010571489771875336 *Z)]; }
Insert cell
Insert cell
gamma_inverse = (x) =>
(x > 0.04045) * Math.pow((x + 0.055) * 0.9478672985781991, 2.4) +
(x <= 0.04045) * 0.07739938080495357 * x
Insert cell
srgb_to_xyz = function rgb_to_xyz([R, G, B]) {
R = gamma_inverse(R), G = gamma_inverse(G), B = gamma_inverse(B);
return [
41.23865632529916 *R + 35.75914909206253 *G + 18.045049120356364*B,
21.26368216773238 *R + 71.51829818412506 *G + 7.218019648142546*B,
1.9330620152483982*R + 11.919716364020843*G + 95.03725870054352 *B]; }
Insert cell
Insert cell
Insert cell
xyz_to_rgb709 = function xyz_to_rgb709([X, Y, Z]) {
return [
+ 0.03241003232976359 *X - 0.015373989694887858*Y - 0.004986158819963629 *Z,
- 0.009692242522025166 *X + 0.01875929983695176 *Y + 0.00041554226340084706*Z,
+ 0.0005563941985197545*X - 0.0020401120612391 *Y + 0.010571489771875336 *Z]; }
Insert cell
rgb709_to_xyz = function rgb709_to_xyz([R, G, B]) {
return [
41.23865632529916 *R + 35.75914909206253 *G + 18.045049120356364*B,
21.26368216773238 *R + 71.51829818412506 *G + 7.218019648142546*B,
1.9330620152483982*R + 11.919716364020843*G + 95.03725870054352 *B]; }
Insert cell
Insert cell
in_gamut = function in_gamut([X, Y, Z]) {
const
R = + 0.03241003232976359 *X - 0.015373989694887858*Y - 0.004986158819963629 *Z,
G = - 0.009692242522025166 *X + 0.01875929983695176 *Y + 0.00041554226340084706*Z,
B = + 0.0005563941985197545*X - 0.0020401120612391 *Y + 0.010571489771875336 *Z;
return !!(R<=1 & R>=0 & G<=1 & G>=0 & B<=1 & B>=0);
}
Insert cell
Insert cell
to_hex = function to_hex([R, G, B]) {
R = Math.round(0xff * R), G = Math.round(0xff * G), B = Math.round(0xff * B);
if (Math.max(R, G, B) > 255 || Math.min(R, G, B) < 0) {
throw new Error('Bad Input: R, G, and B must be in range [0, 1]'); }
return '#' + (1 << 24 | R << 16 | G << 8 | B).toString(16).slice(1);
}
Insert cell
int8_to_hex = function int8_to_hex([R, G, B]) {
R = Math.round(R), G = Math.round(G), B = Math.round(B);
if (Math.max(R, G, B) > 255 || Math.min(R, G, B) < 0) {
throw new Error('Bad Input: R, G, and B must be in range [0, 255]'); }
return '#' + (1 << 24 | R << 16 | G << 8 | B).toString(16).slice(1);
}
Insert cell
from_hex = function from_hex(hex) {
if (!/^#?[0-9a-fA-F]{6}$/.test(hex)) {
throw new Error('Bad Input: Must be of form "666FAD" or "#DEFACE"'); }
const RGB = parseInt(hex.substr(-6), 16);
return [
(RGB >> 16) / 0xff, // first byte -> R
(RGB >> 8 & 0xff) / 0xff, // second byte -> G
(RGB & 0xff) / 0xff]; // third byte -> B
}
Insert cell
int8_from_hex = function from_hex(hex) {
if (!/^#?[0-9a-fA-F]{6}$/.test(hex)) {
throw new Error('Bad Input: Must be of form "666FAD" or "#DEFACE"'); }
const RGB = parseInt(hex.substr(-6), 16);
return [
(RGB >> 16), // first byte -> R
(RGB >> 8 & 0xff), // second byte -> G
(RGB & 0xff)]; // third byte -> B
}
Insert cell
Insert cell
chromaticities = ({
r: [0.64, 0.33, 0.03],
g: [0.30, 0.60, 0.10],
b: [0.15, 0.06, 0.79]
})
Insert cell
XYZ_D65 = [0.31271, 0.32902, 0.35827].map(X=> X/0.32902*100)
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more