})
it('handles black', () => {
- let {r, g, b} = new SVG.Color(0, 0, 0).lab().rgb()
- expect( r ).toBeCloseTo(0, 0)
- expect( g ).toBeCloseTo(0, 0)
- expect( b ).toBeCloseTo(0, 0)
+ let color = new SVG.Color(0, 0, 0).lab().rgb()
+ expect( color.r ).toBeCloseTo(0, 0)
+ expect( color.g ).toBeCloseTo(0, 0)
+ expect( color.b ).toBeCloseTo(0, 0)
+ expect( color.toHex() ).toBe('#000000')
})
it('handles white', () => {
- let {r, g, b} = new SVG.Color(255, 255, 255).lab().rgb()
- expect( r ).toBeCloseTo(255, 0)
- expect( g ).toBeCloseTo(255, 0)
- expect( b ).toBeCloseTo(255, 0)
+ let color = new SVG.Color(255, 255, 255).lab().rgb()
+ expect( color.r ).toBeCloseTo(255, 0)
+ expect( color.g ).toBeCloseTo(255, 0)
+ expect( color.b ).toBeCloseTo(255, 0)
+ expect( color.toHex() ).toBe('#ffffff')
})
})
})
it('handles black', () => {
- let {r, g, b} = new SVG.Color(0, 0, 0).lch().rgb()
- expect( r ).toBeCloseTo(0, 0)
- expect( g ).toBeCloseTo(0, 0)
- expect( b ).toBeCloseTo(0, 0)
+ let color = new SVG.Color(0, 0, 0).lch().rgb()
+ expect( color.r ).toBeCloseTo(0, 0)
+ expect( color.g ).toBeCloseTo(0, 0)
+ expect( color.b ).toBeCloseTo(0, 0)
+ expect( color.toHex() ).toBe('#000000')
})
it('handles white', () => {
- let {r, g, b} = new SVG.Color(255, 255, 255).lch().rgb()
- expect( r ).toBeCloseTo(255, 0)
- expect( g ).toBeCloseTo(255, 0)
- expect( b ).toBeCloseTo(255, 0)
+ let color = new SVG.Color(255, 255, 255).lch().rgb()
+ expect( color.r ).toBeCloseTo(255, 0)
+ expect( color.g ).toBeCloseTo(255, 0)
+ expect( color.b ).toBeCloseTo(255, 0)
+ expect( color.toHex() ).toBe('#ffffff')
})
})
})
it('handles black', () => {
- let {r, g, b} = new SVG.Color(0, 0, 0).hsl().rgb()
- expect( r ).toBeCloseTo(0, 0)
- expect( g ).toBeCloseTo(0, 0)
- expect( b ).toBeCloseTo(0, 0)
+ let color = new SVG.Color(0, 0, 0).hsl().rgb()
+ expect( color.r ).toBeCloseTo(0, 0)
+ expect( color.g ).toBeCloseTo(0, 0)
+ expect( color.b ).toBeCloseTo(0, 0)
+ expect( color.toHex() ).toBe('#000000')
})
it('handles white', () => {
- let {r, g, b} = new SVG.Color(255, 255, 255).hsl().rgb()
- expect( r ).toBeCloseTo(255, 0)
- expect( g ).toBeCloseTo(255, 0)
- expect( b ).toBeCloseTo(255, 0)
+ let color = new SVG.Color(255, 255, 255).hsl().rgb()
+ expect( color.r ).toBeCloseTo(255, 0)
+ expect( color.g ).toBeCloseTo(255, 0)
+ expect( color.b ).toBeCloseTo(255, 0)
+ expect( color.toHex() ).toBe('#ffffff')
})
})
})
it('handles black', () => {
- let {r, g, b} = new SVG.Color(0, 0, 0).cmyk().rgb()
- expect( r ).toBeCloseTo(0, 0)
- expect( g ).toBeCloseTo(0, 0)
- expect( b ).toBeCloseTo(0, 0)
+ let color = new SVG.Color(0, 0, 0).cmyk().rgb()
+ expect( color.r ).toBeCloseTo(0, 0)
+ expect( color.g ).toBeCloseTo(0, 0)
+ expect( color.b ).toBeCloseTo(0, 0)
+ expect( color.toHex() ).toBe('#000000')
})
it('handles white', () => {
- let {r, g, b} = new SVG.Color(255, 255, 255).cmyk().rgb()
- expect( r ).toBeCloseTo(255, 0)
- expect( g ).toBeCloseTo(255, 0)
- expect( b ).toBeCloseTo(255, 0)
+ let color = new SVG.Color(255, 255, 255).cmyk().rgb()
+ expect( color.r ).toBeCloseTo(255, 0)
+ expect( color.g ).toBeCloseTo(255, 0)
+ expect( color.b ).toBeCloseTo(255, 0)
+ expect( color.toHex() ).toBe('#ffffff')
})
})
}
function is (object, space) {
- for (const key of space) {
- if (object[key] == null) {
+ for (let i = space.length; i--;) {
+ if (object[space[i]] == null) {
return false
}
}
Input and Output methods
*/
- toHex () {
+ _clamped () {
let { _a, _b, _c } = this.rgb()
- let [ r, g, b ] = [ _a, _b, _c ].map(componentHex)
+ let { max, min, round } = Math
+ let format = v => max(0, min(round(v), 255))
+ return [ _a, _b, _c ].map(format)
+ }
+
+ toHex () {
+ let [ r, g, b ] = this._clamped().map(componentHex)
return `#${r}${g}${b}`
}
}
toRgb () {
- let { r, g, b } = this.rgb()
- let { max, min, round } = Math
- let format = v => max(0, min(round(v), 255))
- let [ rV, gV, bV ] = [ r, g, b ].map(format)
+ let [ rV, gV, bV ] = this._clamped()
let string = `rgb(${rV},${gV},${bV})`
return string
}