colr-convert
Version:
Color conversion functions
125 lines (111 loc) • 3.15 kB
JavaScript
'use strict';
var assert = require('assert');
var convert = require('./index');
function round2dp (float) {
return Math.round(float * 100) / 100;
}
function compare (actual, expected) {
if (!Array.isArray(expected)) {
assert.equal(actual, expected);
} else {
assert.deepEqual(actual.map(round2dp), expected.map(round2dp));
}
}
function test (from, to, colors) {
it('should convert ' + from + ' to ' + to, function () {
colors.forEach(function (color) {
compare(
convert[from][to](color[0]),
color[1]
);
});
});
}
describe('colr-convert', function () {
var tests = {
grayscale: {
rgb: [
[0, [0, 0, 0]],
[128, [128, 128, 128]],
[255, [255, 255, 255]],
],
},
hex: {
rgb: [
['#000000', [0, 0, 0]],
['01ab48', [1, 171, 72]],
['#aabbcc', [170, 187, 204]],
['ffffff', [255, 255, 255]],
['#FFFFFF', [255, 255, 255]],
['808080', [128, 128, 128]],
['888', [136, 136, 136]],
['FAB', [255, 170, 187]],
],
},
rgb: {
grayscale: [
[[0, 0, 0], 0],
[[30, 40, 50], 38.15],
[[132, 128, 9], 115.63],
[[255, 255, 255], 255],
],
hex: [
[[0, 0, 0], '#000000'],
[[12.4, 88.2, 192], '#0c58c0'],
[[80, 11, 202], '#500bca'],
[[255, 255, 255], '#ffffff'],
],
hsv: [
[[0, 0, 0], [0, 0, 0]],
[[20, 30, 40], [210, 50, 15.69]],
[[200, 100, 80], [10, 60, 78.43]],
[[255, 255, 255], [0, 0, 100]],
[[128, 70, 143], [287.67, 51.05, 56.08]],
[[129, 70, 143], [288.49, 51.05, 56.08]],
],
hsl: [
[[0, 0, 0], [0, 0, 0]],
[[20, 30, 40], [210, 33.33, 11.76]],
[[200, 100, 80], [10, 52.17, 54.90]],
[[255, 255, 255], [0, 0, 100]],
]
},
hsv: {
rgb: [
[[0, 0, 0], [0, 0, 0]],
[[20, 30, 40], [102, 81.6, 71.4]],
[[180, 80, 60], [30.6, 153, 153]],
[[360, 100, 100], [255, 0, 0]],
[[288, 51, 56], [128.23, 69.97, 142.8]],
[[289, 51, 56], [129.45, 69.97, 142.8]],
[[287.67, 51.05, 56.08], [128, 70, 143]],
[[288.49, 51.05, 56.08], [129, 70, 143]],
],
hsl: [
[[0, 0, 0], [0, 0, 0]],
[[20, 30, 40], [20, 17.65, 34]],
[[180, 80, 60], [180, 66.67, 36]],
[[360, 100, 100], [360, 100, 50]],
]
},
hsl: {
rgb: [
[[0, 0, 0], [0, 0, 0]],
[[20, 30, 40], [132.6, 91.8, 71.4]],
[[180, 80, 60], [71.4, 234.6, 234.6]],
[[360, 100, 100], [255, 255, 255]],
],
hsv: [
[[0, 0, 0], [0, 0, 0]],
[[20, 30, 40], [20, 46.15, 52]],
[[180, 80, 60], [180, 69.57, 92]],
[[360, 100, 100], [360, 0, 100]],
]
}
};
for (var from in tests) {
for (var to in tests[from]) {
test(from, to, tests[from][to]);
}
}
});