Skip to content

Commit

Permalink
Begin cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dy committed Jan 31, 2025
1 parent 7a9eb23 commit 8964507
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 94 deletions.
40 changes: 13 additions & 27 deletions cmy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,26 @@
*/
import rgb from './rgb.js';

/** @type {Partial<import('./index.js').ColorSpace>} */
var cmy = {
const cmy = {
name: 'cmy',
min: [0,0,0],
max: [100,100,100],
channel: ['cyan', 'magenta', 'yellow'],
alias: ['CMY']
};


/**
* CMY to RGB
*
* @param {Array<number>} cmy Channels
* @param {Array<number>} CMY channels
*
* @return {Array<number>} RGB channels
*/
cmy.rgb = function(cmy) {
var c = cmy[0] / 100,
m = cmy[1] / 100,
y = cmy[2] / 100;

return [
(1 - c) * 255,
(1 - m) * 255,
(1 - y) * 255
];
};

cmy.rgb = ([c, m, y]) => [
(1 - c/100) * 255,
(1 - m/100) * 255,
(1 - y/100) * 255
];

/**
* RGB to CMY
Expand All @@ -40,16 +31,11 @@ cmy.rgb = function(cmy) {
*
* @return {Array<number>} CMY channels
*/
rgb.cmy = function(rgb) {
var r = rgb[0] / 255,
g = rgb[1] / 255,
b = rgb[2] / 255;
rgb.cmy = ([r, g, b]) => [
(1-r/255) * 100 || 0,
(1-g/255) * 100 || 0,
(1-b/255) * 100 || 0
];

return [
(1-r) * 100 || 0,
(1-g) * 100 || 0,
(1-b) * 100 || 0
];
}

export default /** @type {import('./index.js').ColorSpace} */ (cmy);
export default cmy;
19 changes: 9 additions & 10 deletions cmyk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@
*/
import rgb from './rgb.js';

/** @type {Partial<import('./index.js').ColorSpace>} */
const cmyk = {
name: 'cmyk',
min: [0,0,0,0],
max: [100,100,100,100],
channel: ['cyan', 'magenta', 'yellow', 'black'],
alias: ['CMYK'],

rgb: function(cmyk) {
var c = cmyk[0] / 100,
rgb: (cmyk) => {
let c = cmyk[0] / 100,
m = cmyk[1] / 100,
y = cmyk[2] / 100,
k = cmyk[3] / 100,
r, g, b;

r = 1 - Math.min(1, c * (1 - k) + k);
g = 1 - Math.min(1, m * (1 - k) + k);
r = 1 - Math.min(1, c * (1 - k) + k),
g = 1 - Math.min(1, m * (1 - k) + k),
b = 1 - Math.min(1, y * (1 - k) + k);

return [r * 255, g * 255, b * 255];
}
};


//extend rgb
rgb.cmyk = function(rgb) {
var r = rgb[0] / 255,
rgb.cmyk = (rgb) => {
let r = rgb[0] / 255,
g = rgb[1] / 255,
b = rgb[2] / 255,
c, m, y, k;
Expand All @@ -37,7 +35,8 @@ rgb.cmyk = function(rgb) {
c = (1 - r - k) / (1 - k) || 0;
m = (1 - g - k) / (1 - k) || 0;
y = (1 - b - k) / (1 - k) || 0;

return [c * 100, m * 100, y * 100, k * 100];
};

export default /** @type {import('./index.js').ColorSpace} */ (cmyk);
export default cmyk;
52 changes: 19 additions & 33 deletions coloroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,23 @@
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'],

//hue, saturation, luminosity
//note that hue values are ids, not the numbers - not every value is possible
//e.g. 38 will be rounded to 36
// hue, saturation, luminosity
// note that hue values are ids, not the numbers - not every value is possible
// e.g. 38 will be rounded to 36
channel: ['A', 'T', 'V'],
min: [10, 0, 0],
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
*/

// 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],
Expand Down Expand Up @@ -85,16 +77,15 @@ var coloroid = {
[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];

xyy: function ([A, T, V]) {
//find the closest row in the table
var row;
for (var i = 0; i < table.length; i++) {
Expand All @@ -105,7 +96,6 @@ var coloroid = {
}

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

var Y = V * V / 100;
Expand Down Expand Up @@ -190,17 +180,13 @@ A λ ф tg ф ctg ф xλ yλ zλ xλ yλ
*/




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


/**
* Some precalculations
* 2° D65 whitepoint is used
*/
// Some precalculations
// 2° D65 whitepoint is used
var i = 'D65';
var o = 2;

Expand All @@ -212,8 +198,6 @@ var y0 = Xn / (Xn + Yn + Zn);
var x0 = Yn / (Xn + Yn + Zn);
var ew = (Xn + Yn + Zn) / 100;



/**
* From xyY to coloroid
*
Expand Down Expand Up @@ -257,7 +241,9 @@ xyy.coloroid = function (arg) {
return [A, T, V];
};

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



export default /** @type {import('./index.js').ColorSpace & ColoroidSpecific} */ (coloroid);
export default coloroid;
25 changes: 8 additions & 17 deletions cubehelix.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,36 @@
*
* @module color-space/cubehelix
*/

/** @typedef {{defaults: {start: number, rotation: number, hue: number, gamma: number}}} CubeHelixSpecific */

import rgb from './rgb.js';

/** Default options for space */
var defaults = {
//0..3
// 0..3
start: 0,
//-10..10
// -10..10
rotation: 0.5,
//0..1+
// 0..1+
hue: 1,
//0..2
// 0..2
gamma: 1
};

/** @type {Partial<import('./index.js').ColorSpace> & CubeHelixSpecific} */
var cubehelix = {
name: 'cubehelix',
channel: ['fraction'],
min: [0],
max: [1],
defaults: defaults
defaults
};


/**
* Transform cubehelix level to RGB
*
* @param {Number|Array<number>} fraction 0..1 cubehelix level
* @param {number|Array<number>} fraction 0..1 cubehelix level
* @param {Object<string, number>} options Mapping options, overrides defaults
*
* @return {Array<number>} rgb tuple
*/
cubehelix.rgb = function(fraction, options) {
options = options || {};

cubehelix.rgb = function(fraction, options={}) {
if (Array.isArray(fraction)) fraction = fraction[0];

var start = options.start !== undefined ? options.start : defaults.start;
Expand Down Expand Up @@ -70,7 +62,6 @@ cubehelix.rgb = function(fraction, options) {
* RGB to cubehelix
*
* @param {Array<number>} rgb RGB values
*
* @return {Array<number>} cubehelix fraction(s)
*/
rgb.cubehelix = function(rgb) {
Expand All @@ -79,4 +70,4 @@ rgb.cubehelix = function(rgb) {
};


export default /** @type {import('./index.js').ColorSpace & CubeHelixSpecific} */ (cubehelix);
export default cubehelix;
11 changes: 4 additions & 7 deletions rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
*
* @module color-space/rgb
*/


/** @type {Partial<import('./index.js').ColorSpace>} */
var rgb = {
const rgb = {
name: 'rgb',
min: [0,0,0],
max: [255,255,255],
min: [0, 0, 0],
max: [255, 255, 255],
channel: ['red', 'green', 'blue'],
alias: ['RGB']
};

export default /** @type {import('./index.js').ColorSpace} */ (rgb);
export default rgb;

0 comments on commit 8964507

Please sign in to comment.