Skip to content

Commit

Permalink
Merge pull request #58 from ahocevar/typed
Browse files Browse the repository at this point in the history
Generate types from JSDoc annotations
  • Loading branch information
dy authored Jan 19, 2025
2 parents ca51544 + c9c45f4 commit 557cd51
Show file tree
Hide file tree
Showing 44 changed files with 941 additions and 716 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Scripting_*
docs
*.pdf
*.pak
dist


design
Expand Down
13 changes: 7 additions & 6 deletions cmy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import rgb from './rgb.js';

/** @type {Partial<import('./index.js').ColorSpace>} */
var cmy = {
name: 'cmy',
min: [0,0,0],
Expand All @@ -15,9 +16,9 @@ var cmy = {
/**
* CMY to RGB
*
* @param {Array} cmy Channels
* @param {Array<number>} cmy Channels
*
* @return {Array} RGB channels
* @return {Array<number>} RGB channels
*/
cmy.rgb = function(cmy) {
var c = cmy[0] / 100,
Expand All @@ -35,9 +36,9 @@ cmy.rgb = function(cmy) {
/**
* RGB to CMY
*
* @param {Array} rgb channels
* @param {Array<number>} rgb channels
*
* @return {Array} CMY channels
* @return {Array<number>} CMY channels
*/
rgb.cmy = function(rgb) {
var r = rgb[0] / 255,
Expand All @@ -49,6 +50,6 @@ rgb.cmy = function(rgb) {
(1-g) * 100 || 0,
(1-b) * 100 || 0
];
};
}

export default cmy
export default /** @type {import('./index.js').ColorSpace} */ (cmy);
3 changes: 2 additions & 1 deletion cmyk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import rgb from './rgb.js';

/** @type {Partial<import('./index.js').ColorSpace>} */
const cmyk = {
name: 'cmyk',
min: [0,0,0,0],
Expand Down Expand Up @@ -39,4 +40,4 @@ rgb.cmyk = function(rgb) {
return [c * 100, m * 100, y * 100, k * 100];
};

export default cmyk
export default /** @type {import('./index.js').ColorSpace} */ (cmyk);
219 changes: 105 additions & 114 deletions coloroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import xyy from './xyy.js';
import xyz from './xyz.js';

/** @typedef {{table: Array<Array<number>>}} ColoroidSpecific */

/**
* Main color space object
*/
/* @type {Partial<import('./index.js').ColorSpace> & {xyy: import('./index.js').Transform} & ColoroidSpecific}
*/
var coloroid = {
name: 'coloroid',
alias: ['ATV'],
Expand All @@ -22,70 +24,106 @@ var coloroid = {
//e.g. 38 will be rounded to 36
channel: ['A','T','V'],
min: [10, 0, 0],
max: [76, 100, 100]
};
max: [76, 100, 100],
/**
* Coloroid table
* Regression of values is almost impossible, as hues don’t correlate
* Even angle values are picked very inconsistently, based on aesthetical evaluation.
*
* - tgф, ctgф are removed, ф is searched instead
* - eλ = xλ + yλ + zλ
* - λ is removed as not used
*/
table: [
//A angle eλ xλ yλ
[ 10, 59.0, 1.724349, 0.44987, 0.53641 ],
[ 11, 55.3, 1.740844, 0.46248, 0.52444 ],
[ 12, 51.7, 1.754985, 0.47451, 0.51298 ],
[ 13, 48.2, 1.767087, 0.48601, 0.50325 ],
[ 14, 44.8, 1.775953, 0.49578, 0.49052 ],
[ 15, 41.5, 1.785073, 0.50790, 0.43035 ],
[ 16, 38.2, 1.791104, 0.51874, 0.46934 ],
[ 20, 34.9, 1.794831, 0.52980, 0.45783 ],
[ 21, 31.5, 1.798664, 0.54137, 0.44559 ],
[ 22, 28.0, 1.794819, 0.55367, 0.43253 ],
[ 23, 24.4, 1.789610, 0.56680, 0.41811 ],
[ 24, 20.6, 1.809483, 0.58128, 0.40176 ],
[ 25, 16.6, 1.760983, 0.59766, 0.38300 ],
[ 26, 12.3, 1.723443, 0.61653, 0.36061 ],
[ 30, 7.7, 1.652891, 0.63896, 0.33358 ],
[ 31, 2.8, 1.502607, 0.66619, 0.29930 ],
[ 32, -2.5, 1.072500, 0.70061, 0.26753 ],
[ 33, -8.4, 1.136637, 0.63925, 0.22631 ],
[ 34, -19.8, 1.232286, 0.53962, 0.19721 ],
[ 35, -31.6, 1.310120, 0.50340, 0.17495 ],
[ 40, -43.2, 1.376610, 0.46041, 0.15603 ],
[ 41, -54.6, 1.438692, 0.42386, 0.13846 ],
[ 42, -65.8, 1.501582, 0.38991, 0.12083 ],
[ 43, -76.8, 1.570447, 0.35586, 0.10328 ],
[ 44, -86.8, 1.645583, 0.32195, 0.08496 ],
[ 45, -95.8, 1.732083, 0.28657, 0.05155 ],
[ 46, -108.4, 1.915753, 0.22202, 0.01771 ],
[ 50, -117.2, 2.146310, 0.15664, 0.05227 ],
[ 51, -124.7, 1.649939, 0.12736, 0.09020 ],
[ 52, -131.8, 1.273415, 0.10813, 0.12506 ],
[ 53, -138.5, 1.080809, 0.09414, 0.15741 ],
[ 54, -145.1, 0.957076, 0.03249, 0.18958 ],
[ 55, -152.0, 0.868976, 0.07206, 0.24109 ],
[ 56, -163.4, 0.771731, 0.05787, 0.30378 ],
[ 60, -177.2, 0.697108, 0.04353, 0.35696 ],
[ 61, 171.6, 0.655803, 0.03291, 0.41971 ],
[ 62, 152.4, 0.623958, 0.02240, 0.49954 ],
[ 63, 148.4, 0.596037, 0.01196, 0.60321 ],
[ 64, 136.8, 0.607413, 0.00425, 0.73542 ],
[ 65, 125.4, 0.659923, 0.01099, 0.83391 ],
[ 66, 114.2, 0.859517, 0.08050, 0.77474 ],
[ 70, 103.2, 1.195683, 0.20259, 0.70460 ],
[ 71, 93.2, 1.407534, 0.28807, 0.65230 ],
[ 72, 84.2, 1.532829, 0.34422, 0.61930 ],
[ 73, 77.3, 1.603792, 0.37838, 0.59533 ],
[ 74, 71.6, 1.649448, 0.40290, 0.57716 ],
[ 75, 66.9, 1.681080, 0.42141, 0.56222 ],
[ 76, 62.8, 1.704979, 0.43647, 0.54895 ]
],
/**
* Backwise - from coloroid to xyY
*
* @param {Array<number>} arg Coloroid values
*
* @return {Array<number>} xyY values
*/
xyy: function (arg) {
var A = arg[0], T = arg[1], V = arg[2];

//find the closest row in the table
var row;
for (var i = 0; i < table.length; i++) {
if (A <= table[i][0]) {
row = table[i];
break;
}
}

//FIXME row is possibly undefined
//@ts-ignore
var yl = row[4], el = row[2], xl = row[3];

/**
* Coloroid table
* Regression of values is almost impossible, as hues don’t correlate
* Even angle values are picked very inconsistently, based on aesthetical evaluation.
*
* - tgф, ctgф are removed, ф is searched instead
* - eλ = xλ + yλ + zλ
* - λ is removed as not used
*/
coloroid.table = [
//A angle eλ xλ yλ
[ 10, 59.0, 1.724349, 0.44987, 0.53641 ],
[ 11, 55.3, 1.740844, 0.46248, 0.52444 ],
[ 12, 51.7, 1.754985, 0.47451, 0.51298 ],
[ 13, 48.2, 1.767087, 0.48601, 0.50325 ],
[ 14, 44.8, 1.775953, 0.49578, 0.49052 ],
[ 15, 41.5, 1.785073, 0.50790, 0.43035 ],
[ 16, 38.2, 1.791104, 0.51874, 0.46934 ],
[ 20, 34.9, 1.794831, 0.52980, 0.45783 ],
[ 21, 31.5, 1.798664, 0.54137, 0.44559 ],
[ 22, 28.0, 1.794819, 0.55367, 0.43253 ],
[ 23, 24.4, 1.789610, 0.56680, 0.41811 ],
[ 24, 20.6, 1.809483, 0.58128, 0.40176 ],
[ 25, 16.6, 1.760983, 0.59766, 0.38300 ],
[ 26, 12.3, 1.723443, 0.61653, 0.36061 ],
[ 30, 7.7, 1.652891, 0.63896, 0.33358 ],
[ 31, 2.8, 1.502607, 0.66619, 0.29930 ],
[ 32, -2.5, 1.072500, 0.70061, 0.26753 ],
[ 33, -8.4, 1.136637, 0.63925, 0.22631 ],
[ 34, -19.8, 1.232286, 0.53962, 0.19721 ],
[ 35, -31.6, 1.310120, 0.50340, 0.17495 ],
[ 40, -43.2, 1.376610, 0.46041, 0.15603 ],
[ 41, -54.6, 1.438692, 0.42386, 0.13846 ],
[ 42, -65.8, 1.501582, 0.38991, 0.12083 ],
[ 43, -76.8, 1.570447, 0.35586, 0.10328 ],
[ 44, -86.8, 1.645583, 0.32195, 0.08496 ],
[ 45, -95.8, 1.732083, 0.28657, 0.05155 ],
[ 46, -108.4, 1.915753, 0.22202, 0.01771 ],
[ 50, -117.2, 2.146310, 0.15664, 0.05227 ],
[ 51, -124.7, 1.649939, 0.12736, 0.09020 ],
[ 52, -131.8, 1.273415, 0.10813, 0.12506 ],
[ 53, -138.5, 1.080809, 0.09414, 0.15741 ],
[ 54, -145.1, 0.957076, 0.03249, 0.18958 ],
[ 55, -152.0, 0.868976, 0.07206, 0.24109 ],
[ 56, -163.4, 0.771731, 0.05787, 0.30378 ],
[ 60, -177.2, 0.697108, 0.04353, 0.35696 ],
[ 61, 171.6, 0.655803, 0.03291, 0.41971 ],
[ 62, 152.4, 0.623958, 0.02240, 0.49954 ],
[ 63, 148.4, 0.596037, 0.01196, 0.60321 ],
[ 64, 136.8, 0.607413, 0.00425, 0.73542 ],
[ 65, 125.4, 0.659923, 0.01099, 0.83391 ],
[ 66, 114.2, 0.859517, 0.08050, 0.77474 ],
[ 70, 103.2, 1.195683, 0.20259, 0.70460 ],
[ 71, 93.2, 1.407534, 0.28807, 0.65230 ],
[ 72, 84.2, 1.532829, 0.34422, 0.61930 ],
[ 73, 77.3, 1.603792, 0.37838, 0.59533 ],
[ 74, 71.6, 1.649448, 0.40290, 0.57716 ],
[ 75, 66.9, 1.681080, 0.42141, 0.56222 ],
[ 76, 62.8, 1.704979, 0.43647, 0.54895 ]
];
var Y = V*V / 100;

var Yl = yl * el * 100;

var x = (100*Y*x0*ew + 100*xl*el*T - Yl*T*x0*ew) / (100*T*el - Yl*T*ew + 100*Y*ew);
var y = (100*Y + 100*T*yl*el - Yl*T) / (Y*ew*100 + T*100*el - T*Yl*ew);

// var x = (100*Y*ew*x0 + 100*T*el*xl - T*Yl*ew*x0) / (100*T*el - T*Yl*ew + 100*Y*ew);
// var y = 100*Y / (100*T*el + 100*T*ew*Yl + 100*ew*Y);

// var x = (ew*x0*(V*V - 100*T*row[6]) + 100*T*el*xl) / (ew*(V*V - 100*T*row[6]) + 100*T*el);
// var y = V*V/(ew*(V*V + 100*T*row[6]) + 100*T*el);

return [x,y,Y];
}
};


/**
Expand Down Expand Up @@ -156,7 +194,7 @@ A λ ф tg ф ctg ф xλ yλ zλ xλ yλ

/** Create angle-sorted table */
var table = coloroid.table;
var angleTable = [].concat(table.slice(-13),table.slice(0, -13));
var angleTable = table.slice(-13).concat(table.slice(0, -13));


/**
Expand All @@ -179,9 +217,9 @@ var ew = (Xn + Yn + Zn) / 100;
/**
* From xyY to coloroid
*
* @param {Array} arg xyY tuple
* @param {Array<number>} arg xyY tuple
*
* @return {Array} ATV coloroid channels
* @return {Array<number>} ATV coloroid channels
*/
xyy.coloroid = function (arg) {
var x = arg[0], y = arg[1], Y = arg[2];
Expand Down Expand Up @@ -219,49 +257,6 @@ xyy.coloroid = function (arg) {
return [A, T, V];
};





/**
* Backwise - from coloroid to xyY
*
* @param {Array} arg Coloroid values
*
* @return {Array} xyY values
*/
coloroid.xyy = function (arg) {
var A = arg[0], T = arg[1], V = arg[2];

//find the closest row in the table
var row;
for (var i = 0; i < table.length; i++) {
if (A <= table[i][0]) {
row = table[i];
break;
}
}

var yl = row[4], el = row[2], xl = row[3];

var Y = V*V / 100;

var Yl = yl * el * 100;

var x = (100*Y*x0*ew + 100*xl*el*T - Yl*T*x0*ew) / (100*T*el - Yl*T*ew + 100*Y*ew);
var y = (100*Y + 100*T*yl*el - Yl*T) / (Y*ew*100 + T*100*el - T*Yl*ew);

// var x = (100*Y*ew*x0 + 100*T*el*xl - T*Yl*ew*x0) / (100*T*el - T*Yl*ew + 100*Y*ew);
// var y = 100*Y / (100*T*el + 100*T*ew*Yl + 100*ew*Y);

// var x = (ew*x0*(V*V - 100*T*row[6]) + 100*T*el*xl) / (ew*(V*V - 100*T*row[6]) + 100*T*el);
// var y = V*V/(ew*(V*V + 100*T*row[6]) + 100*T*el);

return [x,y,Y];
};



/** Proper transformation to a XYZ (via xyY) */
xyz.coloroid = function (arg) {
return xyy.coloroid(xyz.xyy(arg));
Expand All @@ -272,8 +267,4 @@ coloroid.xyz = function (arg) {



export default coloroid;




export default /** @type {import('./index.js').ColorSpace & ColoroidSpecific} */ (coloroid);
Loading

0 comments on commit 557cd51

Please sign in to comment.