From 7f2f24ed883ce72e14abfed70a988124c4ec73d7 Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 30 Jul 2024 14:39:43 +0800 Subject: [PATCH] test: migrate to vitest (#337) --- test/mix.test.cjs | 153 ------- test/mix.test.js | 91 ++++ test/num.test.cjs | 54 --- test/num.test.js | 27 ++ test/num2rgb.test.cjs | 27 -- test/num2rgb.test.js | 24 + test/oklab2rgb.test.cjs | 65 --- test/oklab2rgb.test.js | 39 ++ test/oklch2rgb.test.cjs | 71 --- test/oklch2rgb.test.js | 39 ++ test/random.test.cjs | 16 - test/random.test.js | 9 + test/rgb2cmyk.test.cjs | 27 -- test/rgb2cmyk.test.js | 23 + test/rgb2css.test.cjs | 52 --- test/rgb2css.test.js | 38 ++ test/{rgb2hex.test.cjs => rgb2hex.test.js} | 50 +-- test/rgb2hsi.test.cjs | 53 --- test/rgb2hsi.test.js | 40 ++ test/rgb2hsv.test.cjs | 37 -- test/rgb2hsv.test.js | 33 ++ test/{rgb2lab.test.cjs => rgb2lab.test.js} | 57 ++- test/{rgb2lch.test.cjs => rgb2lch.test.js} | 57 ++- test/rgb2oklab.test.cjs | 60 --- test/rgb2oklab.test.js | 42 ++ .../{rgb2oklch.test.cjs => rgb2oklch.test.js} | 48 +- test/scale.lcorrection.test.cjs | 89 ---- test/scale.lcorrection.test.js | 60 +++ test/scales.test.cjs | 409 ------------------ test/scales.test.js | 408 +++++++++++++++++ test/temperature2rgb.test.cjs | 30 -- test/temperature2rgb.test.js | 22 + test/unpack.test.cjs | 29 -- test/unpack.test.js | 16 + test/valid.test.cjs | 22 - test/valid.test.js | 12 + 36 files changed, 1012 insertions(+), 1317 deletions(-) delete mode 100644 test/mix.test.cjs create mode 100644 test/mix.test.js delete mode 100644 test/num.test.cjs create mode 100644 test/num.test.js delete mode 100644 test/num2rgb.test.cjs create mode 100644 test/num2rgb.test.js delete mode 100644 test/oklab2rgb.test.cjs create mode 100644 test/oklab2rgb.test.js delete mode 100644 test/oklch2rgb.test.cjs create mode 100644 test/oklch2rgb.test.js delete mode 100644 test/random.test.cjs create mode 100644 test/random.test.js delete mode 100644 test/rgb2cmyk.test.cjs create mode 100644 test/rgb2cmyk.test.js delete mode 100644 test/rgb2css.test.cjs create mode 100644 test/rgb2css.test.js rename test/{rgb2hex.test.cjs => rgb2hex.test.js} (51%) delete mode 100644 test/rgb2hsi.test.cjs create mode 100644 test/rgb2hsi.test.js delete mode 100644 test/rgb2hsv.test.cjs create mode 100644 test/rgb2hsv.test.js rename test/{rgb2lab.test.cjs => rgb2lab.test.js} (53%) rename test/{rgb2lch.test.cjs => rgb2lch.test.js} (53%) delete mode 100644 test/rgb2oklab.test.cjs create mode 100644 test/rgb2oklab.test.js rename test/{rgb2oklch.test.cjs => rgb2oklch.test.js} (52%) delete mode 100644 test/scale.lcorrection.test.cjs create mode 100644 test/scale.lcorrection.test.js delete mode 100644 test/scales.test.cjs create mode 100644 test/scales.test.js delete mode 100644 test/temperature2rgb.test.cjs create mode 100644 test/temperature2rgb.test.js delete mode 100644 test/unpack.test.cjs create mode 100644 test/unpack.test.js delete mode 100644 test/valid.test.cjs create mode 100644 test/valid.test.js diff --git a/test/mix.test.cjs b/test/mix.test.cjs deleted file mode 100644 index 5d32bce6..00000000 --- a/test/mix.test.cjs +++ /dev/null @@ -1,153 +0,0 @@ -require('es6-shim'); -const vows = require('vows'); -const assert = require('assert'); -const chroma = require('../chroma.cjs'); - -vows.describe('Some tests for chroma.color()') - - .addBatch({ - 'hsv interpolation white <-> red': { - topic: chroma('white').interpolate('red', 0.5, 'hsv'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff8080'); - } - }, - - 'use mix as alias': { - topic: chroma('white').mix('red', 0.5, 'hsv'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff8080'); - } - }, - - 'alternative mix syntax': { - topic: chroma.mix('red', 'blue', 0.25, 'rgb'), - works(topic) { - return assert.deepEqual(topic.hex(), '#bf0040'); - } - }, - - 'hsl interpolation white <-> red': { - topic: chroma('white').interpolate('red', 0.5, 'hsl'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff8080'); - } - }, - - 'rgb interpolation white <-> red': { - topic: chroma('white').interpolate('red', 0.5, 'rgb'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff8080'); - } - }, - - 'hsv interpolation red <-> white': { - topic: chroma('red').interpolate('white', 0.5, 'hsv'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff8080'); - } - }, - - 'hsl interpolation red <-> white': { - topic: chroma('red').interpolate('white', 0.5, 'hsl'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff8080'); - } - }, - - 'rgb interpolation red <-> white': { - topic: chroma('red').interpolate('white', 0.5, 'rgb'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff8080'); - } - }, - - 'interpolation short function': { - topic: { - f(t) { - return chroma - .interpolate('#ff0000', '#ffffff', t, 'hsv') - .hex(); - } - }, - 'starts at red'(topic) { - return assert.equal(topic.f(0), '#ff0000'); - }, - 'goes over light red'(topic) { - return assert.equal(topic.f(0.5), '#ff8080'); - }, - 'ends at white'(topic) { - return assert.equal(topic.f(1), '#ffffff'); - } - }, - - 'num interpolation white <-> red': { - topic: chroma(0xffffff).interpolate(0xff0000, 0.5, 'num'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff7fff'); - } - }, - - 'num interpolation red <-> white': { - topic: chroma(0xff0000).interpolate(0xffffff, 0.5, 'num'), - works(topic) { - return assert.deepEqual(topic.hex(), '#ff7fff'); - } - }, - - 'interpolation short function with num provided': { - topic: { - f(t) { - return chroma - .interpolate(0xff0000, 0xffffff, t, 'num') - .hex(); - } - }, - 'starts at red'(topic) { - return assert.equal(topic.f(0), '#ff0000'); - }, - 'goes over light red'(topic) { - return assert.equal(topic.f(0.5), '#ff7fff'); - }, - 'ends at white'(topic) { - return assert.equal(topic.f(1), '#ffffff'); - } - }, - - 'interpolate in num': { - topic: chroma.interpolate( - chroma.num(0xffffe0), - chroma.num(0x102180), - 0.5, - 'num' - ), - hex(topic) { - return assert.equal(topic.hex(), '#8810b0'); - }, - num(topic) { - return assert.equal(topic.num(), 8917168); - } - }, - - 'interpolate in hsv': { - topic: chroma.interpolate('white', 'black', 0.5, 'hsv'), - hex(topic) { - return assert.equal(topic.hex(), '#808080'); - } - }, - - 'interpolate in hsl': { - topic: chroma.interpolate('lightyellow', 'navy', 0.5, 'hsl'), - hex(topic) { - return assert.equal(topic.hex(), '#31ff98'); - } - }, - - 'interpolate in lrgb': { - topic: chroma.interpolate('red', 'blue', 0.5, 'lrgb'), - hex(topic) { - return assert.equal(topic.hex(), '#b400b4'); - } - } - }) - .export(module); diff --git a/test/mix.test.js b/test/mix.test.js new file mode 100644 index 00000000..fbcd1810 --- /dev/null +++ b/test/mix.test.js @@ -0,0 +1,91 @@ +import { describe, it, expect } from 'vitest'; +import chroma from '../index.js'; + +describe('Some tests for chroma.color()', () => { + it('hsv interpolation white <-> red', () => { + const result = chroma('white').interpolate('red', 0.5, 'hsv'); + expect(result.hex()).toBe('#ff8080'); + }); + + it('use mix as alias', () => { + const result = chroma('white').mix('red', 0.5, 'hsv'); + expect(result.hex()).toBe('#ff8080'); + }); + + it('alternative mix syntax', () => { + const result = chroma.mix('red', 'blue', 0.25, 'rgb'); + expect(result.hex()).toBe('#bf0040'); + }); + + it('hsl interpolation white <-> red', () => { + const result = chroma('white').interpolate('red', 0.5, 'hsl'); + expect(result.hex()).toBe('#ff8080'); + }); + + it('rgb interpolation white <-> red', () => { + const result = chroma('white').interpolate('red', 0.5, 'rgb'); + expect(result.hex()).toBe('#ff8080'); + }); + + it('hsv interpolation red <-> white', () => { + const result = chroma('red').interpolate('white', 0.5, 'hsv'); + expect(result.hex()).toBe('#ff8080'); + }); + + it('hsl interpolation red <-> white', () => { + const result = chroma('red').interpolate('white', 0.5, 'hsl'); + expect(result.hex()).toBe('#ff8080'); + }); + + it('rgb interpolation red <-> white', () => { + const result = chroma('red').interpolate('white', 0.5, 'rgb'); + expect(result.hex()).toBe('#ff8080'); + }); + + it('interpolation short function', () => { + const interpolateFn = (t) => chroma.interpolate('#ff0000', '#ffffff', t, 'hsv').hex(); + + expect(interpolateFn(0)).toBe('#ff0000'); + expect(interpolateFn(0.5)).toBe('#ff8080'); + expect(interpolateFn(1)).toBe('#ffffff'); + }); + + it('num interpolation white <-> red', () => { + const result = chroma(0xffffff).interpolate(0xff0000, 0.5, 'num'); + expect(result.hex()).toBe('#ff7fff'); + }); + + it('num interpolation red <-> white', () => { + const result = chroma(0xff0000).interpolate(0xffffff, 0.5, 'num'); + expect(result.hex()).toBe('#ff7fff'); + }); + + it('interpolation short function with num provided', () => { + const interpolateFn = (t) => chroma.interpolate(0xff0000, 0xffffff, t, 'num').hex(); + + expect(interpolateFn(0)).toBe('#ff0000'); + expect(interpolateFn(0.5)).toBe('#ff7fff'); + expect(interpolateFn(1)).toBe('#ffffff'); + }); + + it('interpolate in num', () => { + const result = chroma.interpolate(chroma.num(0xffffe0), chroma.num(0x102180), 0.5, 'num'); + expect(result.hex()).toBe('#8810b0'); + expect(result.num()).toBe(8917168); + }); + + it('interpolate in hsv', () => { + const result = chroma.interpolate('white', 'black', 0.5, 'hsv'); + expect(result.hex()).toBe('#808080'); + }); + + it('interpolate in hsl', () => { + const result = chroma.interpolate('lightyellow', 'navy', 0.5, 'hsl'); + expect(result.hex()).toBe('#31ff98'); + }); + + it('interpolate in lrgb', () => { + const result = chroma.interpolate('red', 'blue', 0.5, 'lrgb'); + expect(result.hex()).toBe('#b400b4'); + }); +}); diff --git a/test/num.test.cjs b/test/num.test.cjs deleted file mode 100644 index f644ae07..00000000 --- a/test/num.test.cjs +++ /dev/null @@ -1,54 +0,0 @@ -require('es6-shim'); - -const vows = require('vows'); -const assert = require('assert'); -const chroma = require('../chroma.cjs'); - -vows.describe('Some tests for chroma.num()') - .addBatch({ - 'number output': { - topic: chroma.hsl(0, 1, 0.5, 0.5), - numoutput: function () { - return function (topic) { - assert.equal(topic.num(), 0xff0000); - }; - } - }, - 'num color': { - topic: [ - chroma(0xff0000), - chroma(0x000000), - chroma(0xffffff), - chroma(0x31ff98), - chroma('red') - ], - hex: function (topic) { - assert.equal(topic[0].hex(), '#ff0000'); - }, - num: function (topic) { - assert.equal(topic[0].num(), 0xff0000); - }, - 'hex-black': function (topic) { - assert.equal(topic[1].hex(), '#000000'); - }, - 'num-black': function (topic) { - assert.equal(topic[1].num(), 0x000000); - }, - 'hex-white': function (topic) { - assert.equal(topic[2].hex(), '#ffffff'); - }, - 'num-white': function (topic) { - assert.equal(topic[2].num(), 0xffffff); - }, - 'hex-rand': function (topic) { - assert.equal(topic[3].hex(), '#31ff98'); - }, - 'num-rand': function (topic) { - assert.equal(topic[3].num(), 0x31ff98); - }, - 'rum-red': function (topic) { - assert.equal(topic[4].num(), 0xff0000); - } - } - }) - ['export'](module); diff --git a/test/num.test.js b/test/num.test.js new file mode 100644 index 00000000..353bbfa7 --- /dev/null +++ b/test/num.test.js @@ -0,0 +1,27 @@ +import { describe, it, expect } from 'vitest'; +import chroma from '../index.js'; + +describe('Some tests for chroma.num()', () => { + it('number output', () => { + const color = chroma.hsl(0, 1, 0.5, 0.5); + expect(color.num()).toBe(0xff0000); + }); + + it('num color', () => { + const colors = [chroma(0xff0000), chroma(0x000000), chroma(0xffffff), chroma(0x31ff98), chroma('red')]; + + expect(colors[0].hex()).toBe('#ff0000'); + expect(colors[0].num()).toBe(0xff0000); + + expect(colors[1].hex()).toBe('#000000'); + expect(colors[1].num()).toBe(0x000000); + + expect(colors[2].hex()).toBe('#ffffff'); + expect(colors[2].num()).toBe(0xffffff); + + expect(colors[3].hex()).toBe('#31ff98'); + expect(colors[3].num()).toBe(0x31ff98); + + expect(colors[4].num()).toBe(0xff0000); + }); +}); diff --git a/test/num2rgb.test.cjs b/test/num2rgb.test.cjs deleted file mode 100644 index ab3db517..00000000 --- a/test/num2rgb.test.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import num2rgb from '../src/io/num/num2rgb.js'; - -vows.describe('Testing num2rgb color conversions') - .addBatch({ - 'parse simple numeric HEX colors': { - topic: { - black: { in: 0x000000, out: [0, 0, 0, 1] }, - white: { in: 0xffffff, out: [255, 255, 255, 1] }, - red: { in: 0xff0000, out: [255, 0, 0, 1] }, - green: { in: 0x00ff00, out: [0, 255, 0, 1] }, - blue: { in: 0x0000ff, out: [0, 0, 255, 1] }, - yellow: { in: 0xffff00, out: [255, 255, 0, 1] }, - cyan: { in: 0x00ffff, out: [0, 255, 255, 1] }, - magenta: { in: 0xff00ff, out: [255, 0, 255, 1] } - }, - num(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual(num2rgb(topic[key].in), topic[key].out); - }); - } - } - }) - .export(module); diff --git a/test/num2rgb.test.js b/test/num2rgb.test.js new file mode 100644 index 00000000..1c1124b4 --- /dev/null +++ b/test/num2rgb.test.js @@ -0,0 +1,24 @@ +import { describe, it, expect } from 'vitest'; +import num2rgb from '../src/io/num/num2rgb.js'; + +describe('Testing num2rgb color conversions', () => { + const testCases = { + black: { in: 0x000000, out: [0, 0, 0, 1] }, + white: { in: 0xffffff, out: [255, 255, 255, 1] }, + red: { in: 0xff0000, out: [255, 0, 0, 1] }, + green: { in: 0x00ff00, out: [0, 255, 0, 1] }, + blue: { in: 0x0000ff, out: [0, 0, 255, 1] }, + yellow: { in: 0xffff00, out: [255, 255, 0, 1] }, + cyan: { in: 0x00ffff, out: [0, 255, 255, 1] }, + magenta: { in: 0xff00ff, out: [255, 0, 255, 1] } + }; + + Object.keys(testCases).forEach((key) => { + const { in: input, out: expected } = testCases[key]; + + it(`should convert numeric HEX to RGB for ${key}`, () => { + const result = num2rgb(input); + expect(result).toEqual(expected); + }); + }); +}); diff --git a/test/oklab2rgb.test.cjs b/test/oklab2rgb.test.cjs deleted file mode 100644 index 06579269..00000000 --- a/test/oklab2rgb.test.cjs +++ /dev/null @@ -1,65 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import limit from '../src/utils/limit.js'; -import oklab2rgb from '../src/io/oklab/oklab2rgb.js'; - -const round = (v) => limit(Math.round(v), 0, 255); - -vows.describe('Testing CMYK color conversions') - .addBatch({ - oklab2rgb: { - topic: { - black: { in: [0.0, 0.0, 0.0], out: [0, 0, 0, 1] }, - white: { in: [1.0, 0.0, 0.0], out: [255, 255, 255, 1] }, - gray: { in: [0.59987, 0.0, 0.0], out: [128, 128, 128, 1] }, - red: { in: [0.62796, 0.22486, 0.12585], out: [255, 0, 0, 1] }, - yellow: { - in: [0.96798, -0.07137, 0.19857], - out: [255, 255, 0, 1] - }, - green: { in: [0.51975, -0.1403, 0.10768], out: [0, 128, 0, 1] }, - cyan: { - in: [0.9054, -0.14944, -0.0394], - out: [0, 255, 255, 1] - }, - blue: { - in: [0.45201, -0.03246, -0.31153], - out: [0, 0, 255, 1] - }, - magenta: { - in: [0.70167, 0.27457, -0.16916], - out: [255, 0, 255, 1] - } - }, - lab_arr(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual( - oklab2rgb(topic[key].in).map(round), - topic[key].out - ); - }); - }, - lab_args(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual( - oklab2rgb.apply(null, topic[key].in).map(round), - topic[key].out, - key - ); - }); - }, - lab_obj(topic) { - Object.keys(topic).forEach((key) => { - const [l, a, b] = topic[key].in; - assert.deepEqual( - oklab2rgb({ l, a, b }).map(round), - topic[key].out, - key - ); - }); - } - } - }) - .export(module); diff --git a/test/oklab2rgb.test.js b/test/oklab2rgb.test.js new file mode 100644 index 00000000..07402d2c --- /dev/null +++ b/test/oklab2rgb.test.js @@ -0,0 +1,39 @@ +import { describe, it, expect } from 'vitest'; +import limit from '../src/utils/limit.js'; +import oklab2rgb from '../src/io/oklab/oklab2rgb.js'; + +const round = (v) => limit(Math.round(v), 0, 255); + +describe('Testing OKLab to RGB color conversions', () => { + const testCases = { + black: { in: [0.0, 0.0, 0.0], out: [0, 0, 0, 1] }, + white: { in: [1.0, 0.0, 0.0], out: [255, 255, 255, 1] }, + gray: { in: [0.59987, 0.0, 0.0], out: [128, 128, 128, 1] }, + red: { in: [0.62796, 0.22486, 0.12585], out: [255, 0, 0, 1] }, + yellow: { in: [0.96798, -0.07137, 0.19857], out: [255, 255, 0, 1] }, + green: { in: [0.51975, -0.1403, 0.10768], out: [0, 128, 0, 1] }, + cyan: { in: [0.9054, -0.14944, -0.0394], out: [0, 255, 255, 1] }, + blue: { in: [0.45201, -0.03246, -0.31153], out: [0, 0, 255, 1] }, + magenta: { in: [0.70167, 0.27457, -0.16916], out: [255, 0, 255, 1] } + }; + + Object.keys(testCases).forEach((key) => { + const { in: input, out: expected } = testCases[key]; + + it(`should convert OKLab to RGB for ${key} with array input`, () => { + const result = oklab2rgb(input).map(round); + expect(result).toEqual(expected); + }); + + it(`should convert OKLab to RGB for ${key} with arguments`, () => { + const result = oklab2rgb(...input).map(round); + expect(result).toEqual(expected); + }); + + it(`should convert OKLab to RGB for ${key} with object input`, () => { + const [l, a, b] = input; + const result = oklab2rgb({ l, a, b }).map(round); + expect(result).toEqual(expected); + }); + }); +}); diff --git a/test/oklch2rgb.test.cjs b/test/oklch2rgb.test.cjs deleted file mode 100644 index 6c071ecf..00000000 --- a/test/oklch2rgb.test.cjs +++ /dev/null @@ -1,71 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import limit from '../src/utils/limit.js'; -import oklch2rgb from '../src/io/oklch/oklch2rgb.js'; - -const round = (v) => limit(Math.round(v), 0, 255); - -vows.describe('Testing LCH conversions') - .addBatch({ - 'oklch to rgb': { - topic: { - black: { in: [0.0, 0.0, NaN], out: [0, 0, 0, 1] }, - white: { in: [1.0, 0.0, NaN], out: [255, 255, 255, 1] }, - gray: { in: [0.59987, 0.0, NaN], out: [128, 128, 128, 1] }, - red: { - in: [0.62796, 0.25768, 29.233885192342633], - out: [255, 0, 0, 1] - }, - yellow: { - in: [0.96798, 0.21101, 109.76923207652125], - out: [255, 255, 0, 1] - }, - green: { - in: [0.51975, 0.17686, 142.49533888780996], - out: [0, 128, 0, 1] - }, - cyan: { - in: [0.9054, 0.15455, 194.76894793196382], - out: [0, 255, 255, 1] - }, - blue: { - in: [0.45201, 0.31321, 264.052020638055], - out: [0, 0, 255, 1] - }, - magenta: { - in: [0.70167, 0.32249, 328.36341792345144], - out: [255, 0, 255, 1] - } - }, - lch_arr(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual( - oklch2rgb(topic[key].in).map(round), - topic[key].out - ); - }); - }, - lch_args(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual( - oklch2rgb.apply(null, topic[key].in).map(round), - topic[key].out, - key - ); - }); - }, - lch_obj(topic) { - Object.keys(topic).forEach((key) => { - const [l, c, h] = topic[key].in; - assert.deepEqual( - oklch2rgb({ l, c, h }).map(round), - topic[key].out, - key - ); - }); - } - } - }) - .export(module); diff --git a/test/oklch2rgb.test.js b/test/oklch2rgb.test.js new file mode 100644 index 00000000..734b789a --- /dev/null +++ b/test/oklch2rgb.test.js @@ -0,0 +1,39 @@ +import { describe, it, expect } from 'vitest'; +import limit from '../src/utils/limit.js'; +import oklch2rgb from '../src/io/oklch/oklch2rgb.js'; + +const round = (v) => limit(Math.round(v), 0, 255); + +describe('Testing LCH conversions', () => { + const testCases = { + black: { in: [0.0, 0.0, NaN], out: [0, 0, 0, 1] }, + white: { in: [1.0, 0.0, NaN], out: [255, 255, 255, 1] }, + gray: { in: [0.59987, 0.0, NaN], out: [128, 128, 128, 1] }, + red: { in: [0.62796, 0.25768, 29.233885192342633], out: [255, 0, 0, 1] }, + yellow: { in: [0.96798, 0.21101, 109.76923207652125], out: [255, 255, 0, 1] }, + green: { in: [0.51975, 0.17686, 142.49533888780996], out: [0, 128, 0, 1] }, + cyan: { in: [0.9054, 0.15455, 194.76894793196382], out: [0, 255, 255, 1] }, + blue: { in: [0.45201, 0.31321, 264.052020638055], out: [0, 0, 255, 1] }, + magenta: { in: [0.70167, 0.32249, 328.36341792345144], out: [255, 0, 255, 1] } + }; + + Object.keys(testCases).forEach((key) => { + const { in: input, out: expected } = testCases[key]; + + it(`should convert oklch to rgb for ${key}`, () => { + const result = oklch2rgb(input).map(round); + expect(result).toEqual(expected); + }); + + it(`should convert oklch to rgb for ${key} using arguments`, () => { + const result = oklch2rgb(...input).map(round); + expect(result).toEqual(expected); + }); + + it(`should convert oklch to rgb for ${key} using object`, () => { + const [l, c, h] = input; + const result = oklch2rgb({ l, c, h }).map(round); + expect(result).toEqual(expected); + }); + }); +}); diff --git a/test/random.test.cjs b/test/random.test.cjs deleted file mode 100644 index dc5c3595..00000000 --- a/test/random.test.cjs +++ /dev/null @@ -1,16 +0,0 @@ -require('es6-shim'); -const vows = require('vows'); -const assert = require('assert'); -const chroma = require('../chroma.cjs'); - -vows.describe('Some tests for random colors') - - .addBatch({ - 'random colors': { - topic: chroma.random(), - 'valid hex code'(topic) { - return assert(/^#[0-9a-f]{6}$/i.test(topic.hex())); - } - } - }) - .export(module); diff --git a/test/random.test.js b/test/random.test.js new file mode 100644 index 00000000..4b34a0a8 --- /dev/null +++ b/test/random.test.js @@ -0,0 +1,9 @@ +import { describe, it, expect } from 'vitest'; +import chroma from '../index.js'; + +describe('Some tests for random colors', () => { + it('should generate valid hex codes for random colors', () => { + const color = chroma.random(); + expect(/^#[0-9a-f]{6}$/i.test(color.hex())).toBe(true); + }); +}); diff --git a/test/rgb2cmyk.test.cjs b/test/rgb2cmyk.test.cjs deleted file mode 100644 index 0eec18f8..00000000 --- a/test/rgb2cmyk.test.cjs +++ /dev/null @@ -1,27 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import rgb2cmyk from '../src/io/cmyk/rgb2cmyk.js'; - -vows.describe('Testing CMYK color conversions') - .addBatch({ - 'parse simple CMYK colors': { - topic: { - black: { cmyk: [0, 0, 0, 1], rgb: [0, 0, 0, 1] }, - white: { cmyk: [0, 0, 0, 0], rgb: [255, 255, 255, 1] }, - red: { cmyk: [0, 1, 1, 0], rgb: [255, 0, 0, 1] }, - green: { cmyk: [1, 0, 1, 0], rgb: [0, 255, 0, 1] }, - blue: { cmyk: [1, 1, 0, 0], rgb: [0, 0, 255, 1] }, - yellow: { cmyk: [0, 0, 1, 0], rgb: [255, 255, 0, 1] }, - cyan: { cmyk: [1, 0, 0, 0], rgb: [0, 255, 255, 1] }, - magenta: { cmyk: [0, 1, 0, 0], rgb: [255, 0, 255, 1] } - }, - rgb2cmyk(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual(rgb2cmyk(topic[key].rgb), topic[key].cmyk); - }); - } - } - }) - .export(module); diff --git a/test/rgb2cmyk.test.js b/test/rgb2cmyk.test.js new file mode 100644 index 00000000..6e99ef55 --- /dev/null +++ b/test/rgb2cmyk.test.js @@ -0,0 +1,23 @@ +import { describe, it, expect } from 'vitest'; +import rgb2cmyk from '../src/io/cmyk/rgb2cmyk.js'; + +const tests = { + black: { cmyk: [0, 0, 0, 1], rgb: [0, 0, 0, 1] }, + white: { cmyk: [0, 0, 0, 0], rgb: [255, 255, 255, 1] }, + red: { cmyk: [0, 1, 1, 0], rgb: [255, 0, 0, 1] }, + green: { cmyk: [1, 0, 1, 0], rgb: [0, 255, 0, 1] }, + blue: { cmyk: [1, 1, 0, 0], rgb: [0, 0, 255, 1] }, + yellow: { cmyk: [0, 0, 1, 0], rgb: [255, 255, 0, 1] }, + cyan: { cmyk: [1, 0, 0, 0], rgb: [0, 255, 255, 1] }, + magenta: { cmyk: [0, 1, 0, 0], rgb: [255, 0, 255, 1] } +}; + +describe('Testing CMYK color conversions', () => { + Object.keys(tests).forEach((key) => { + const { cmyk, rgb } = tests[key]; + + it(`rgb2cmyk ${key} converts RGB to CMYK`, () => { + expect(rgb2cmyk(rgb)).toEqual(cmyk); + }); + }); +}); diff --git a/test/rgb2css.test.cjs b/test/rgb2css.test.cjs deleted file mode 100644 index 3ce8c79c..00000000 --- a/test/rgb2css.test.cjs +++ /dev/null @@ -1,52 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import rgb2css from '../src/io/css/rgb2css.js'; - -const tests = { - black: { rgb: [0, 0, 0], css: 'rgb(0,0,0)' }, - red: { rgb: [255, 0, 0], css: 'rgb(255,0,0)' }, - auto_rgba: { rgb: [255, 0, 0, 0.25], css: 'rgba(255,0,0,0.25)' }, - force_rgba: { rgb: [255, 0, 0], mode: 'rgba', css: 'rgba(255,0,0,1)' }, - hsl: { rgb: [255, 0, 0], mode: 'hsl', css: 'hsl(0,100%,50%)' }, - auto_hsla: { - rgb: [255, 0, 0, 0.5], - mode: 'hsl', - css: 'hsla(0,100%,50%,0.5)' - }, - force_hsla: { - rgb: [255, 255, 0, 0.75], - mode: 'hsl', - css: 'hsla(60,100%,50%,0.75)' - } -}; - -const batch = {}; - -Object.keys(tests).forEach((key) => { - batch[key] = { - topic: tests[key], - array(topic) { - assert.equal(rgb2css(topic.rgb, topic.mode || 'rgb'), topic.css); - }, - obj(topic) { - let [r, g, b] = topic.rgb; - let obj = { - r, - g, - b, - ...(topic.rgb.length > 3 ? { a: topic.rgb[3] } : {}) - }; - assert.equal(rgb2css(obj, topic.mode), topic.css); - }, - args(topic) { - if (topic.mode != 'rgb') return; - assert.deepStrictEqual(rgb2css.apply(null, topic.rgb), topic.hex); - } - }; -}); - -vows.describe('Testing rgb2css color conversions') - .addBatch(batch) - .export(module); diff --git a/test/rgb2css.test.js b/test/rgb2css.test.js new file mode 100644 index 00000000..9e852cef --- /dev/null +++ b/test/rgb2css.test.js @@ -0,0 +1,38 @@ +import { describe, it, expect } from 'vitest'; +import rgb2css from '../src/io/css/rgb2css.js'; + +const tests = { + black: { rgb: [0, 0, 0], css: 'rgb(0,0,0)' }, + red: { rgb: [255, 0, 0], css: 'rgb(255,0,0)' }, + auto_rgba: { rgb: [255, 0, 0, 0.25], css: 'rgba(255,0,0,0.25)' }, + force_rgba: { rgb: [255, 0, 0], mode: 'rgba', css: 'rgba(255,0,0,1)' }, + hsl: { rgb: [255, 0, 0], mode: 'hsl', css: 'hsl(0,100%,50%)' }, + auto_hsla: { rgb: [255, 0, 0, 0.5], mode: 'hsl', css: 'hsla(0,100%,50%,0.5)' }, + force_hsla: { rgb: [255, 255, 0, 0.75], mode: 'hsl', css: 'hsla(60,100%,50%,0.75)' } +}; + +describe('Testing rgb2css color conversions', () => { + Object.keys(tests).forEach((key) => { + const { rgb, mode, css } = tests[key]; + + it(`rgb2css ${key} converts array`, () => { + expect(rgb2css(rgb, mode || 'rgb')).toBe(css); + }); + + it(`rgb2css ${key} converts object`, () => { + const [r, g, b, a] = rgb; + const obj = { + r, + g, + b, + ...(a !== undefined ? { a } : {}) + }; + expect(rgb2css(obj, mode)).toBe(css); + }); + + it(`rgb2css ${key} converts arguments`, () => { + if (mode !== 'rgb') return; + expect(rgb2css(...rgb)).toBe(css); + }); + }); +}); diff --git a/test/rgb2hex.test.cjs b/test/rgb2hex.test.js similarity index 51% rename from test/rgb2hex.test.cjs rename to test/rgb2hex.test.js index c7a1a19d..771a7443 100644 --- a/test/rgb2hex.test.cjs +++ b/test/rgb2hex.test.js @@ -1,7 +1,4 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - +import { describe, it, expect } from 'vitest'; import rgb2hex from '../src/io/hex/rgb2hex.js'; const tests = { @@ -19,35 +16,28 @@ const tests = { force_rgb: { rgb: [255, 0, 255, 0.5], mode: 'rgb', hex: '#ff00ff' } }; -const batch = {}; +describe('Test rgb2hex color conversions', () => { + Object.keys(tests).forEach((key) => { + const { rgb, mode, hex } = tests[key]; + + it(`rgb2hex ${key} converts array`, () => { + expect(rgb2hex(rgb, mode || 'auto')).toBe(hex); + }); -Object.keys(tests).forEach((key) => { - batch[`rgb2hex ${key}`] = { - topic: tests[key], - array(topic) { - assert.deepStrictEqual( - rgb2hex(topic.rgb, topic.mode || 'auto'), - topic.hex - ); - }, - obj(topic) { - let [r, g, b] = topic.rgb; - let obj = { + it(`rgb2hex ${key} converts object`, () => { + const [r, g, b, a] = rgb; + const obj = { r, g, b, - ...(topic.rgb.length > 3 ? { a: topic.rgb[3] } : {}) + ...(a !== undefined ? { a } : {}) }; - assert.deepStrictEqual( - rgb2hex(obj, topic.mode || 'auto'), - topic.hex - ); - }, - args(topic) { - if (topic.mode != 'auto') return; - assert.deepStrictEqual(rgb2hex.apply(null, topic.rgb), topic.hex); - } - }; -}); + expect(rgb2hex(obj, mode || 'auto')).toBe(hex); + }); -vows.describe('Test rgb2hex color conversions').addBatch(batch).export(module); + it(`rgb2hex ${key} converts arguments`, () => { + if (mode !== 'auto') return; + expect(rgb2hex(...rgb)).toBe(hex); + }); + }); +}); diff --git a/test/rgb2hsi.test.cjs b/test/rgb2hsi.test.cjs deleted file mode 100644 index 0fc93a9c..00000000 --- a/test/rgb2hsi.test.cjs +++ /dev/null @@ -1,53 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import rgb2hsi from '../src/io/hsi/rgb2hsi.js'; - -const tests = { - black2: { hsi: [NaN, 0, 0], rgb: [0, 0, 0, 1] }, - white: { hsi: [NaN, 0, 1], rgb: [255, 255, 255, 1] }, - gray: { hsi: [NaN, 0, 0.5], rgb: [127.5, 127.5, 127.5, 1] }, - red: { hsi: [0, 1, 1 / 3], rgb: [255, 0, 0, 1] }, - yellow: { hsi: [60, 1, 2 / 3], rgb: [255, 255, 0, 1] }, - green: { hsi: [120, 1, 1 / 3], rgb: [0, 255, 0, 1] }, - cyan: { hsi: [180, 1, 2 / 3], rgb: [0, 255, 255, 1] }, - blue: { hsi: [240, 1, 1 / 3], rgb: [0, 0, 255, 1] }, - magenta: { hsi: [300, 1, 2 / 3], rgb: [255, 0, 255, 1] } -}; - -const round = (digits) => { - const d = Math.pow(10, digits); - return (v) => Math.round(v * d) / d; -}; -const rnd = round(5); - -const batch = {}; - -Object.keys(tests).forEach((key) => { - batch[`rgb2hsi ${key}`] = { - topic: tests[key], - array(topic) { - assert.deepStrictEqual( - rgb2hsi(topic.rgb).map(rnd), - topic.hsi.map(rnd) - ); - }, - obj(topic) { - let [r, g, b] = topic.rgb; - assert.deepStrictEqual( - rgb2hsi({ r, g, b }).map(rnd), - topic.hsi.map(rnd) - ); - }, - args(topic) { - if (topic.mode != 'auto') return; - assert.deepStrictEqual( - rgb2hsi.apply(null, topic.rgb).map(rnd), - topic.hsi.map(rnd) - ); - } - }; -}); - -vows.describe('Test rgb2hsi color conversions').addBatch(batch).export(module); diff --git a/test/rgb2hsi.test.js b/test/rgb2hsi.test.js new file mode 100644 index 00000000..e44d890d --- /dev/null +++ b/test/rgb2hsi.test.js @@ -0,0 +1,40 @@ +import { describe, it, expect } from 'vitest'; +import rgb2hsi from '../src/io/hsi/rgb2hsi.js'; + +const tests = { + black2: { hsi: [NaN, 0, 0], rgb: [0, 0, 0, 1] }, + white: { hsi: [NaN, 0, 1], rgb: [255, 255, 255, 1] }, + gray: { hsi: [NaN, 0, 0.5], rgb: [127.5, 127.5, 127.5, 1] }, + red: { hsi: [0, 1, 1 / 3], rgb: [255, 0, 0, 1] }, + yellow: { hsi: [60, 1, 2 / 3], rgb: [255, 255, 0, 1] }, + green: { hsi: [120, 1, 1 / 3], rgb: [0, 255, 0, 1] }, + cyan: { hsi: [180, 1, 2 / 3], rgb: [0, 255, 255, 1] }, + blue: { hsi: [240, 1, 1 / 3], rgb: [0, 0, 255, 1] }, + magenta: { hsi: [300, 1, 2 / 3], rgb: [255, 0, 255, 1] } +}; + +const round = (digits) => { + const d = Math.pow(10, digits); + return (v) => Math.round(v * d) / d; +}; +const rnd = round(5); + +describe('Test rgb2hsi color conversions', () => { + Object.keys(tests).forEach((key) => { + const { hsi, rgb } = tests[key]; + + it(`rgb2hsi ${key} converts array`, () => { + expect(rgb2hsi(rgb).map(rnd)).toEqual(hsi.map(rnd)); + }); + + it(`rgb2hsi ${key} converts object`, () => { + const [r, g, b] = rgb; + expect(rgb2hsi({ r, g, b }).map(rnd)).toEqual(hsi.map(rnd)); + }); + + it(`rgb2hsi ${key} converts arguments`, () => { + // Assuming `mode` is always 'auto' based on original code + expect(rgb2hsi(...rgb).map(rnd)).toEqual(hsi.map(rnd)); + }); + }); +}); diff --git a/test/rgb2hsv.test.cjs b/test/rgb2hsv.test.cjs deleted file mode 100644 index 2dc61c36..00000000 --- a/test/rgb2hsv.test.cjs +++ /dev/null @@ -1,37 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import rgb2hsv from '../src/io/hsv/rgb2hsv.js'; - -const tests = { - black: { hsv: [NaN, 0, 0], rgb: [0, 0, 0, 1] }, - white: { hsv: [NaN, 0, 1], rgb: [255, 255, 255, 1] }, - gray: { hsv: [NaN, 0, 0.5], rgb: [127.5, 127.5, 127.5, 1] }, - red: { hsv: [0, 1, 1], rgb: [255, 0, 0, 1] }, - yellow: { hsv: [60, 1, 1], rgb: [255, 255, 0, 1] }, - green: { hsv: [120, 1, 1], rgb: [0, 255, 0, 1] }, - cyan: { hsv: [180, 1, 1], rgb: [0, 255, 255, 1] }, - blue: { hsv: [240, 1, 1], rgb: [0, 0, 255, 1] }, - magenta: { hsv: [300, 1, 1], rgb: [255, 0, 255, 1] } -}; - -const batch = {}; - -Object.keys(tests).forEach((key) => { - batch[`rgb2hsv ${key}`] = { - topic: tests[key], - array(topic) { - assert.deepStrictEqual(rgb2hsv(topic.rgb), topic.hsv); - }, - obj(topic) { - let [r, g, b] = topic.rgb; - assert.deepStrictEqual(rgb2hsv({ r, g, b }), topic.hsv); - }, - args(topic) { - assert.deepStrictEqual(rgb2hsv.apply(null, topic.rgb), topic.hsv); - } - }; -}); - -vows.describe('Test rgb2hsv color conversions').addBatch(batch).export(module); diff --git a/test/rgb2hsv.test.js b/test/rgb2hsv.test.js new file mode 100644 index 00000000..098a60b3 --- /dev/null +++ b/test/rgb2hsv.test.js @@ -0,0 +1,33 @@ +import { describe, it, expect } from 'vitest'; +import rgb2hsv from '../src/io/hsv/rgb2hsv.js'; + +const tests = { + black: { hsv: [NaN, 0, 0], rgb: [0, 0, 0, 1] }, + white: { hsv: [NaN, 0, 1], rgb: [255, 255, 255, 1] }, + gray: { hsv: [NaN, 0, 0.5], rgb: [127.5, 127.5, 127.5, 1] }, + red: { hsv: [0, 1, 1], rgb: [255, 0, 0, 1] }, + yellow: { hsv: [60, 1, 1], rgb: [255, 255, 0, 1] }, + green: { hsv: [120, 1, 1], rgb: [0, 255, 0, 1] }, + cyan: { hsv: [180, 1, 1], rgb: [0, 255, 255, 1] }, + blue: { hsv: [240, 1, 1], rgb: [0, 0, 255, 1] }, + magenta: { hsv: [300, 1, 1], rgb: [255, 0, 255, 1] } +}; + +describe('Test rgb2hsv color conversions', () => { + Object.keys(tests).forEach((key) => { + const { hsv, rgb } = tests[key]; + + it(`rgb2hsv ${key} converts array`, () => { + expect(rgb2hsv(rgb)).toEqual(hsv); + }); + + it(`rgb2hsv ${key} converts object`, () => { + const [r, g, b] = rgb; + expect(rgb2hsv({ r, g, b })).toEqual(hsv); + }); + + it(`rgb2hsv ${key} converts arguments`, () => { + expect(rgb2hsv(...rgb)).toEqual(hsv); + }); + }); +}); diff --git a/test/rgb2lab.test.cjs b/test/rgb2lab.test.js similarity index 53% rename from test/rgb2lab.test.cjs rename to test/rgb2lab.test.js index 86fa9827..5f18e654 100644 --- a/test/rgb2lab.test.cjs +++ b/test/rgb2lab.test.js @@ -1,9 +1,15 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - +import { describe, it, expect } from 'vitest'; import rgb2lab from '../src/io/lab/rgb2lab.js'; +const round = (digits) => { + const d = Math.pow(10, digits); + return (v) => { + if (v > -1e-3 && v < 1e-3) v = 0; + return Math.round(v * d) / d; + }; +}; +const rnd = round(2); + const tests = { black: { lab: [0, 0, 0], rgb: [0, 0, 0] }, white: { lab: [100, 0, 0], rgb: [255, 255, 255] }, @@ -16,34 +22,21 @@ const tests = { magenta: { lab: [60.32, 98.23, -60.82], rgb: [255, 0, 255] } }; -const round = (digits) => { - const d = Math.pow(10, digits); - return (v) => { - if (v > -1e-3 && v < 1e-3) v = 0; - return Math.round(v * d) / d; - }; -}; -const rnd = round(2); +describe('Test rgb2lab color conversions', () => { + Object.keys(tests).forEach((key) => { + const { lab, rgb } = tests[key]; -const batch = {}; + it(`rgb2lab ${key} converts array`, () => { + expect(rgb2lab(rgb).map(rnd)).toEqual(lab); + }); -Object.keys(tests).forEach((key) => { - batch[`rgb2lab ${key}`] = { - topic: tests[key], - array(topic) { - assert.deepStrictEqual(rgb2lab(topic.rgb).map(rnd), topic.lab); - }, - obj(topic) { - let [r, g, b] = topic.rgb; - assert.deepStrictEqual(rgb2lab({ r, g, b }).map(rnd), topic.lab); - }, - args(topic) { - assert.deepStrictEqual( - rgb2lab.apply(null, topic.rgb).map(rnd), - topic.lab - ); - } - }; -}); + it(`rgb2lab ${key} converts object`, () => { + const [r, g, b] = rgb; + expect(rgb2lab({ r, g, b }).map(rnd)).toEqual(lab); + }); -vows.describe('Test rgb2lab color conversions').addBatch(batch).export(module); + it(`rgb2lab ${key} converts arguments`, () => { + expect(rgb2lab(...rgb).map(rnd)).toEqual(lab); + }); + }); +}); diff --git a/test/rgb2lch.test.cjs b/test/rgb2lch.test.js similarity index 53% rename from test/rgb2lch.test.cjs rename to test/rgb2lch.test.js index 66734abe..74a09bbb 100644 --- a/test/rgb2lch.test.cjs +++ b/test/rgb2lch.test.js @@ -1,9 +1,15 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - +import { describe, it, expect } from 'vitest'; import rgb2lch from '../src/io/lch/rgb2lch.js'; +const round = (digits) => { + const d = Math.pow(10, digits); + return (v) => { + if (v > -1e-3 && v < 1e-3) v = 0; + return Math.round(v * d) / d; + }; +}; +const rnd = round(2); + const tests = { black: { lch: [0, 0, NaN], rgb: [0, 0, 0] }, white: { lch: [100, 0, NaN], rgb: [255, 255, 255] }, @@ -16,34 +22,21 @@ const tests = { magenta: { lch: [60.32, 115.54, 328.23], rgb: [255, 0, 255] } }; -const round = (digits) => { - const d = Math.pow(10, digits); - return (v) => { - if (v > -1e-3 && v < 1e-3) v = 0; - return Math.round(v * d) / d; - }; -}; -const rnd = round(2); +describe('Test rgb2lch color conversions', () => { + Object.keys(tests).forEach((key) => { + const { lch, rgb } = tests[key]; -const batch = {}; + it(`rgb2lch ${key} converts array`, () => { + expect(rgb2lch(rgb).map(rnd)).toEqual(lch); + }); -Object.keys(tests).forEach((key) => { - batch[`rgb2lch ${key}`] = { - topic: tests[key], - array(topic) { - assert.deepStrictEqual(rgb2lch(topic.rgb).map(rnd), topic.lch); - }, - obj(topic) { - let [r, g, b] = topic.rgb; - assert.deepStrictEqual(rgb2lch({ r, g, b }).map(rnd), topic.lch); - }, - args(topic) { - assert.deepStrictEqual( - rgb2lch.apply(null, topic.rgb).map(rnd), - topic.lch - ); - } - }; -}); + it(`rgb2lch ${key} converts object`, () => { + const [r, g, b] = rgb; + expect(rgb2lch({ r, g, b }).map(rnd)).toEqual(lch); + }); -vows.describe('Test rgb2lch color conversions').addBatch(batch).export(module); + it(`rgb2lch ${key} converts arguments`, () => { + expect(rgb2lch(...rgb).map(rnd)).toEqual(lch); + }); + }); +}); diff --git a/test/rgb2oklab.test.cjs b/test/rgb2oklab.test.cjs deleted file mode 100644 index bcb9ff71..00000000 --- a/test/rgb2oklab.test.cjs +++ /dev/null @@ -1,60 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import limit from '../src/utils/limit.js'; -import rgb2oklab from '../src/io/oklab/rgb2oklab.js'; - -const round = (digits) => { - const d = Math.pow(10, digits); - return (v) => { - if (v > -1e-3 && v < 1e-3) v = 0; - return Math.round(v * d) / d; - }; -}; -const rnd = round(3); - -vows.describe('Testing OKLab color conversions') - .addBatch({ - 'rgb2oklab: parse simple OKLab colors': { - topic: { - black: { out: [0.0, 0.0, 0.0], in: [0, 0, 0, 1] }, - white: { out: [1.0, 0.0, 0.0], in: [255, 255, 255, 1] }, - gray: { out: [0.6, 0.0, 0.0], in: [128, 128, 128, 1] }, - red: { out: [0.628, 0.225, 0.126], in: [255, 0, 0, 1] }, - yellow: { out: [0.968, -0.071, 0.199], in: [255, 255, 0, 1] }, - green: { out: [0.52, -0.14, 0.108], in: [0, 128, 0, 1] }, - cyan: { out: [0.905, -0.149, -0.039], in: [0, 255, 255, 1] }, - blue: { out: [0.452, -0.032, -0.312], in: [0, 0, 255, 1] }, - magenta: { out: [0.702, 0.275, -0.169], in: [255, 0, 255, 1] } - }, - lab_arr(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual( - rgb2oklab(topic[key].in).map(rnd), - topic[key].out - ); - }); - }, - lab_args(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual( - rgb2oklab.apply(null, topic[key].in).map(rnd), - topic[key].out, - key - ); - }); - }, - lab_obj(topic) { - Object.keys(topic).forEach((key) => { - const [r, g, b] = topic[key].in; - assert.deepEqual( - rgb2oklab({ r, g, b }).map(rnd), - topic[key].out, - key - ); - }); - } - } - }) - .export(module); diff --git a/test/rgb2oklab.test.js b/test/rgb2oklab.test.js new file mode 100644 index 00000000..afaceddf --- /dev/null +++ b/test/rgb2oklab.test.js @@ -0,0 +1,42 @@ +import { describe, it, expect } from 'vitest'; +import rgb2oklab from '../src/io/oklab/rgb2oklab.js'; + +const round = (digits) => { + const d = Math.pow(10, digits); + return (v) => { + if (v > -1e-3 && v < 1e-3) v = 0; + return Math.round(v * d) / d; + }; +}; +const rnd = round(3); + +describe('Testing OKLab color conversions', () => { + const colors = { + black: { out: [0.0, 0.0, 0.0], in: [0, 0, 0, 1] }, + white: { out: [1.0, 0.0, 0.0], in: [255, 255, 255, 1] }, + gray: { out: [0.6, 0.0, 0.0], in: [128, 128, 128, 1] }, + red: { out: [0.628, 0.225, 0.126], in: [255, 0, 0, 1] }, + yellow: { out: [0.968, -0.071, 0.199], in: [255, 255, 0, 1] }, + green: { out: [0.52, -0.14, 0.108], in: [0, 128, 0, 1] }, + cyan: { out: [0.905, -0.149, -0.039], in: [0, 255, 255, 1] }, + blue: { out: [0.452, -0.032, -0.312], in: [0, 0, 255, 1] }, + magenta: { out: [0.702, 0.275, -0.169], in: [255, 0, 255, 1] } + }; + + Object.keys(colors).forEach((key) => { + const { out, in: rgb } = colors[key]; + + it(`rgb2oklab ${key} converts array`, () => { + expect(rgb2oklab(rgb).map(rnd)).toEqual(out); + }); + + it(`rgb2oklab ${key} converts arguments`, () => { + expect(rgb2oklab(...rgb).map(rnd)).toEqual(out); + }); + + it(`rgb2oklab ${key} converts object`, () => { + const [r, g, b] = rgb; + expect(rgb2oklab({ r, g, b }).map(rnd)).toEqual(out); + }); + }); +}); diff --git a/test/rgb2oklch.test.cjs b/test/rgb2oklch.test.js similarity index 52% rename from test/rgb2oklch.test.cjs rename to test/rgb2oklch.test.js index 14445d49..07725f8d 100644 --- a/test/rgb2oklch.test.cjs +++ b/test/rgb2oklch.test.js @@ -1,7 +1,4 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - +import { describe, it, expect } from 'vitest'; import rgb2oklch from '../src/io/oklch/rgb2oklch.js'; const tests = { @@ -25,30 +22,23 @@ const round = (digits) => { }; const rnd = round(3); -const batch = {}; +describe('Test rgb2oklch color conversions', () => { + Object.keys(tests).forEach((key) => { + const test = tests[key]; -Object.keys(tests).forEach((key) => { - batch[`rgb2oklch ${key}`] = { - topic: tests[key], - array(topic) { - assert.deepStrictEqual(rgb2oklch(topic.rgb).map(rnd), topic.oklch); - }, - obj(topic) { - let [r, g, b] = topic.rgb; - assert.deepStrictEqual( - rgb2oklch({ r, g, b }).map(rnd), - topic.oklch - ); - }, - args(topic) { - assert.deepStrictEqual( - rgb2oklch.apply(null, topic.rgb).map(rnd), - topic.oklch - ); - } - }; -}); + describe(`rgb2oklch ${key}`, () => { + it('converts array', () => { + expect(rgb2oklch(test.rgb).map(rnd)).toEqual(test.oklch); + }); -vows.describe('Test rgb2oklch color conversions') - .addBatch(batch) - .export(module); + it('converts object', () => { + const [r, g, b] = test.rgb; + expect(rgb2oklch({ r, g, b }).map(rnd)).toEqual(test.oklch); + }); + + it('converts arguments', () => { + expect(rgb2oklch.apply(null, test.rgb).map(rnd)).toEqual(test.oklch); + }); + }); + }); +}); diff --git a/test/scale.lcorrection.test.cjs b/test/scale.lcorrection.test.cjs deleted file mode 100644 index 413b0e1d..00000000 --- a/test/scale.lcorrection.test.cjs +++ /dev/null @@ -1,89 +0,0 @@ -require('es6-shim'); -const vows = require('vows'); -const assert = require('assert'); -const chroma = require('../chroma.cjs'); - -vows.describe('Testing lightess correction') - - .addBatch({ - 'simple two color linear interpolation': { - topic: { - f: chroma.scale(['white', 'black']).mode('lab') - }, - 'center L is 50'(topic) { - assert.equal(Math.round(topic.f(0.5).lab()[0]), 50); - } - }, - - 'hot - w/o correction': { - topic: { - f: chroma.scale(['white', 'yellow', 'red', 'black']).mode('lab') - }, - 'center L is 74'(topic) { - assert.equal(Math.round(topic.f(0.5).lab()[0]), 74); - } - }, - - 'hot - with correction': { - topic: { - f: chroma - .scale(['white', 'yellow', 'red', 'black']) - .mode('lab') - .correctLightness(true) - }, - 'center L is 50'(topic) { - assert.equal(Math.round(topic.f(0.5).lab()[0]), 50); - } - }, - - 'hot - w/o correction - domained [0,100]': { - topic: { - f: chroma - .scale(['white', 'yellow', 'red', 'black']) - .domain([0, 100]) - .mode('lab') - }, - 'center L is 74'(topic) { - assert.equal(Math.round(topic.f(50).lab()[0]), 74); - } - }, - - 'hot - with correction - domained [0,100]': { - topic: { - f: chroma - .scale(['white', 'yellow', 'red', 'black']) - .domain([0, 100]) - .mode('lab') - .correctLightness(true) - }, - 'center L is 50'(topic) { - assert.equal(Math.round(topic.f(50).lab()[0]), 50); - } - }, - - 'hot - w/o correction - domained [0,20,40,60,80,100]': { - topic: { - f: chroma - .scale(['white', 'yellow', 'red', 'black']) - .domain([0, 20, 40, 60, 80, 100]) - .mode('lab') - }, - 'center L is 74'(topic) { - assert.equal(Math.round(topic.f(50).lab()[0]), 74); - } - }, - - 'hot - with correction - domained [0,20,40,60,80,100]': { - topic: { - f: chroma - .scale(['white', 'yellow', 'red', 'black']) - .domain([0, 20, 40, 60, 80, 100]) - .mode('lab') - .correctLightness(true) - }, - 'center L is 50'(topic) { - assert.equal(Math.round(topic.f(50).lab()[0]), 50); - } - } - }) - .export(module); diff --git a/test/scale.lcorrection.test.js b/test/scale.lcorrection.test.js new file mode 100644 index 00000000..a34c9a93 --- /dev/null +++ b/test/scale.lcorrection.test.js @@ -0,0 +1,60 @@ +import { describe, it, expect } from 'vitest'; +import chroma from '../index.js'; + +describe('Testing lightness correction', () => { + describe('simple two color linear interpolation', () => { + const f = chroma.scale(['white', 'black']).mode('lab'); + + it('center L is 50', () => { + expect(Math.round(f(0.5).lab()[0])).toBe(50); + }); + }); + + describe('hot - w/o correction', () => { + const f = chroma.scale(['white', 'yellow', 'red', 'black']).mode('lab'); + + it('center L is 74', () => { + expect(Math.round(f(0.5).lab()[0])).toBe(74); + }); + }); + + describe('hot - with correction', () => { + const f = chroma.scale(['white', 'yellow', 'red', 'black']).mode('lab').correctLightness(true); + + it('center L is 50', () => { + expect(Math.round(f(0.5).lab()[0])).toBe(50); + }); + }); + + describe('hot - w/o correction - domained [0,100]', () => { + const f = chroma.scale(['white', 'yellow', 'red', 'black']).domain([0, 100]).mode('lab'); + + it('center L is 74', () => { + expect(Math.round(f(50).lab()[0])).toBe(74); + }); + }); + + describe('hot - with correction - domained [0,100]', () => { + const f = chroma.scale(['white', 'yellow', 'red', 'black']).domain([0, 100]).mode('lab').correctLightness(true); + + it('center L is 50', () => { + expect(Math.round(f(50).lab()[0])).toBe(50); + }); + }); + + describe('hot - w/o correction - domained [0,20,40,60,80,100]', () => { + const f = chroma.scale(['white', 'yellow', 'red', 'black']).domain([0, 20, 40, 60, 80, 100]).mode('lab'); + + it('center L is 74', () => { + expect(Math.round(f(50).lab()[0])).toBe(74); + }); + }); + + describe('hot - with correction - domained [0,20,40,60,80,100]', () => { + const f = chroma.scale(['white', 'yellow', 'red', 'black']).domain([0, 20, 40, 60, 80, 100]).mode('lab').correctLightness(true); + + it('center L is 50', () => { + expect(Math.round(f(50).lab()[0])).toBe(50); + }); + }); +}); diff --git a/test/scales.test.cjs b/test/scales.test.cjs deleted file mode 100644 index a25d3533..00000000 --- a/test/scales.test.cjs +++ /dev/null @@ -1,409 +0,0 @@ -require('es6-shim'); -const vows = require('vows'); -const assert = require('assert'); -const chroma = require('../chroma.cjs'); -import scale from '../src/generator/scale.js'; - -vows.describe('Some tests for scale()') - - .addBatch({ - 'simple rgb scale (white-->black)': { - topic: { - f: scale(['white', 'black']) - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#ffffff'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(0.5).hex(), '#808080'); - }, - 'ends black'(topic) { - assert.equal(topic.f(1).hex(), '#000000'); - } - }, - - 'simple hsv scale (white-->black)': { - topic: { - f: scale(['white', 'black']).mode('hsv') - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#ffffff'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(0.5).hex(), '#808080'); - }, - 'ends black'(topic) { - assert.equal(topic.f(1).hex(), '#000000'); - }, - colors(topic) { - assert.deepEqual(topic.f.colors(), ['#ffffff', '#000000']); - }, - 'colors start and end'(topic) { - assert.deepEqual(topic.f.colors(2), ['#ffffff', '#000000']); - }, - 'color mode'(topic) { - assert.deepEqual(topic.f.colors(2, 'rgb')[1], [0, 0, 0]); - }, - 'color mode null len'(topic) { - assert.equal(topic.f.colors(2, null).length, 2); - }, - 'color mode null'(topic) { - assert(topic.f.colors(2, null)[0]._rgb); - } - }, - - 'simple hsv scale (white-->black), classified': { - topic: { - f: scale(['white', 'black']).classes(7).mode('hsv') - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#ffffff'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(0.5).hex(), '#808080'); - }, - 'ends black'(topic) { - assert.equal(topic.f(1).hex(), '#000000'); - }, - colors(topic) { - assert.deepEqual(topic.f.colors(7), [ - '#ffffff', - '#d5d5d5', - '#aaaaaa', - '#808080', - '#555555', - '#2a2a2a', - '#000000' - ]); - } - }, - - 'simple lab scale (white-->black)': { - topic: { - f: scale(['white', 'black']).mode('lab') - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#ffffff'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(0.5).hex(), '#777777'); - }, - 'ends black'(topic) { - assert.equal(topic.f(1).hex(), '#000000'); - } - }, - - 'colorbrewer scale': { - topic: { - f: scale('RdYlGn') - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#a50026'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(0.5).hex(), '#ffffbf'); - }, - 'ends black'(topic) { - assert.equal(topic.f(1).hex(), '#006837'); - } - }, - - 'colorbrewer scale - domained': { - topic: { - f: scale('RdYlGn').domain([0, 100]) - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#a50026'); - }, - foo(topic) { - assert.notEqual(topic.f(10).hex(), '#ffffbf'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(50).hex(), '#ffffbf'); - }, - 'ends black'(topic) { - assert.equal(topic.f(100).hex(), '#006837'); - } - }, - - 'colorbrewer scale - lowercase': { - topic: { - f: scale('rdylgn') - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#a50026'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(0.5).hex(), '#ffffbf'); - }, - 'ends black'(topic) { - assert.equal(topic.f(1).hex(), '#006837'); - } - }, - - 'colorbrewer scale - domained - classified': { - topic: { - f: scale('RdYlGn').domain([0, 100]).classes(5) - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#a50026'); - }, - 10(topic) { - assert.equal(topic.f(10).hex(), '#a50026'); - }, - 'mid gray'(topic) { - assert.equal(topic.f(50).hex(), '#ffffbf'); - }, - 'ends black'(topic) { - assert.equal(topic.f(100).hex(), '#006837'); - }, - 'get colors'(topic) { - assert.deepEqual(topic.f.colors(5), [ - '#a50026', - '#f98e52', - '#ffffbf', - '#86cb67', - '#006837' - ]); - } - }, - - 'calling domain with no arguments': { - topic: { - f: scale('RdYlGn').domain([0, 100]).classes(5) - }, - 'returns domain'(topic) { - assert.deepEqual(topic.f.domain(), [0, 100]); - }, - 'returns classes'(topic) { - assert.deepEqual(topic.f.classes(), [0, 20, 40, 60, 80, 100]); - } - }, - - 'source array keeps untouched': { - topic: chroma.brewer.Blues.slice(0), - 'colors are an array'(colors) { - assert.equal(colors.length, 9); - }, - 'colors are strings'(colors) { - assert.equal(typeof colors[0], 'string'); - }, - 'creating a color scale'(colors) { - scale(colors); - assert(true); - }, - 'colors are still strings'(colors) { - assert.equal(typeof colors[0], 'string'); - } - }, - - 'domain with same min and max': { - topic: { - f: scale(['white', 'black']).domain([1, 1]) - }, - 'returns color'(topic) { - assert.deepEqual(topic.f(1).hex(), '#000000'); - } - }, - - 'simple num scale (white-->black)': { - topic: { - f: scale(['white', 'black']).mode('num') - }, - 'starts white'(topic) { - assert.equal(topic.f(0).hex(), '#ffffff'); - }, - '25%'(topic) { - assert.equal(topic.f(0.25).hex(), '#bfffff'); - }, - '50%'(topic) { - assert.equal(topic.f(0.5).hex(), '#7fffff'); - }, - '75%'(topic) { - assert.equal(topic.f(0.75).hex(), '#3fffff'); - }, - '95%'(topic) { - assert.equal(topic.f(0.95).hex(), '#0ccccc'); - }, - 'ends black'(topic) { - assert.equal(topic.f(1).hex(), '#000000'); - } - }, - - 'css rgb colors': { - topic: scale('YlGnBu')(0.3).css(), - 'have rounded rgb() values'(topic) { - assert.equal(topic, 'rgb(170,222,183)'); - } - }, - - 'css rgba colors': { - topic: scale('YlGnBu')(0.3).alpha(0.675).css(), - 'dont round alpha value'(topic) { - assert.equal(topic, 'rgba(170,222,183,0.675)'); - } - }, - - 'get colors from a scale': { - topic: { - f: scale(['yellow', 'darkgreen']) - }, - 'just colors'(topic) { - assert.deepEqual(topic.f.colors(), ['#ffff00', '#006400']); - }, - 'five hex colors'(topic) { - assert.deepEqual(topic.f.colors(5), [ - '#ffff00', - '#bfd800', - '#80b200', - '#408b00', - '#006400' - ]); - }, - 'three css colors'(topic) { - assert.deepEqual(topic.f.colors(3, 'css'), [ - 'rgb(255,255,0)', - 'rgb(128,178,0)', - 'rgb(0,100,0)' - ]); - } - }, - - 'get colors from a scale with more than two colors': { - topic: { - f: scale(['yellow', 'orange', 'darkgreen']) - }, - 'just origianl colors'(topic) { - assert.deepEqual(topic.f.colors(), [ - '#ffff00', - '#ffa500', - '#006400' - ]); - } - }, - - 'test example in readme': { - topic: { - f: scale('RdYlGn') - }, - 'five hex colors (new)'(topic) { - assert.deepEqual(topic.f.colors(5), [ - '#a50026', - '#f98e52', - '#ffffbf', - '#86cb67', - '#006837' - ]); - } - }, - - 'weird result': { - topic: { - f: scale([ - [0, 0, 0, 1], - [255, 255, 255, 1] - ]) - .domain([0, 10]) - .mode('rgb') - }, - 'has hex function at 0.5'(topic) { - assert.equal(typeof topic.f(0.5).hex, 'function'); - }, - 'has hex function at 0'(topic) { - assert.equal(typeof topic.f(0).hex, 'function'); - } - }, - - 'scale padding, simple': { - topic: { - f: scale('RdYlBu').padding(0.15) - }, - 0(topic) { - assert.equal(topic.f(0).hex(), '#e64f35'); - }, - 0.5(topic) { - assert.equal(topic.f(0.5).hex(), '#ffffbf'); - }, - 1(topic) { - assert.equal(topic.f(1).hex(), '#5d91c3'); - } - }, - - 'scale padding, one-sided': { - topic: { - f: scale('OrRd').padding([0.2, 0]) - }, - 0(topic) { - assert.equal(topic.f(0).hex(), '#fddcaf'); - }, - 0.5(topic) { - assert.equal(topic.f(0.5).hex(), '#f26d4b'); - }, - 1(topic) { - assert.equal(topic.f(1).hex(), '#7f0000'); - } - }, - - 'colors return original colors': { - topic: { - f: scale(['red', 'white', 'blue']) - }, - 'same colors'(topic) { - assert.deepEqual(topic.f.colors(), [ - '#ff0000', - '#ffffff', - '#0000ff' - ]); - } - }, - - 'scale with one color': { - topic: { - f: scale(['red']) - }, - 'should return that color'(topic) { - assert.equal(topic.f(0.3).hex(), '#ff0000'); - } - }, - - 'scale() no data color': { - topic: { - f: scale('OrRd') - }, - 'null --> nodata'(topic) { - assert.equal(topic.f(null).hex(), '#cccccc'); - }, - 'undefined --> nodata'(topic) { - assert.equal(topic.f(undefined).hex(), '#cccccc'); - }, - 'custom nodata color'(topic) { - assert.equal( - topic.f.nodata('#eee')(undefined).hex(), - '#eeeeee' - ); - } - }, - - 'scale wrapped in a scale': { - topic: { - f1: scale('OrRd'), - f: scale('OrRd').domain([0, 0.25, 1]) - }, - start(topic) { - assert.equal(topic.f(0).hex(), topic.f1(0).hex()); - }, - end(topic) { - assert.equal(topic.f(1).hex(), topic.f1(1).hex()); - }, - center(topic) { - assert.equal(topic.f(0.125).hex(), topic.f1(0.25).hex()); - }, - center2(topic) { - assert.equal(topic.f(0.25).hex(), topic.f1(0.5).hex()); - }, - center3(topic) { - assert.equal(topic.f(0.625).hex(), topic.f1(0.75).hex()); - } - } - }) - .export(module); diff --git a/test/scales.test.js b/test/scales.test.js new file mode 100644 index 00000000..2d262d76 --- /dev/null +++ b/test/scales.test.js @@ -0,0 +1,408 @@ +import { describe, it, expect } from 'vitest'; +import chroma from '../index.js'; +import scale from '../src/generator/scale.js'; + +describe('Some tests for scale()', () => { + describe('simple rgb scale (white-->black)', () => { + const f = scale(['white', 'black']); + + it('starts white', () => { + expect(f(0).hex()).toBe('#ffffff'); + }); + + it('mid gray', () => { + expect(f(0.5).hex()).toBe('#808080'); + }); + + it('ends black', () => { + expect(f(1).hex()).toBe('#000000'); + }); + }); + + describe('simple hsv scale (white-->black)', () => { + const f = scale(['white', 'black']).mode('hsv'); + + it('starts white', () => { + expect(f(0).hex()).toBe('#ffffff'); + }); + + it('mid gray', () => { + expect(f(0.5).hex()).toBe('#808080'); + }); + + it('ends black', () => { + expect(f(1).hex()).toBe('#000000'); + }); + + it('colors', () => { + expect(f.colors()).toEqual(['#ffffff', '#000000']); + }); + + it('colors start and end', () => { + expect(f.colors(2)).toEqual(['#ffffff', '#000000']); + }); + + it('color mode', () => { + expect(f.colors(2, 'rgb')[1]).toEqual([0, 0, 0]); + }); + + it('color mode null len', () => { + expect(f.colors(2, null).length).toBe(2); + }); + + it('color mode null', () => { + expect(f.colors(2, null)[0]._rgb).toBeDefined(); + }); + }); + + describe('simple hsv scale (white-->black), classified', () => { + const f = scale(['white', 'black']).classes(7).mode('hsv'); + + it('starts white', () => { + expect(f(0).hex()).toBe('#ffffff'); + }); + + it('mid gray', () => { + expect(f(0.5).hex()).toBe('#808080'); + }); + + it('ends black', () => { + expect(f(1).hex()).toBe('#000000'); + }); + + it('colors', () => { + expect(f.colors(7)).toEqual(['#ffffff', '#d5d5d5', '#aaaaaa', '#808080', '#555555', '#2a2a2a', '#000000']); + }); + }); + + describe('simple lab scale (white-->black)', () => { + const f = scale(['white', 'black']).mode('lab'); + + it('starts white', () => { + expect(f(0).hex()).toBe('#ffffff'); + }); + + it('mid gray', () => { + expect(f(0.5).hex()).toBe('#777777'); + }); + + it('ends black', () => { + expect(f(1).hex()).toBe('#000000'); + }); + }); + + describe('colorbrewer scale', () => { + const f = scale('RdYlGn'); + + it('starts white', () => { + expect(f(0).hex()).toBe('#a50026'); + }); + + it('mid gray', () => { + expect(f(0.5).hex()).toBe('#ffffbf'); + }); + + it('ends black', () => { + expect(f(1).hex()).toBe('#006837'); + }); + }); + + describe('colorbrewer scale - domained', () => { + const f = scale('RdYlGn').domain([0, 100]); + + it('starts white', () => { + expect(f(0).hex()).toBe('#a50026'); + }); + + it('foo', () => { + expect(f(10).hex()).not.toBe('#ffffbf'); + }); + + it('mid gray', () => { + expect(f(50).hex()).toBe('#ffffbf'); + }); + + it('ends black', () => { + expect(f(100).hex()).toBe('#006837'); + }); + }); + + describe('colorbrewer scale - lowercase', () => { + const f = scale('rdylgn'); + + it('starts white', () => { + expect(f(0).hex()).toBe('#a50026'); + }); + + it('mid gray', () => { + expect(f(0.5).hex()).toBe('#ffffbf'); + }); + + it('ends black', () => { + expect(f(1).hex()).toBe('#006837'); + }); + }); + + describe('colorbrewer scale - domained - classified', () => { + const f = scale('RdYlGn').domain([0, 100]).classes(5); + + it('starts white', () => { + expect(f(0).hex()).toBe('#a50026'); + }); + + it('10', () => { + expect(f(10).hex()).toBe('#a50026'); + }); + + it('mid gray', () => { + expect(f(50).hex()).toBe('#ffffbf'); + }); + + it('ends black', () => { + expect(f(100).hex()).toBe('#006837'); + }); + + it('get colors', () => { + expect(f.colors(5)).toEqual(['#a50026', '#f98e52', '#ffffbf', '#86cb67', '#006837']); + }); + }); + + describe('calling domain with no arguments', () => { + const f = scale('RdYlGn').domain([0, 100]).classes(5); + + it('returns domain', () => { + expect(f.domain()).toEqual([0, 100]); + }); + + it('returns classes', () => { + expect(f.classes()).toEqual([0, 20, 40, 60, 80, 100]); + }); + }); + + describe('source array keeps untouched', () => { + const colors = chroma.brewer.Blues.slice(0); + + it('colors are an array', () => { + expect(colors.length).toBe(9); + }); + + it('colors are strings', () => { + expect(typeof colors[0]).toBe('string'); + }); + + it('creating a color scale', () => { + scale(colors); + expect(true).toBe(true); + }); + + it('colors are still strings', () => { + expect(typeof colors[0]).toBe('string'); + }); + }); + + describe('domain with same min and max', () => { + const f = scale(['white', 'black']).domain([1, 1]); + + it('returns color', () => { + expect(f(1).hex()).toBe('#000000'); + }); + }); + + describe('simple num scale (white-->black)', () => { + const f = scale(['white', 'black']).mode('num'); + + it('starts white', () => { + expect(f(0).hex()).toBe('#ffffff'); + }); + + it('25%', () => { + expect(f(0.25).hex()).toBe('#bfffff'); + }); + + it('50%', () => { + expect(f(0.5).hex()).toBe('#7fffff'); + }); + + it('75%', () => { + expect(f(0.75).hex()).toBe('#3fffff'); + }); + + it('95%', () => { + expect(f(0.95).hex()).toBe('#0ccccc'); + }); + + it('ends black', () => { + expect(f(1).hex()).toBe('#000000'); + }); + }); + + describe('css rgb colors', () => { + const color = scale('YlGnBu')(0.3).css(); + + it('have rounded rgb() values', () => { + expect(color).toBe('rgb(170,222,183)'); + }); + }); + + describe('css rgba colors', () => { + const color = scale('YlGnBu')(0.3).alpha(0.675).css(); + + it('dont round alpha value', () => { + expect(color).toBe('rgba(170,222,183,0.675)'); + }); + }); + + describe('get colors from a scale', () => { + const f = scale(['yellow', 'darkgreen']); + + it('just colors', () => { + expect(f.colors()).toEqual(['#ffff00', '#006400']); + }); + + it('five hex colors', () => { + expect(f.colors(5)).toEqual(['#ffff00', '#bfd800', '#80b200', '#408b00', '#006400']); + }); + + it('three css colors', () => { + expect(f.colors(3, 'css')).toEqual(['rgb(255,255,0)', 'rgb(128,178,0)', 'rgb(0,100,0)']); + }); + }); + + describe('get colors from a scale', () => { + const f = scale(['yellow', 'darkgreen']); + + it('just colors', () => { + expect(f.colors()).toEqual(['#ffff00', '#006400']); + }); + + it('five hex colors', () => { + expect(f.colors(5)).toEqual(['#ffff00', '#bfd800', '#80b200', '#408b00', '#006400']); + }); + + it('three css colors', () => { + expect(f.colors(3, 'css')).toEqual(['rgb(255,255,0)', 'rgb(128,178,0)', 'rgb(0,100,0)']); + }); + }); + + describe('get colors from a scale with more than two colors', () => { + const f = scale(['yellow', 'orange', 'darkgreen']); + + it('just original colors', () => { + expect(f.colors()).toEqual(['#ffff00', '#ffa500', '#006400']); + }); + }); + + describe('test example in readme', () => { + const f = scale('RdYlGn'); + + it('five hex colors (new)', () => { + expect(f.colors(5)).toEqual(['#a50026', '#f98e52', '#ffffbf', '#86cb67', '#006837']); + }); + }); + + describe('weird result', () => { + const f = scale([ + [0, 0, 0, 1], + [255, 255, 255, 1] + ]) + .domain([0, 10]) + .mode('rgb'); + + it('has hex function at 0.5', () => { + expect(typeof f(0.5).hex).toBe('function'); + }); + + it('has hex function at 0', () => { + expect(typeof f(0).hex).toBe('function'); + }); + }); + + describe('scale padding, simple', () => { + const f = scale('RdYlBu').padding(0.15); + + it('0', () => { + expect(f(0).hex()).toBe('#e64f35'); + }); + + it('0.5', () => { + expect(f(0.5).hex()).toBe('#ffffbf'); + }); + + it('1', () => { + expect(f(1).hex()).toBe('#5d91c3'); + }); + }); + + describe('scale padding, one-sided', () => { + const f = scale('OrRd').padding([0.2, 0]); + + it('0', () => { + expect(f(0).hex()).toBe('#fddcaf'); + }); + + it('0.5', () => { + expect(f(0.5).hex()).toBe('#f26d4b'); + }); + + it('1', () => { + expect(f(1).hex()).toBe('#7f0000'); + }); + }); + + describe('colors return original colors', () => { + const f = scale(['red', 'white', 'blue']); + + it('same colors', () => { + expect(f.colors()).toEqual(['#ff0000', '#ffffff', '#0000ff']); + }); + }); + + describe('scale with one color', () => { + const f = scale(['red']); + + it('should return that color', () => { + expect(f(0.3).hex()).toBe('#ff0000'); + }); + }); + + describe('scale() no data color', () => { + const f = scale('OrRd'); + + it('null --> nodata', () => { + expect(f(null).hex()).toBe('#cccccc'); + }); + + it('undefined --> nodata', () => { + expect(f(undefined).hex()).toBe('#cccccc'); + }); + + it('custom nodata color', () => { + expect(f.nodata('#eee')(undefined).hex()).toBe('#eeeeee'); + }); + }); + + describe('scale wrapped in a scale', () => { + const f1 = scale('OrRd'); + const f = scale('OrRd').domain([0, 0.25, 1]); + + it('start', () => { + expect(f(0).hex()).toBe(f1(0).hex()); + }); + + it('end', () => { + expect(f(1).hex()).toBe(f1(1).hex()); + }); + + it('center', () => { + expect(f(0.125).hex()).toBe(f1(0.25).hex()); + }); + + it('center2', () => { + expect(f(0.25).hex()).toBe(f1(0.5).hex()); + }); + + it('center3', () => { + expect(f(0.625).hex()).toBe(f1(0.75).hex()); + }); + }); +}); diff --git a/test/temperature2rgb.test.cjs b/test/temperature2rgb.test.cjs deleted file mode 100644 index 6a45cc24..00000000 --- a/test/temperature2rgb.test.cjs +++ /dev/null @@ -1,30 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import temperature2rgb from '../src/io/temp/temperature2rgb.js'; -const { round } = Math; - -vows.describe('Testing Kelvin color conversions') - .addBatch({ - 'parse simple kelvin colors': { - topic: { - '1k': { in: 1000, out: [255, 58, 0, 1] }, - '4k': { in: 4000, out: [255, 208, 164, 1] }, - '5k': { in: 5000, out: [255, 228, 205, 1] }, - '7k': { in: 7000, out: [245, 243, 255, 1] }, - '10k': { in: 10000, out: [204, 220, 255, 1] }, - '20k': { in: 20000, out: [168, 197, 255, 1] }, - '30k': { in: 30000, out: [159, 190, 255, 1] } - }, - hsv_arr(topic) { - Object.keys(topic).forEach((key) => { - assert.deepEqual( - temperature2rgb(topic[key].in).map(round), - topic[key].out - ); - }); - } - } - }) - .export(module); diff --git a/test/temperature2rgb.test.js b/test/temperature2rgb.test.js new file mode 100644 index 00000000..16422306 --- /dev/null +++ b/test/temperature2rgb.test.js @@ -0,0 +1,22 @@ +import { describe, it, expect } from 'vitest'; +import temperature2rgb from '../src/io/temp/temperature2rgb.js'; + +const { round } = Math; + +describe('Testing Kelvin color conversions', () => { + const cases = { + '1k': { in: 1000, out: [255, 58, 0, 1] }, + '4k': { in: 4000, out: [255, 208, 164, 1] }, + '5k': { in: 5000, out: [255, 228, 205, 1] }, + '7k': { in: 7000, out: [245, 243, 255, 1] }, + '10k': { in: 10000, out: [204, 220, 255, 1] }, + '20k': { in: 20000, out: [168, 197, 255, 1] }, + '30k': { in: 30000, out: [159, 190, 255, 1] } + }; + + Object.keys(cases).forEach((key) => { + it(`parse simple kelvin colors for ${key}`, () => { + expect(temperature2rgb(cases[key].in).map(round)).toEqual(cases[key].out); + }); + }); +}); diff --git a/test/unpack.test.cjs b/test/unpack.test.cjs deleted file mode 100644 index f9fd1814..00000000 --- a/test/unpack.test.cjs +++ /dev/null @@ -1,29 +0,0 @@ -const vows = require('vows'); -const assert = require('assert'); -require('es6-shim'); - -import unpack from '../src/utils/unpack.js'; - -// const round = (digits) => { -// const d = Math.pow(10,digits); -// return (v) => Math.round(v*d) / d; -// } - -vows.describe('Testing unpack') - .addBatch({ - 'parse simple CMYK colors': { - args() { - return assert.deepEqual(unpack([1, 2, 3, 4]), [1, 2, 3, 4]); - }, - array() { - return assert.deepEqual(unpack([[1, 2, 3, 4]]), [1, 2, 3, 4]); - }, - object() { - return assert.deepEqual( - unpack([{ c: 1, m: 2, y: 3, k: 4 }], 'cmyk'), - [1, 2, 3, 4] - ); - } - } - }) - .export(module); diff --git a/test/unpack.test.js b/test/unpack.test.js new file mode 100644 index 00000000..95d0835e --- /dev/null +++ b/test/unpack.test.js @@ -0,0 +1,16 @@ +import { describe, it, expect } from 'vitest'; +import unpack from '../src/utils/unpack.js'; + +describe('Testing unpack', () => { + it('parse simple CMYK colors (args)', () => { + expect(unpack([1, 2, 3, 4])).toEqual([1, 2, 3, 4]); + }); + + it('parse simple CMYK colors (array)', () => { + expect(unpack([[1, 2, 3, 4]])).toEqual([1, 2, 3, 4]); + }); + + it('parse simple CMYK colors (object)', () => { + expect(unpack([{ c: 1, m: 2, y: 3, k: 4 }], 'cmyk')).toEqual([1, 2, 3, 4]); + }); +}); diff --git a/test/valid.test.cjs b/test/valid.test.cjs deleted file mode 100644 index f6021ea4..00000000 --- a/test/valid.test.cjs +++ /dev/null @@ -1,22 +0,0 @@ -require('es6-shim'); -const vows = require('vows'); -const assert = require('assert'); -const chroma = require('../chroma.cjs'); - -vows.describe('Some tests for chroma.valid') - - .addBatch({ - 'valid color': { - topic: chroma.valid('red'), - 'is true'(topic) { - return assert(topic); - } - }, - 'invalid color': { - topic: chroma.valid('bread'), - 'is false'(topic) { - return assert(!topic); - } - } - }) - .export(module); diff --git a/test/valid.test.js b/test/valid.test.js new file mode 100644 index 00000000..96f9da13 --- /dev/null +++ b/test/valid.test.js @@ -0,0 +1,12 @@ +import { describe, it, expect } from 'vitest'; +import chroma from '../index.js'; + +describe('Some tests for chroma.valid', () => { + it('valid color', () => { + expect(chroma.valid('red')).toBe(true); + }); + + it('invalid color', () => { + expect(chroma.valid('bread')).toBe(false); + }); +});