Skip to content

Commit

Permalink
added channels property
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Mar 30, 2016
1 parent f07255a commit 084f540
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
37 changes: 25 additions & 12 deletions conversions.js
Expand Up @@ -13,20 +13,33 @@ for (var key in cssKeywords) {
}

var convert = module.exports = {
rgb: {},
hsl: {},
hsv: {},
hwb: {},
cmyk: {},
xyz: {},
lab: {},
lch: {},
hex: {},
keyword: {},
ansi16: {},
ansi256: {}
rgb: {channels: 3},
hsl: {channels: 3},
hsv: {channels: 3},
hwb: {channels: 3},
cmyk: {channels: 4},
xyz: {channels: 3},
lab: {channels: 3},
lch: {channels: 3},
hex: {channels: 1},
keyword: {channels: 1},
ansi16: {channels: 1},
ansi256: {channels: 1}
};

// hide .channels property
for (var model in convert) {
if (convert.hasOwnProperty(model)) {
if (!('channels' in convert[model])) {
throw new Error('missing channels property: ' + model);
}

var channels = convert[model].channels;
delete convert[model].channels;
Object.defineProperty(convert[model], 'channels', {value: channels});
}
}

convert.rgb.hsl = function (rgb) {
var r = rgb[0] / 255;
var g = rgb[1] / 255;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Expand Up @@ -61,6 +61,8 @@ function wrapRounded(fn) {
models.forEach(function (fromModel) {
convert[fromModel] = {};

Object.defineProperty(convert[fromModel], 'channels', {value: conversions[fromModel].channels});

var routes = route(fromModel);
var routeModels = Object.keys(routes);

Expand Down
4 changes: 4 additions & 0 deletions test/basic.js
Expand Up @@ -25,6 +25,10 @@ for (var len = models.length, i = 0; i < len; i++) {
console.log(chalk.red([fromModel, toModel].join('->')), chalk.red('(no conversion)'));
}
}

// should not expose channels
assert(convert[toModel].channels > 0);
assert(Object.keys(convert[toModel]).indexOf('channels') === -1);
}

assert.deepEqual(convert.rgb.hsl([140, 200, 100]), [96, 48, 59]);
Expand Down

0 comments on commit 084f540

Please sign in to comment.