UNPKG

3.5 kBJavaScriptView Raw
1
2const colorsys = require('../colorsys')
3const chai = require('chai')
4
5chai.should()
6const expect = chai.expect
7
8describe('colorsys', function() {
9 it('should convert rgb <-> hsl', function() {
10 const hsl = colorsys.rgb_to_hsl({ r: 255, g: 255, b: 255 })
11 expect(hsl).to.deep.equal({ h: 0 , s: 0 , l: 100 })
12 const rgb = colorsys.hsl_to_rgb(hsl)
13 expect(rgb).to.deep.equal({ r: 255, g: 255, b: 255 })
14 })
15
16 it('should convert hex <-> rgb', function() {
17 const hex = colorsys.rgb_to_hex({ r: 255, g: 0, b: 255 })
18 expect(hex).to.equal('#ff00ff')
19 expect(colorsys.hex_to_rgb('#66d7a9'))
20 .to.deep.equal({r: 102, g: 215, b: 169})
21 const rgb = colorsys.hex_to_rgb(hex)
22 expect(rgb).to.deep.equal({ r: 255, g: 0, b: 255 })
23 })
24
25 /* There is a small deviation when using negative angles */
26 it('should convert hex <-> hsv', function () {
27 const blueHsv = { h: 230, s: 80, v: 100 }
28 const blueHex = '#3456fe'
29 expect(colorsys.hex2Hsv(blueHex)).to.deep.equal(blueHsv)
30 })
31
32 /* There is a small deviation when using negative angles */
33 it('should convert hex <-> hsv', function () {
34 const blueHsv = { h: 230, s: 80, v: 100 }
35 const blueHex = '#3456fe'
36 expect(colorsys.hex2Hsv(blueHex)).to.deep.equal(blueHsv)
37 })
38
39 it('should convert saturated hues to white', function () {
40 const hsv = { h: 147, s: 0, v: 100 }
41 const hsl = { h: 147, s: 0, l: 100 }
42 const cmyk = { c: 0, m: 0, y: 0, k: 0 }
43 const white = { r: 255, g: 255, b: 255 }
44 expect(colorsys.hsv2Rgb(hsv)).to.deep.equal(white)
45 expect(colorsys.hsl2Rgb(hsl)).to.deep.equal(white)
46 expect(colorsys.cmyk2Rgb(cmyk)).to.deep.equal(white)
47 })
48
49 it('should parse from css', function () {
50 const expectedHsla = { h: 140, s: 30, l: 21, a: 0.5 }
51 const hslaString = 'hsla(140, 30%, 21%, .5)'
52 expect(colorsys.parseCss(hslaString)).to.deep.equal(expectedHsla)
53 })
54
55 it('should parse css string as rgb by default', function () {
56 const hexParsed = colorsys.parseCss('#aacc33')
57 expect(hexParsed).to.deep.equal(colorsys.hex2Rgb('#aacc33'))
58 })
59
60 it('should stringify color objects', function () {
61 const rgbObj = colorsys.parseCss('#aacc33')
62 expect(colorsys.stringify(rgbObj)).to.equal('rgb(170, 204, 51)')
63 const hsvObj = colorsys.rgb2Hsv(rgbObj)
64 expect(colorsys.stringify(hsvObj)).to.equal('hsv(73, 75%, 80%)')
65 })
66
67 /* There is a small deviation when using negative angles */
68 it('should handle negative angles', function () {
69 const blueHsl = { h: -130, s: 99, l: 60 }
70 const blueHex = '#3456fe'
71 const greenHsv = { h: 127, s: 73, v: 96 }
72 const greenRgb = { r: 66, g: 244, b: 86 }
73 expect(colorsys.hsl2Hex(blueHsl)).to.equal(blueHex)
74 expect(colorsys.hsv2Rgb(greenHsv)).to.deep.equal(greenRgb)
75 })
76
77 it('should create a random colour in hex', function () {
78 const hex = colorsys.random()
79 const rgb = colorsys.hex2Rgb(hex)
80 expect(hex.length).to.equal(7)
81 expect(hex[0]).to.equal('#')
82 expect(colorsys.hex2Hsl(hex)).to.deep.equal(colorsys.rgb2Hsl(rgb))
83 })
84
85 it('should rotate hue returning a number within 360º', function () {
86 const hsl = { h: 280, s: 100, l: 50 }
87 const hsv = { h: 0, s: 100, l: 50 }
88
89 expect(colorsys.rotateHue(140)).to.equal(140)
90 expect(colorsys.rotateHue(120, 240)).to.equal(0)
91 expect(colorsys.rotateHue(160, -400)).to.equal(120)
92 expect(colorsys.rotateHue(hsl, 130)).to.contain({h: 50})
93 expect(colorsys.rotateHue(hsv, -324)).to.contain({h: 36})
94 })
95})
96