wishthis/node_modules/color-convert/index.js

82 lines
1.7 KiB
JavaScript
Raw Normal View History

2022-06-08 10:36:39 +00:00
const conversions = require('./conversions');
const route = require('./route');
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
const convert = {};
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
const models = Object.keys(conversions);
2022-01-21 08:28:41 +00:00
function wrapRaw(fn) {
2022-06-08 10:36:39 +00:00
const wrappedFn = function (...args) {
const arg0 = args[0];
if (arg0 === undefined || arg0 === null) {
return arg0;
2022-01-21 08:28:41 +00:00
}
2022-06-08 10:36:39 +00:00
if (arg0.length > 1) {
args = arg0;
2022-01-21 08:28:41 +00:00
}
return fn(args);
};
2022-06-08 10:36:39 +00:00
// Preserve .conversion property if there is one
2022-01-21 08:28:41 +00:00
if ('conversion' in fn) {
wrappedFn.conversion = fn.conversion;
}
return wrappedFn;
}
function wrapRounded(fn) {
2022-06-08 10:36:39 +00:00
const wrappedFn = function (...args) {
const arg0 = args[0];
if (arg0 === undefined || arg0 === null) {
return arg0;
2022-01-21 08:28:41 +00:00
}
2022-06-08 10:36:39 +00:00
if (arg0.length > 1) {
args = arg0;
2022-01-21 08:28:41 +00:00
}
2022-06-08 10:36:39 +00:00
const result = fn(args);
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
// We're assuming the result is an array here.
2022-01-21 08:28:41 +00:00
// see notice in conversions.js; don't use box types
// in conversion functions.
if (typeof result === 'object') {
2022-06-08 10:36:39 +00:00
for (let len = result.length, i = 0; i < len; i++) {
2022-01-21 08:28:41 +00:00
result[i] = Math.round(result[i]);
}
}
return result;
};
2022-06-08 10:36:39 +00:00
// Preserve .conversion property if there is one
2022-01-21 08:28:41 +00:00
if ('conversion' in fn) {
wrappedFn.conversion = fn.conversion;
}
return wrappedFn;
}
2022-06-08 10:36:39 +00:00
models.forEach(fromModel => {
2022-01-21 08:28:41 +00:00
convert[fromModel] = {};
Object.defineProperty(convert[fromModel], 'channels', {value: conversions[fromModel].channels});
Object.defineProperty(convert[fromModel], 'labels', {value: conversions[fromModel].labels});
2022-06-08 10:36:39 +00:00
const routes = route(fromModel);
const routeModels = Object.keys(routes);
2022-01-21 08:28:41 +00:00
2022-06-08 10:36:39 +00:00
routeModels.forEach(toModel => {
const fn = routes[toModel];
2022-01-21 08:28:41 +00:00
convert[fromModel][toModel] = wrapRounded(fn);
convert[fromModel][toModel].raw = wrapRaw(fn);
});
});
module.exports = convert;